<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>game development Archives - SoftUni Global</title>
	<atom:link href="https://softuni.org/tag/game-development/feed/" rel="self" type="application/rss+xml" />
	<link>https://softuni.org/tag/game-development/</link>
	<description>Learn Programming and Start a Developer Job</description>
	<lastBuildDate>Fri, 25 Nov 2022 09:16:17 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.11</generator>

<image>
	<url>https://softuni.org/wp-content/uploads/2022/04/cropped-SoftUni-Global-Logo-Square-notext-32x32.png</url>
	<title>game development Archives - SoftUni Global</title>
	<link>https://softuni.org/tag/game-development/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Making A Sliding Puzzle in Java: Step-by-step Guide [Project Tutorials]</title>
		<link>https://softuni.org/project-tutorials/making-a-sliding-puzzle-in-java/</link>
					<comments>https://softuni.org/project-tutorials/making-a-sliding-puzzle-in-java/#comments</comments>
		
		<dc:creator><![CDATA[Nikol Ruseva]]></dc:creator>
		<pubDate>Mon, 17 Oct 2022 13:05:04 +0000</pubDate>
				<category><![CDATA[Step-by-Step Project Tutorials]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[Java programming]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[puzzle]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[step-by-step]]></category>
		<guid isPermaLink="false">https://softuni.org/?p=23354</guid>

					<description><![CDATA[<p>In this edition of the Project Tutorial series, we will create a simple sliding puzzle game in Java.</p>
<p>The post <a href="https://softuni.org/project-tutorials/making-a-sliding-puzzle-in-java/">Making A Sliding Puzzle in Java: Step-by-step Guide [Project Tutorials]</a> appeared first on <a href="https://softuni.org">SoftUni Global</a>.</p>
]]></description>
										<content:encoded><![CDATA[		<div data-elementor-type="wp-post" data-elementor-id="23354" class="elementor elementor-23354" data-elementor-post-type="post">
						<section class="elementor-section elementor-top-section elementor-element elementor-element-5d5405e elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="5d5405e" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-0d32134" data-id="0d32134" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
						<div class="elementor-element elementor-element-9216798 elementor-widget elementor-widget-text-editor" data-id="9216798" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">Sliding puzzles are mechanical devices made of blocks of wood or plastic set into a frame as well as simple but addictive computer games that improve your problem-solving skills. The pieces are usually simple shapes or imprinted with colors, patterns, sections of a bigger picture, numbers, or letters.  </span><span style="font-weight: 400;">Sliding puzzles, also known as sliders or sliding-block puzzles, are different from rearrangement puzzles in that the pieces cannot be lifted or removed from the frame. To win, you have to arrange them in a specific pattern by sliding them into certain routes on the board. Each move opens a new path and rearranges the pieces, so you have to plan carefully in order to achieve the end configuration.</span></p><p><span style="font-weight: 400;">Following this tutorial, you will make <strong>your own 3&#215;3 sliding puzzle</strong> using Java.</span><span style="font-weight: 400;"><br /></span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-ac60dbc elementor-widget elementor-widget-image" data-id="ac60dbc" data-element_type="widget" data-widget_type="image.default">
				<div class="elementor-widget-container">
															<img decoding="async" src="https://softuni.org/wp-content/uploads/2022/10/Puzzle.gif" title="Sliding-Puzzle-Gameplay" alt="Sliding-Puzzle-Gameplay" loading="lazy" />															</div>
				</div>
				<div class="elementor-element elementor-element-34fb39a elementor-widget elementor-widget-text-editor" data-id="34fb39a" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;"><strong>Note: To create the game, you can use more advanced implementations like arrays and matrices. The method used in this tutorial is simple and targets absolute beginners.</strong><br />Let’s begin.</span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-2041cf4 elementor-widget elementor-widget-heading" data-id="2041cf4" data-element_type="widget" data-widget_type="heading.default">
				<div class="elementor-widget-container">
					<h2 class="elementor-heading-title elementor-size-xl">Before You Start</h2>				</div>
				</div>
				<div class="elementor-element elementor-element-1b467b7 elementor-widget elementor-widget-text-editor" data-id="1b467b7" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">For the user interface, you use <strong>Swing</strong>. </span>Swing<span style="font-weight: 400;"> is a <strong>GUI (Graphical User Interface)</strong> widget toolkit for Java.</span><span style="font-weight: 400;"> It is built on top of the AWT (Abstract Windowing Toolkit) API. Unlike AWT, Java Swing provides platform-independent and lightweight components.</span></p><p><span style="font-weight: 400;">The javax.swing package provides classes for the Java Swing API such as <strong>JButton, JTextField, JLabel, JMenu</strong>, etc. You will use the Main method to start the program, but the programming logic will be written in a separate class.</span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-3c4f9e1 elementor-widget elementor-widget-heading" data-id="3c4f9e1" data-element_type="widget" data-widget_type="heading.default">
				<div class="elementor-widget-container">
					<h2 class="elementor-heading-title elementor-size-xl">
Preparing Variables
</h2>				</div>
				</div>
				<div class="elementor-element elementor-element-c74cf06 elementor-widget elementor-widget-text-editor" data-id="c74cf06" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">Create a new project and give it a descriptive and meaningful name. </span></p><p><span style="font-weight: 400;">Your <strong>Main</strong> <strong>method</strong> should look like this:</span><span style="font-weight: 400;"><br /></span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-6a49652 elementor-widget elementor-widget-code-highlight" data-id="6a49652" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-csharp line-numbers">
				<code readonly="true" class="language-csharp">
					<xmp>public class Main {
   public static void main(String[] args){
       new Puzzle();
   }
}</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-411639f elementor-widget elementor-widget-text-editor" data-id="411639f" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">Right-click on your project folder and create a new Java class &#8211; </span><b>Puzzle</b><span style="font-weight: 400;">. To use the functionalities (methods) of Swing, the class should </span><b>extend JFrame. </b><span style="font-weight: 400;">To make certain actions happen when you click on the puzzle pieces, you also need an event listener. Make the class </span><b>implement ActionListener.</b><b><br /></b></p>								</div>
				</div>
				<div class="elementor-element elementor-element-f4a4827 elementor-widget elementor-widget-code-highlight" data-id="f4a4827" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-csharp line-numbers">
				<code readonly="true" class="language-csharp">
					<xmp>import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Puzzle extends JFrame implements ActionListener{
}</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-b3697b2 elementor-widget elementor-widget-text-editor" data-id="b3697b2" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">Your puzzle will be <strong>3&#215;3</strong> so you need to declare </span><b>9 buttons </b><span style="font-weight: 400;">for each of the puzzle pieces, as well as a </span><b>shuffle </b><span style="font-weight: 400;">button to rearrange them. You can also implement a </span><b>counter </b><span style="font-weight: 400;">for the number of clicks the user has performed. To make the counter, you need an </span><b>integer </b><span style="font-weight: 400;">and a </span><b>label </b><span style="font-weight: 400;">to visualize the number.</span><span style="font-weight: 400;"><br /></span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-4b97940 elementor-widget elementor-widget-code-highlight" data-id="4b97940" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-csharp line-numbers">
				<code readonly="true" class="language-csharp">
					<xmp>JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,shuffle;
int counter = 0;
JLabel counterLabel;</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-000838a elementor-widget elementor-widget-heading" data-id="000838a" data-element_type="widget" data-widget_type="heading.default">
				<div class="elementor-widget-container">
					<h2 class="elementor-heading-title elementor-size-xl">Creating Constructor
</h2>				</div>
				</div>
				<div class="elementor-element elementor-element-138d7ce elementor-widget elementor-widget-text-editor" data-id="138d7ce" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">When you start the game, the first thing you will see is a new <strong>game</strong> <strong>window</strong>. Let’s set it up. Because the Puzzle class inherits (extends) from JFrame, it can call JFrame methods like <strong>setSize()</strong> and <strong>add(element)</strong>.</span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-ecc7ef9 elementor-widget elementor-widget-code-highlight" data-id="ecc7ef9" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-csharp line-numbers">
				<code readonly="true" class="language-csharp">
					<xmp>Puzzle(){
   setSize(400,400);
   setLayout(null);
   setVisible(true);
   setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-550d174 elementor-widget elementor-widget-text-editor" data-id="550d174" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">It is <strong>400&#215;400 pixels</strong> in size (which you can customize to your preference) and will close when you click the<strong> X button</strong> on the top right.</span></p><p><span style="font-weight: 400;">Next, you need to initialize all the elements.</span><span style="font-weight: 400;"><br /></span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-7dbb766 elementor-widget elementor-widget-code-highlight" data-id="7dbb766" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-csharp line-numbers">
				<code readonly="true" class="language-csharp">
					<xmp>b1 = new JButton("1");
b2 = new JButton(" ");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");
b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("2");
shuffle = new JButton("Shuffle!");
counterLabel = new JLabel("Clicks: 0");</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-cde3b5f elementor-widget elementor-widget-text-editor" data-id="cde3b5f" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">Then <strong>add</strong> these elements <strong>to the window</strong> and set proper <strong>bounds</strong>.</span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-ee2595f elementor-widget elementor-widget-code-highlight" data-id="ee2595f" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-csharp line-numbers">
				<code readonly="true" class="language-csharp">
					<xmp>add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);add(b7);add(b8);add(b9); add(shuffle);
Container contentPane = this.getContentPane();
contentPane.add(counterLabel);

b1.setBounds(90,60,50,40);
b2.setBounds(160,60,50,40);
b3.setBounds(230,60,50,40);
b4.setBounds(90,115,50,40);
b5.setBounds(160,115,50,40);
b6.setBounds(230,115,50,40);
b7.setBounds(90,170,50,40);
b8.setBounds(160,170,50,40);
b9.setBounds(230,170,50,40);
shuffle.setBounds(135,245,100,40);
counterLabel.setBounds(145,15,180,40);</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-473d62c elementor-widget elementor-widget-text-editor" data-id="473d62c" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">Now run the program. It should look like this:</span><span style="font-weight: 400;"><br /></span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-2fe425a elementor-widget elementor-widget-image" data-id="2fe425a" data-element_type="widget" data-widget_type="image.default">
				<div class="elementor-widget-container">
															<img fetchpriority="high" decoding="async" width="476" height="484" src="https://softuni.org/wp-content/uploads/2022/10/Sliding-Puzzle.png" class="attachment-large size-large wp-image-23359" alt="Sliding-Puzzle" srcset="https://softuni.org/wp-content/uploads/2022/10/Sliding-Puzzle.png 476w, https://softuni.org/wp-content/uploads/2022/10/Sliding-Puzzle-295x300.png 295w" sizes="(max-width: 476px) 100vw, 476px" />															</div>
				</div>
				<div class="elementor-element elementor-element-2a20d4a elementor-widget elementor-widget-text-editor" data-id="2a20d4a" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">The next step is <strong>optional</strong>, but it will improve the <strong>visual appeal</strong> of your game. It involves changing the background color of the puzzle pieces, the font, and the font size. You can select your own or use the ones from this example.</span><span style="font-weight: 400;"><br /></span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-886c880 elementor-widget elementor-widget-code-highlight" data-id="886c880" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-csharp line-numbers">
				<code readonly="true" class="language-csharp">
					<xmp>shuffle.setBackground(Color.LIGHT_GRAY);

b1.setBackground(Color.decode("#5adbb5"));
b2.setBackground(Color.decode("#5adbb5"));
b3.setBackground(Color.decode("#5adbb5"));
b4.setBackground(Color.decode("#5adbb5"));
b5.setBackground(Color.decode("#5adbb5"));
b6.setBackground(Color.decode("#5adbb5"));
b7.setBackground(Color.decode("#5adbb5"));
b8.setBackground(Color.decode("#5adbb5"));
b9.setBackground(Color.decode("#5adbb5"));

b1.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 18));
b2.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 18));
b3.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 18));
b4.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 18));
b5.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 18));
b6.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 18));
b7.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 18));
b8.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 18));
b9.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 18));
shuffle.setFont(new Font(Font.DIALOG, Font.PLAIN, 18));
counterLabel.setFont(new Font(Font.DIALOG, Font.PLAIN, 18));
</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-bcddbdc elementor-widget elementor-widget-spacer" data-id="bcddbdc" data-element_type="widget" data-widget_type="spacer.default">
				<div class="elementor-widget-container">
							<div class="elementor-spacer">
			<div class="elementor-spacer-inner"></div>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-6ab465e elementor-widget elementor-widget-text-editor" data-id="6ab465e" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">In the end, you get:</span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-389ed4c elementor-widget elementor-widget-image" data-id="389ed4c" data-element_type="widget" data-widget_type="image.default">
				<div class="elementor-widget-container">
															<img decoding="async" width="474" height="485" src="https://softuni.org/wp-content/uploads/2022/10/Sliding-Puzzle-Customized.png" class="attachment-large size-large wp-image-23360" alt="" srcset="https://softuni.org/wp-content/uploads/2022/10/Sliding-Puzzle-Customized.png 474w, https://softuni.org/wp-content/uploads/2022/10/Sliding-Puzzle-Customized-293x300.png 293w" sizes="(max-width: 474px) 100vw, 474px" />															</div>
				</div>
				<div class="elementor-element elementor-element-e6e05a0 elementor-widget elementor-widget-text-editor" data-id="e6e05a0" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">Finally, attach an action listener to every button:</span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-1265ac8 elementor-widget elementor-widget-code-highlight" data-id="1265ac8" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-javascript line-numbers">
				<code readonly="true" class="language-javascript">
					<xmp>b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
