
c# Pong program
Ahoj měl bych dotaz, zkoušel jsem si naprogramovat hru PONG na internetu jsem našel jakýsi návod jak jí naprogramovat tak, že vy ovládáte sebe pomocí myše ale hrajete proti "AI" měl bych otázku a to takovou, jak by se to mělo naprogramovat aby místo AI hrál druhý hráč na klávesách W - nahoru, S - dolu.
A ještě aby nahoře byla tabulka skore, která by počítala do 7 a poté by napsala Vyhrál hráč červený/modrý. Níže přikládám program, který zatím mám. Opravdu by se mi to hodilo a byl bych každýmu moc vděčný za pomoc.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Pong
{
public partial class Form1 : Form
{
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
int ballSpeedX = 3;
int ballSpeedY = 3;
int gameTimeInterval = 1;
PictureBox picBoxPlayer, picBoxPlayer2, picBoxBall;
Timer gameTime;
Size sizePlayer = new Size(25, 100);
Size sizePlayer2 = new Size(25, 100);
Size sizeBall = new Size(20, 20);
public Form1()
{
picBoxPlayer = new PictureBox();//
picBoxPlayer2 = new PictureBox();//Initializes the PictureBoxes
picBoxBall = new PictureBox();//
gameTime = new Timer();//Initializes the Timer
gameTime.Enabled = true;//Enables the Timer
gameTime.Interval = gameTimeInterval;//Set the timer's interval
gameTime.Tick += new EventHandler(gameTime_Tick);//Creates the Timer's Tick event
InitializeComponent();
this.Width = SCREEN_WIDTH;
this.Height = SCREEN_HEIGHT;
this.StartPosition = FormStartPosition.CenterScreen;//opens the form in center of the screen
picBoxPlayer.Size = sizePlayer;//sets the size of the picturebox
picBoxPlayer.Location = new Point(picBoxPlayer.Width / 2, ClientSize.Height / 2 - picBoxPlayer.Height / 2);//sets it's location (centered)
picBoxPlayer.BackColor = Color.Blue;//fills the picturebox with a color
this.Controls.Add(picBoxPlayer);//adds the picture box to the form
picBoxPlayer2.Size = sizePlayer2;
picBoxPlayer2.Location = new Point(picBoxPlayer2.Width / 2, ClientSize.Height / 2 - picBoxPlayer2.Height / 2);
picBoxPlayer2.BackColor = Color.Red;
this.Controls.Add(picBoxPlayer2);
picBoxBall.Size = sizeBall;
picBoxBall.Location = new Point(ClientSize.Width / 2 - picBoxBall.Width / 2, ClientSize.Height / 2 - picBoxBall.Height / 2);
picBoxBall.BackColor = Color.Green;
this.Controls.Add(picBoxBall);
}
private void gameTime_Tick(object sender, EventArgs e)
{
picBoxBall.Location = new Point(picBoxBall.Location.X + ballSpeedX, picBoxBall.Location.Y + ballSpeedY);
gameAreaCollisions();//Checks for collisions with the form's border
padlleCollision();//Checks for collisions with the padlles
playerMovement();//Updates the player's position
player2Movement();//Updates the ai's position
}
private void gameAreaCollisions()
{
if (picBoxBall.Location.Y > ClientSize.Height - picBoxBall.Height || picBoxBall.Location.Y < 0)
{
ballSpeedY = -ballSpeedY;
}
else if (picBoxBall.Location.X > ClientSize.Width)
{
resetBall();
}
else if (picBoxBall.Location.X < 0)
{
resetBall();
}
}
private void resetBall()
{
picBoxBall.Location = new Point(ClientSize.Width / 2 - picBoxBall.Width / 2, ClientSize.Height / 2 - picBoxBall.Height / 2);
}
private void playerMovement()
{
if (this.PointToClient(MousePosition).Y >= picBoxPlayer.Height / 2 && this.PointToClient(MousePosition).Y <= ClientSize.Height - picBoxPlayer.Height / 2)
{
int playerX = picBoxPlayer.Width / 2;
int playerY = this.PointToClient(MousePosition).Y - picBoxPlayer.Height / 2;
//on click W player.Y++;
//on click S player.Y--;
picBoxPlayer.Location = new Point(playerX, playerY);
}
}
private void player2Movement()
{
if (ballSpeedX > 0)
{
picBoxPlayer2.Location = new Point(ClientSize.Width - (picBoxPlayer2.Width + picBoxPlayer2.Width / 2), picBoxBall.Location.Y - picBoxPlayer2.Height / 2);
}
}
private void padlleCollision()
{
if (picBoxBall.Bounds.IntersectsWith(picBoxPlayer2.Bo unds))
{
ballSpeedX = -ballSpeedX;
}
if (picBoxBall.Bounds.IntersectsWith(picBoxPlayer.Bou nds))
{
ballSpeedX = -ballSpeedX;
}
}
}
}