Zadatak: Napraviti igricu u kojoj se loptica odbija od reket na dnu, a ako loptica "ispadne" mimo reket, onda je igrica gotova.

Dole je dat primer u kojem bi moglo i da radi. Pokušajte da napravite sami tu igricu kod kuće, koristeći verziju Visual Studija koju god da imate. Treba vam Forma (Form1), panel (racket) u nekoj boji koji će izigravati reket, boks sa slikom loptice (ball), tajmer za to da se za svaki tik menja nešto u igrici. Dodajte način da prikažete broj bodova. Pitanje za razmišljanje: Kako se izlazi iz igrice?

Pogledajte sledeći kod:

namespace LopticaSkocica
{
public partial class Form1 : Form
{
public int speed_left = 10; // speed of ball
public int speed_top = 10;
public int point = 0; // score point

public Form1()
{
InitializeComponent();
timer1.Enabled = true;
Cursor.Hide(); // hide the cursor

this.FormBorderStyle = FormBorderStyle.None; // remove any border
this.TopMost = true; // bring the form to the front
this.Bounds = Screen.PrimaryScreen.Bounds; // make it fullscreen
racket.Top = this.Bottom - (this.Bottom / 10); // set the position of racket
}

private void timer1_Tick(object sender, EventArgs e)
{
racket.Left = Cursor.Position.X - (racket.Width / 2); // set the center of the racket to the position of the cursor
ball.Left += speed_left; // move the ball
ball.Top += speed_top;

//racket collision:

if(ball.Bottom >= racket.Top && ball.Bottom <= racket.Bottom && ball.Left >= racket.Left && ball.Right <= racket.Right)
{
speed_top += 2;
speed_left += 2;
speed_top = -speed_top; // change the direction
point += 1;
lblBodovi.Text = point.ToString();
}

//colision with wall

if(ball.Left <= this.Left)
{
speed_left = -speed_left;
}
if(ball.Right >= this.Right)
{
speed_left = -speed_left;
}
if(ball.Bottom >= this.Bottom)
{
timer1.Enabled = false; // ball is out - stop the game
MessageBox.Show("Igra je gotova");
}
if(ball.Top <= this.Top)
{
speed_top = -speed_top;
}

}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Escape)
{
Application.Exit(); // press ESC to quit
}
}
}
}




Last modified: Wednesday, 24 November 2021, 3:37 PM