shuffle.addActionListener(this);
}</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-0330f2b elementor-widget elementor-widget-text-editor" data-id="0330f2b" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">And the Puzzle <strong>constructor</strong> is done.</span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-097e0c9 elementor-widget elementor-widget-heading" data-id="097e0c9" data-element_type="widget" data-widget_type="heading.default">
				<div class="elementor-widget-container">
					<h2 class="elementor-heading-title elementor-size-xl">Adding Action</h2>				</div>
				</div>
				<div class="elementor-element elementor-element-6ac099f elementor-widget elementor-widget-text-editor" data-id="6ac099f" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">The Action Listener is a simple event handler with only one method &#8211; <strong>actionPerformed </strong></span><span style="font-weight: 400;"><strong>(ActionEvent e)</strong>. It is used to define what should happen when a user performs a certain operation. It <strong>registers</strong> <strong>the</strong> <strong>event</strong> of clicking a button, for example. The ActionListener is one for all buttons, so you have to make a separate if statement for each one of them in the actionPerformed method. Let’s start with the </span><b>Shuffle </b><span style="font-weight: 400;">button.</span><span style="font-weight: 400;"><br /></span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-ce1e1d9 elementor-widget elementor-widget-code-highlight" data-id="ce1e1d9" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-javascript line-numbers">
				<code readonly="true" class="language-javascript">
					<xmp>public void actionPerformed(ActionEvent e){
   if(e.getSource() == shuffle){
       String s = b4.getText();
       b4.setText(b9.getText());
       b9.setText(s);
       s = b1.getText();
       b1.setText(b5.getText());
       b5.setText(s);
       s = b2.getText();
       b2.setText(b7.getText());
       b7.setText(s);
       counter = -1;
       counterLabel.setText("Clicks: 0");
   }</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-91f92b4 elementor-widget elementor-widget-text-editor" data-id="91f92b4" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;"><strong>The logic</strong> of moving puzzle pieces is simple: instead of physically moving the pieces around the board, you simply <strong>change</strong> <strong>their</strong> <strong>text</strong>.</span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-e3f817b elementor-widget elementor-widget-code-highlight" data-id="e3f817b" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-javascript line-numbers">
				<code readonly="true" class="language-javascript">
					<xmp>if(e.getSource() == b1){
   String s = b1.getText();
   if(b2.getText().equals(" ")){ b2.setText(s); b1.setText(" ");}
   else if(b4.getText().equals(" ")){ b4.setText(s); b1.setText(" ");}
}</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-b36e965 elementor-widget elementor-widget-text-editor" data-id="b36e965" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">Let’s break the process into a <strong>few steps</strong>:</span><span style="font-weight: 400;"><br /></span></p><ol><li><span style="font-weight: 400;"> Extract the text from the current button;</span></li><li><span style="font-weight: 400;"> Determine which of the adjacent buttons has no text;</span></li><li><span style="font-weight: 400;"> Substitute the empty text with the extracted one;</span></li><li><span style="font-weight: 400;"> Set the text of the current button to an empty string.</span></li></ol><p><span style="font-weight: 400;">Follow this logic for the other pieces as well.</span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-48b1c62 elementor-widget elementor-widget-code-highlight" data-id="48b1c62" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-javascript line-numbers">
				<code readonly="true" class="language-javascript">
					<xmp>if(e.getSource() == b2){
   String s=b2.getText();
   if(b1.getText().equals(" ")){ b1.setText(s); b2.setText(" ");}
   else if(b3.getText().equals(" ")){ b3.setText(s); b2.setText(" ");}
   else if(b5.getText().equals(" ")){ b5.setText(s); b2.setText(" ");}
}
if(e.getSource() == b3){
   String s=b3.getText();
   if(b2.getText().equals(" ")){ b2.setText(s); b3.setText(" ");}
   else if(b6.getText().equals(" ")){ b6.setText(s); b3.setText(" ");}
}
if(e.getSource() == b4){
   String s=b4.getText();
   if(b1.getText().equals(" ")){ b1.setText(s); b4.setText(" ");}
   else if(b7.getText().equals(" ")){ b7.setText(s); b4.setText(" ");}
   else if(b5.getText().equals(" ")){ b5.setText(s); b4.setText(" ");}
}
if(e.getSource() == b5){
   String s=b5.getText();
   if(b2.getText().equals(" ")){ b2.setText(s); b5.setText(" ");}
   else if(b4.getText().equals(" ")){ b4.setText(s); b5.setText(" ");}
   else if(b6.getText().equals(" ")){ b6.setText(s); b5.setText(" ");}
   else if(b8.getText().equals(" ")){ b8.setText(s); b5.setText(" ");}
}
if(e.getSource() == b6){

   String s=b6.getText();
   if(b9.getText().equals(" ")){ b9.setText(s); b6.setText(" ");}
   else if(b3.getText().equals(" ")){ b3.setText(s); b6.setText(" ");}
   else if(b5.getText().equals(" ")){ b5.setText(s); b6.setText(" ");}

}
if(e.getSource() == b7){
   String s=b7.getText();
   if(b4.getText().equals(" ")){ b4.setText(s); b7.setText(" ");}
   else if(b8.getText().equals(" ")){ b8.setText(s); b7.setText(" ");}

}
if(e.getSource() == b8){
   String s=b8.getText();
   if(b7.getText().equals(" ")){ b7.setText(s); b8.setText(" ");}
   else if(b9.getText().equals(" ")){ b9.setText(s); b8.setText(" ");}
   else if(b5.getText().equals(" ")){ b5.setText(s); b8.setText(" ");}
}
</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-5ebe135 elementor-widget elementor-widget-text-editor" data-id="5ebe135" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">The last button marks the end of the puzzle, so it should also include a statement that checks whether it has been solved. For that to happen, the pieces must be put in the following order: </span><span style="font-weight: 400;"><br /></span><strong>1 2 3<br />4 5 6<br />7 8 empty</strong></p><p><span style="font-weight: 400;">Therefore, the simplest way to verify this order is to check the text on each button.</span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-e158015 elementor-widget elementor-widget-code-highlight" data-id="e158015" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-javascript line-numbers">
				<code readonly="true" class="language-javascript">
					<xmp>if(e.getSource() == b9){
   String s=b9.getText();
   if(b6.getText().equals(" ")){ b6.setText(s); b9.setText(" ");}
   else if(b8.getText().equals(" ")){ b8.setText(s); b9.setText(" ");}

   if(b1.getText().equals("1")&&b2.getText().equals("2")&&b3.getText()
           .equals("3")&&b4.getText().equals("4")&&b5.getText().equals("5")
           &&b6.getText().equals("6")&&b7.getText().equals("7")&&b8.getText()
           .equals("8")&&b9.getText().equals(" ")){
       JOptionPane.showMessageDialog(Puzzle.this,"YOU WON!\n" + "You clicked: " + counter + " times.");
   }
}</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-c26a37f elementor-widget elementor-widget-text-editor" data-id="c26a37f" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">You can add a <strong>dialog box</strong> with a custom message that will <strong>pop up when the puzzle is solved</strong>. To do that, use the JOptionPane.showMessageDialog() method.</span></p><p><span style="font-weight: 400;">Finally, <strong>increase</strong> the <strong>counter integer</strong> and <strong>update the label&#8217;s text</strong> to the new value at the end of the if statements.</span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-1c61d54 elementor-widget elementor-widget-code-highlight" data-id="1c61d54" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-javascript line-numbers">
				<code readonly="true" class="language-javascript">
					<xmp>counter++;
counterLabel.setText("Clicks: " + counter);</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-2d2541e elementor-widget elementor-widget-text-editor" data-id="2d2541e" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><strong>Your sliding puzzle is ready!</strong></p>								</div>
				</div>
				<div class="elementor-element elementor-element-c3b578a elementor-widget elementor-widget-heading" data-id="c3b578a" data-element_type="widget" data-widget_type="heading.default">
				<div class="elementor-widget-container">
					<h2 class="elementor-heading-title elementor-size-xl">Conclusion</h2>				</div>
				</div>
				<div class="elementor-element elementor-element-6f56a8e elementor-widget elementor-widget-text-editor" data-id="6f56a8e" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p><span style="font-weight: 400;">By following this tutorial, you have programmed your own sliding puzzle in Java using simple if statements and some basic functionality of the Swing library. Explore the library’s other features and think of ways to <strong>expand your project</strong> by adding, for example, a scoreboard, an option to switch between different puzzle sizes, randomization of the shuffle, etc. And don’t forget to <strong>upload</strong> it to your <a href="https://softuni.org/dev-concepts/what-is-git-and-how-to-work-with-github/">GitHub</a> profile.</span><span style="font-weight: 400;"><br /></span><span style="font-weight: 400;"><br /></span><span style="font-weight: 400;">To check out <strong>the final code</strong> and compare it to yours, </span><b>click the link below</b><span style="font-weight: 400;">.</span></p><p><span style="font-weight: 400;">If you need any help or advice, <strong>leave a comment.</strong></span></p>								</div>
				</div>
				<div class="elementor-element elementor-element-0c72984 elementor-widget elementor-widget-spacer" data-id="0c72984" data-element_type="widget" data-widget_type="spacer.default">
				<div class="elementor-widget-container">
							<div class="elementor-spacer">
			<div class="elementor-spacer-inner"></div>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-edfdd22 elementor-widget elementor-widget-text-editor" data-id="edfdd22" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<h2>Lesson Topics</h2><div><div><div>In this tutorial we cover the following steps:</div></div><ul><li><strong>Preparing Variables</strong></li><li><strong>Creating Constructor</strong></li><li><strong>Adding Logic to Action Listener</strong></li></ul></div>								</div>
				</div>
				<div class="elementor-element elementor-element-93d53ef elementor-widget elementor-widget-spacer" data-id="93d53ef" data-element_type="widget" data-widget_type="spacer.default">
				<div class="elementor-widget-container">
							<div class="elementor-spacer">
			<div class="elementor-spacer-inner"></div>
		</div>
						</div>
				</div>
					</div>
		</div>
					</div>
		</section>
				<section class="elementor-section elementor-top-section elementor-element elementor-element-dc263e9 elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="dc263e9" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-2f88ff6" data-id="2f88ff6" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
						<div class="elementor-element elementor-element-96efd1f elementor-align-center elementor-widget elementor-widget-button" data-id="96efd1f" data-element_type="widget" data-widget_type="button.default">
				<div class="elementor-widget-container">
									<div class="elementor-button-wrapper">
					<a class="elementor-button elementor-button-link elementor-size-lg" href="https://github.com/SoftUni/SoftUni-Project-Tutorials-Puzzle-Game-Java" target="_blank">
						<span class="elementor-button-content-wrapper">
						<span class="elementor-button-icon">
				<svg aria-hidden="true" class="e-font-icon-svg e-fas-laptop-code" viewBox="0 0 640 512" xmlns="http://www.w3.org/2000/svg"><path d="M255.03 261.65c6.25 6.25 16.38 6.25 22.63 0l11.31-11.31c6.25-6.25 6.25-16.38 0-22.63L253.25 192l35.71-35.72c6.25-6.25 6.25-16.38 0-22.63l-11.31-11.31c-6.25-6.25-16.38-6.25-22.63 0l-58.34 58.34c-6.25 6.25-6.25 16.38 0 22.63l58.35 58.34zm96.01-11.3l11.31 11.31c6.25 6.25 16.38 6.25 22.63 0l58.34-58.34c6.25-6.25 6.25-16.38 0-22.63l-58.34-58.34c-6.25-6.25-16.38-6.25-22.63 0l-11.31 11.31c-6.25 6.25-6.25 16.38 0 22.63L386.75 192l-35.71 35.72c-6.25 6.25-6.25 16.38 0 22.63zM624 416H381.54c-.74 19.81-14.71 32-32.74 32H288c-18.69 0-33.02-17.47-32.77-32H16c-8.8 0-16 7.2-16 16v16c0 35.2 28.8 64 64 64h512c35.2 0 64-28.8 64-64v-16c0-8.8-7.2-16-16-16zM576 48c0-26.4-21.6-48-48-48H112C85.6 0 64 21.6 64 48v336h512V48zm-64 272H128V64h384v256z"></path></svg>			</span>
									<span class="elementor-button-text">Download Project</span>
					</span>
					</a>
				</div>
								</div>
				</div>
					</div>
		</div>
					</div>
		</section>
				<section class="elementor-section elementor-top-section elementor-element elementor-element-3cd15ec elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="3cd15ec" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-56bfdb0" data-id="56bfdb0" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
							</div>
		</div>
					</div>
		</section>
				</div>
		<p>The post <a href="https://softuni.org/project-tutorials/making-a-sliding-puzzle-in-java/">Making A Sliding Puzzle in Java: Step-by-step Guide [Project Tutorials]</a> appeared first on <a href="https://softuni.org">SoftUni Global</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://softuni.org/project-tutorials/making-a-sliding-puzzle-in-java/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>How To Make A Pong Game in C#: Step-by-step Guide [Project Tutorials]</title>
		<link>https://softuni.org/project-tutorials/how-to-make-a-pong-game-in-csharp-guide/</link>
					<comments>https://softuni.org/project-tutorials/how-to-make-a-pong-game-in-csharp-guide/#comments</comments>
		
		<dc:creator><![CDATA[Nikol Ruseva]]></dc:creator>
		<pubDate>Thu, 01 Sep 2022 14:15:30 +0000</pubDate>
				<category><![CDATA[Step-by-Step Project Tutorials]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software development]]></category>
		<category><![CDATA[step-by-step]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<guid isPermaLink="false">https://softuni.org/?p=23082</guid>

					<description><![CDATA[<p>In this edition of the Project Tutorial series, we will create a simple Pong game with C#.</p>
<p>The post <a href="https://softuni.org/project-tutorials/how-to-make-a-pong-game-in-csharp-guide/">How To Make A Pong Game in C#: Step-by-step Guide [Project Tutorials]</a> appeared first on <a href="https://softuni.org">SoftUni Global</a>.</p>
]]></description>
										<content:encoded><![CDATA[		<div data-elementor-type="wp-post" data-elementor-id="23082" class="elementor elementor-23082" data-elementor-post-type="post">
						<section class="elementor-section elementor-top-section elementor-element elementor-element-5d5405e elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="5d5405e" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-0d32134" data-id="0d32134" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
						<div class="elementor-element elementor-element-9216798 elementor-widget elementor-widget-text-editor" data-id="9216798" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p>Pong is a two-dimensional sports game that is similar to table tennis. It is one of the earliest video games, first released in 1972. Since then, it has been recreated multiple times in different programming languages. Today, you will learn how to make <strong>your own version</strong> of the game using <strong>simple</strong> <strong>programming concepts in C#</strong>.</p><p>Let’s begin.</p>								</div>
				</div>
				<div class="elementor-element elementor-element-2041cf4 elementor-widget elementor-widget-heading" data-id="2041cf4" data-element_type="widget" data-widget_type="heading.default">
				<div class="elementor-widget-container">
					<h2 class="elementor-heading-title elementor-size-xl">Creating the Field</h2>				</div>
				</div>
				<div class="elementor-element elementor-element-c74cf06 elementor-widget elementor-widget-text-editor" data-id="c74cf06" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p>First, you need to prepare the field with an upper and a lower border and two rackets. Declare two constants of type int that define the dimensions of the field &#8211; <b>fieldWidth </b>and <b>fieldLength. </b>To visualize the upper and the lower border, use the string method <b>Repeat </b>on the <b>fieldTile </b>character variable.</p>								</div>
				</div>
				<div class="elementor-element elementor-element-6a49652 elementor-widget elementor-widget-code-highlight" data-id="6a49652" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-csharp line-numbers">
				<code readonly="true" class="language-csharp">
					<xmp>const int fieldLength = 50, fieldWidth = 15;
const char fieldTile = '#';
string line = string.Concat(Enumerable.Repeat(fieldTile, fieldLength));</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-411639f elementor-widget elementor-widget-text-editor" data-id="411639f" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p>Then create the game loop and <b>print </b>the borders on the console. The first line starts from coordinates <b>0,0</b> and the second one &#8211; <b>0, fieldWidth</b>(15).</p>								</div>
				</div>
				<div class="elementor-element elementor-element-f4a4827 elementor-widget elementor-widget-code-highlight" data-id="f4a4827" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-csharp line-numbers">
				<code readonly="true" class="language-csharp">
					<xmp>while(true) 
{ 
    Console.SetCursorPosition(0,0);
    Console.WriteLine(line);

    Console.SetCursorPosition(0,fieldWidth);
    Console.WriteLine(line);
}
</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-b3697b2 elementor-widget elementor-widget-text-editor" data-id="b3697b2" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p>Next, make a variable for the rackets’ size &#8211; <b>racketLength, </b>for the character used to visualize them &#8211; <b>racketTile, </b>and for their positions &#8211; <b>leftRacketHeight </b>and <b>rightRacketHeight</b>.</p>								</div>
				</div>
				<div class="elementor-element elementor-element-4b97940 elementor-widget elementor-widget-code-highlight" data-id="4b97940" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-csharp line-numbers">
				<code readonly="true" class="language-csharp">
					<xmp>const int racketLength = fieldWidth / 4;
const char racketTile = '|';
            
int leftRacketHeight = 0;
int rightRacketHeight = 0;</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-138d7ce elementor-widget elementor-widget-text-editor" data-id="138d7ce" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p>To make the rackets appear on the field,add a <b>for loop </b>in the game loop.</p>								</div>
				</div>
				<div class="elementor-element elementor-element-ecc7ef9 elementor-widget elementor-widget-code-highlight" data-id="ecc7ef9" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-csharp line-numbers">
				<code readonly="true" class="language-csharp">
					<xmp>for(int i = 0; i < racketLength; i++)
{
    Console.SetCursorPosition(0, i + 1 + leftRacketHeight);
    Console.WriteLine(racketTile);
    Console.SetCursorPosition(fieldLength - 1, i + 1 + rightRacketHeight);
    Console.WriteLine(racketTile);
}</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-0853386 elementor-widget elementor-widget-spacer" data-id="0853386" data-element_type="widget" data-widget_type="spacer.default">
				<div class="elementor-widget-container">
							<div class="elementor-spacer">
			<div class="elementor-spacer-inner"></div>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-000838a elementor-widget elementor-widget-heading" data-id="000838a" data-element_type="widget" data-widget_type="heading.default">
				<div class="elementor-widget-container">
					<h2 class="elementor-heading-title elementor-size-xl">Player Movement</h2>				</div>
				</div>
				<div class="elementor-element elementor-element-550d174 elementor-widget elementor-widget-text-editor" data-id="550d174" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p>Make a loop that will continue until a key is pressed. After the loop, check <b>which key has been pressed</b> using a switch and <b>update the rackets’ positions</b> based on that. C<b>lear the previous positions</b> with another for loop.</p>								</div>
				</div>
				<div class="elementor-element elementor-element-7dbb766 elementor-widget elementor-widget-code-highlight" data-id="7dbb766" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-csharp line-numbers">
				<code readonly="true" class="language-csharp">
					<xmp>while(!Console.KeyAvailable)
{
    
}
           
//Check which key has been pressed
switch(Console.ReadKey().Key)
{
    case ConsoleKey.UpArrow:
    if(rightRacketHeight > 0)
    {
        rightRacketHeight--;
    }
    break;

    case ConsoleKey.DownArrow:
    if(rightRacketHeight < fieldWidth - racketLength - 1)
    {
        rightRacketHeight++;
    }
    break;

    case ConsoleKey.W:
    if(leftRacketHeight > 0)
    {
        leftRacketHeight--;
    }
    break;
    
    case ConsoleKey.S:
    if(leftRacketHeight < fieldWidth - racketLength - 1)
    {
        leftRacketHeight++;
    }
    break;
}

//Clear the rackets’ previous positions
for(int i = 1; i < fieldWidth; i++)             
{
Console.SetCursorPosition(0,i);
Console.WriteLine(" ");
Console.SetCursorPosition(fieldLength - 1,i);
Console.WriteLine(" ");
}</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-3e05056 elementor-widget elementor-widget-spacer" data-id="3e05056" data-element_type="widget" data-widget_type="spacer.default">
				<div class="elementor-widget-container">
							<div class="elementor-spacer">
			<div class="elementor-spacer-inner"></div>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-91b4156 elementor-widget elementor-widget-heading" data-id="91b4156" data-element_type="widget" data-widget_type="heading.default">
				<div class="elementor-widget-container">
					<h2 class="elementor-heading-title elementor-size-xl">Adding a Ball</h2>				</div>
				</div>
				<div class="elementor-element elementor-element-06be0a6 elementor-widget elementor-widget-text-editor" data-id="06be0a6" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p>What you need to know about the ball is its <b>coordinates</b>, the <b>character representation </b>on the field, and its <b>direction</b>.</p>								</div>
				</div>
				<div class="elementor-element elementor-element-ee2595f elementor-widget elementor-widget-code-highlight" data-id="ee2595f" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-csharp line-numbers">
				<code readonly="true" class="language-csharp">
					<xmp>int ballX = fieldLength / 2;
    int ballY = fieldWidth / 2;
    const char ballTile = 'O';

    bool isBallGoingDown = true;
    bool isBallGoingRight = true;</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-473d62c elementor-widget elementor-widget-text-editor" data-id="473d62c" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p>Let’s go back to the empty while loop that waits for a key to be pressed. It has to <strong>update the ball’s position.</strong></p>								</div>
				</div>
				<div class="elementor-element elementor-element-886c880 elementor-widget elementor-widget-code-highlight" data-id="886c880" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-csharp line-numbers">
				<code readonly="true" class="language-csharp">
					<xmp>while(!Console.KeyAvailable)
{
    Console.SetCursorPosition(ballX, ballY);
    Console.WriteLine(ballTile);
    Thread.Sleep(100); //Adds a timer so that the players have time to react
    
    Console.SetCursorPosition(ballX, ballY);
    Console.WriteLine(" "); //Clears the previous position of the ball
    
    //Update position of the ball
    if(isBallGoingDown)
    {
    ballY++;
    } else
    {
    ballY--;
    }
    if(isBallGoingRight)
    {
    ballX++;
    } else
    {
    ballX--;
    }
}</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-2a20d4a elementor-widget elementor-widget-text-editor" data-id="2a20d4a" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p>However, this code does not limit the movement of the ball to the borders of the field. You need to add more conditions to the same while loop and declare variables to store the players’ points.</p>								</div>
				</div>
				<div class="elementor-element elementor-element-be03d22 elementor-widget elementor-widget-code-highlight" data-id="be03d22" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-javascript line-numbers">
				<code readonly="true" class="language-javascript">
					<xmp>int leftPlayerPoints = 0;
int rightPlayerPoints = 0;
</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-798a6f4 elementor-widget elementor-widget-code-highlight" data-id="798a6f4" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-javascript line-numbers">
				<code readonly="true" class="language-javascript">
					<xmp>if(ballY == 1 || ballY == fieldWidth - 1)
{
isBallGoingDown = !isBallGoingDown; //Change direction
}

if(ballX == 1)
{
    //Left racket hits the ball and it bounces
   if(ballY >= leftRacketHeight + 1 && ballY <= leftRacketHeight + racketLength) 
   {
       isBallGoingRight = !isBallGoingRight;
   }
   else //Ball goes out of the field; Right player scores
   {
      rightPlayerPoints++;
      ballY = fieldWidth / 2;
      ballX = fieldLength / 2;
      Console.SetCursorPosition(scoreboardX, scoreboardY);
      Console.WriteLine($"{leftPlayerPoints} | {rightPlayerPoints}");
      if(rightPlayerPoints == 10)
      {
          goto outer;
      }
   }
}

if(ballX == fieldLength - 2)
{
    //Right racket hits the ball and it bounces
   if(ballY >= rightRacketHeight + 1 && ballY <= rightRacketHeight + racketLength) 
   {
       isBallGoingRight = !isBallGoingRight;
   }
   else //Ball goes out of the field; Left player scores
   {
      leftPlayerPoints++;
      ballY = fieldWidth / 2;
      ballX = fieldLength / 2;
      Console.SetCursorPosition(scoreboardX, scoreboardY);
      Console.WriteLine($"{leftPlayerPoints} | {rightPlayerPoints}");
      if(leftPlayerPoints == 10)
      {
          goto outer;
      }
       
   }
}</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-bcddbdc elementor-widget elementor-widget-spacer" data-id="bcddbdc" data-element_type="widget" data-widget_type="spacer.default">
				<div class="elementor-widget-container">
							<div class="elementor-spacer">
			<div class="elementor-spacer-inner"></div>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-097e0c9 elementor-widget elementor-widget-heading" data-id="097e0c9" data-element_type="widget" data-widget_type="heading.default">
				<div class="elementor-widget-container">
					<h2 class="elementor-heading-title elementor-size-xl">Scoreboard Visualization</h2>				</div>
				</div>
				<div class="elementor-element elementor-element-e6e05a0 elementor-widget elementor-widget-text-editor" data-id="e6e05a0" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p>To add a scoreboard, you need variables that store its position.</p>								</div>
				</div>
				<div class="elementor-element elementor-element-1265ac8 elementor-widget elementor-widget-code-highlight" data-id="1265ac8" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-javascript line-numbers">
				<code readonly="true" class="language-javascript">
					<xmp>int scoreboardX = fieldLength / 2 -2;
int scoreboardY = fieldWidth + 1;
</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-6ac099f elementor-widget elementor-widget-text-editor" data-id="6ac099f" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p>The scoreboard is printed on the console every time a player increases their score. Let’s say the game ends when one of the players reaches <strong>10 points</strong>. You need to break out of the game loop with the <b>goto</b> command.</p>								</div>
				</div>
				<div class="elementor-element elementor-element-ce1e1d9 elementor-widget elementor-widget-code-highlight" data-id="ce1e1d9" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-javascript line-numbers">
				<code readonly="true" class="language-javascript">
					<xmp>//Left Player
leftPlayerPoints++;
ballY = fieldWidth / 2;
ballX = fieldLength / 2;
Console.SetCursorPosition(scoreboardX, scoreboardY);
Console.WriteLine("${leftPlayerPoints} | {rightPlayerPoints}")

if(leftPlayerPoints == 10)
{
      goto outer;
}

//Right Player
rightPlayerPoints++;
ballY = fieldWidth / 2;
ballX = fieldLength / 2;
Console.SetCursorPosition(scoreboardX, scoreboardY);
Console.WriteLine("${leftPlayerPoints} | {rightPlayerPoints}")
if(rightPlayerPoints == 10)
{
      goto outer;
}
</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-91f92b4 elementor-widget elementor-widget-text-editor" data-id="91f92b4" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p>Outside of the game loop put a <strong>marker</strong>, clear the console and reset the cursor. Check who the winner is and print the appropriate message.</p>								</div>
				</div>
				<div class="elementor-element elementor-element-e3f817b elementor-widget elementor-widget-code-highlight" data-id="e3f817b" data-element_type="widget" data-widget_type="code-highlight.default">
				<div class="elementor-widget-container">
							<div class="prismjs-default copy-to-clipboard ">
			<pre data-line="" class="highlight-height language-javascript line-numbers">
				<code readonly="true" class="language-javascript">
					<xmp>outer:;
        Console.Clear();
        Console.SetCursorPosition(0,0);
        
        if(rightPlayerPoints == 10)
        {
        Console.WriteLine("Right player won!");
        } 
        else 
        {
        Console.WriteLine("Left player won!");
        }</xmp>
				</code>
			</pre>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-b36e965 elementor-widget elementor-widget-text-editor" data-id="b36e965" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<p>If you’ve followed all the steps your project should be finished and working now. You can always <strong>adjust the values</strong> of the variables. For example, you can make the field bigger or smaller or increase the speed of the ball to make the game more challenging.</p><p>To check out the final code and compare it to yours <strong>click the link below</strong>.</p>								</div>
				</div>
				<div class="elementor-element elementor-element-0c72984 elementor-widget elementor-widget-spacer" data-id="0c72984" data-element_type="widget" data-widget_type="spacer.default">
				<div class="elementor-widget-container">
							<div class="elementor-spacer">
			<div class="elementor-spacer-inner"></div>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-88292b4 elementor-widget elementor-widget-video" data-id="88292b4" data-element_type="widget" data-settings="{&quot;youtube_url&quot;:&quot;https:\/\/youtu.be\/K0hh_4Lp6_A&quot;,&quot;video_type&quot;:&quot;youtube&quot;,&quot;controls&quot;:&quot;yes&quot;}" data-widget_type="video.default">
				<div class="elementor-widget-container">
							<div class="elementor-wrapper elementor-open-inline">
			<div class="elementor-video"></div>		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-3738a62 elementor-widget elementor-widget-spacer" data-id="3738a62" data-element_type="widget" data-widget_type="spacer.default">
				<div class="elementor-widget-container">
							<div class="elementor-spacer">
			<div class="elementor-spacer-inner"></div>
		</div>
						</div>
				</div>
				<div class="elementor-element elementor-element-edfdd22 elementor-widget elementor-widget-text-editor" data-id="edfdd22" data-element_type="widget" data-widget_type="text-editor.default">
				<div class="elementor-widget-container">
									<h2>Lesson Topics</h2><div><div><div>In this tutorial we cover the following steps:</div></div><ul><li><strong>Creating a Field</strong></li><li><strong>Implementing Player Movement</strong></li><li><strong>Adding a Ball</strong></li><li><strong>Scoreboard Visualization</strong></li></ul></div>								</div>
				</div>
				<div class="elementor-element elementor-element-93d53ef elementor-widget elementor-widget-spacer" data-id="93d53ef" data-element_type="widget" data-widget_type="spacer.default">
				<div class="elementor-widget-container">
							<div class="elementor-spacer">
			<div class="elementor-spacer-inner"></div>
		</div>
						</div>
				</div>
					</div>
		</div>
					</div>
		</section>
				<section class="elementor-section elementor-top-section elementor-element elementor-element-dc263e9 elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="dc263e9" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-2f88ff6" data-id="2f88ff6" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
						<div class="elementor-element elementor-element-96efd1f elementor-align-center elementor-widget elementor-widget-button" data-id="96efd1f" data-element_type="widget" data-widget_type="button.default">
				<div class="elementor-widget-container">
									<div class="elementor-button-wrapper">
					<a class="elementor-button elementor-button-link elementor-size-lg" href="https://github.com/SoftUni/SoftUni-Project-Tutorials---Pong-Game-C-Sharp/" target="_blank">
						<span class="elementor-button-content-wrapper">
						<span class="elementor-button-icon">
				<svg aria-hidden="true" class="e-font-icon-svg e-fas-laptop-code" viewBox="0 0 640 512" xmlns="http://www.w3.org/2000/svg"><path d="M255.03 261.65c6.25 6.25 16.38 6.25 22.63 0l11.31-11.31c6.25-6.25 6.25-16.38 0-22.63L253.25 192l35.71-35.72c6.25-6.25 6.25-16.38 0-22.63l-11.31-11.31c-6.25-6.25-16.38-6.25-22.63 0l-58.34 58.34c-6.25 6.25-6.25 16.38 0 22.63l58.35 58.34zm96.01-11.3l11.31 11.31c6.25 6.25 16.38 6.25 22.63 0l58.34-58.34c6.25-6.25 6.25-16.38 0-22.63l-58.34-58.34c-6.25-6.25-16.38-6.25-22.63 0l-11.31 11.31c-6.25 6.25-6.25 16.38 0 22.63L386.75 192l-35.71 35.72c-6.25 6.25-6.25 16.38 0 22.63zM624 416H381.54c-.74 19.81-14.71 32-32.74 32H288c-18.69 0-33.02-17.47-32.77-32H16c-8.8 0-16 7.2-16 16v16c0 35.2 28.8 64 64 64h512c35.2 0 64-28.8 64-64v-16c0-8.8-7.2-16-16-16zM576 48c0-26.4-21.6-48-48-48H112C85.6 0 64 21.6 64 48v336h512V48zm-64 272H128V64h384v256z"></path></svg>			</span>
									<span class="elementor-button-text">Download Project</span>
					</span>
					</a>
				</div>
								</div>
				</div>
					</div>
		</div>
					</div>
		</section>
				<section class="elementor-section elementor-top-section elementor-element elementor-element-3cd15ec elementor-section-boxed elementor-section-height-default elementor-section-height-default" data-id="3cd15ec" data-element_type="section">
						<div class="elementor-container elementor-column-gap-default">
					<div class="elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-56bfdb0" data-id="56bfdb0" data-element_type="column">
			<div class="elementor-widget-wrap elementor-element-populated">
							</div>
		</div>
					</div>
		</section>
				</div>
		<p>The post <a href="https://softuni.org/project-tutorials/how-to-make-a-pong-game-in-csharp-guide/">How To Make A Pong Game in C#: Step-by-step Guide [Project Tutorials]</a> appeared first on <a href="https://softuni.org">SoftUni Global</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://softuni.org/project-tutorials/how-to-make-a-pong-game-in-csharp-guide/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)

Served from: softuni.org @ 2026-10-30 07:06:50 by W3 Total Cache
-->