using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace Pong
{
public partial class Form1 : Form
{
//variables
float ballX = 255,
ballY = new Random().Next(1, 280),
ballSpeed = 3.0f,
paddleHeight = 80,
cpuHeight = 80,
cpuSkill = 2.0f;
int hits = 0;
Timer t = new Timer();
bool directionX = true,
directionY = true,
ingame = false;
int player1score = 0, player2score = 0;
SolidBrush drawBrush = new SolidBrush(Color.White);
String beginString = "Welcome to Pong! \n" + "by Javier!";
public Form1()
{
//Initializes the component
InitializeComponent();
}
private void normalToolStripMenuItem_Click(object sender, EventArgs e)
{
menuStrip1.Visible = false;
ingame = true;
t = new Timer();
hits = 0;
ballX = 255;
ballY = new Random().Next(1, 280);
ballSpeed = 3.0f;
directionY = true;
directionX = true;
//Sets up timer
t.Interval = 5;
t.Tick += new EventHandler(t_Tick);
t.Start();
//shows picture
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
//Creates mouse movement
this.MouseMove += new MouseEventHandler(Form1_MouseMove);
}
private void destroyToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void battleToolStripMenuItem_Click(object sender,开发者_如何学Python EventArgs e)
{
}
private void easyToolStripMenuItem_Click(object sender, EventArgs e)
{
easyToolStripMenuItem.Checked = true;
mediumToolStripMenuItem.Checked = false;
hardToolStripMenuItem.Checked = false;
pongmasterToolStripMenuItem.Checked = false;
cpuSkill = 0.0f;
}
private void mediumToolStripMenuItem_Click(object sender, EventArgs e)
{
easyToolStripMenuItem.Checked = false;
mediumToolStripMenuItem.Checked = true;
hardToolStripMenuItem.Checked = false;
pongmasterToolStripMenuItem.Checked = false;
cpuSkill = 2.5f;
}
private void hardToolStripMenuItem_Click(object sender, EventArgs e)
{
easyToolStripMenuItem.Checked = false;
mediumToolStripMenuItem.Checked = false;
hardToolStripMenuItem.Checked = true;
pongmasterToolStripMenuItem.Checked = false;
cpuSkill = 5.5f;
}
private void pongmasterToolStripMenuItem_Click(object sender, EventArgs e)
{
easyToolStripMenuItem.Checked = false;
mediumToolStripMenuItem.Checked = false;
hardToolStripMenuItem.Checked = false;
pongmasterToolStripMenuItem.Checked = true;
cpuSkill = 6.5f;
}
void Form1_MouseMove(object sender, MouseEventArgs e)
{
//Moves the paddle to the mouse's y-coordinate
paddleHeight = e.Y - 40;
}
void t_Tick(object sender, EventArgs e)
{
//Causes ball to bounce of windows
if (ballX + 10 >= 439)
{
//directionX = false;
}
if (ballX - 10 <= 1)
directionX = true;
if (ballY + 10 >= 263)
directionY = false;
if (ballY - 10 <= 1)
directionY = true;
//movement of ball
if (directionX == true)
ballX += ballSpeed;
if (directionY == true)
ballY += ballSpeed;
if (directionX == false)
ballX -= ballSpeed;
if (directionY == false)
ballY -= ballSpeed;
//AI Vs player
if (directionY == true && ballX >= 225 && cpuHeight + 40 < ballY)
cpuHeight += cpuSkill;
if (directionY == false && ballX >= 225 && cpuHeight + 40 > ballY)
cpuHeight -= cpuSkill;
// Player vs paddle
if (directionX == false && ballY + 10 > paddleHeight && ballY - 10 < paddleHeight + 80 && ballX <= 20)
{
directionX = true;
hits += 1;
}
if (directionX == true && ballY + 10 > cpuHeight && ballY - 10 < cpuHeight + 80 && ballX >= 421)
{
directionX = false;
hits += 1;
}
if (((ballX - 10) <= 1) || ((ballX + 20) >= 450))
{
if( (ballX - 10)<=1)
++player2score;
if((ballX + 20) >= 450)
++player1score;
}
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
Pen p = new Pen(Color.Black);
SolidBrush b = new SolidBrush(Color.Orange);
SolidBrush drawBrush = new SolidBrush(Color.White);
if (ingame == true)
{
p.Width = 2;
g.DrawEllipse(p, ballX - 10, ballY - 10, 20, 20);
g.FillEllipse(b, ballX - 10, ballY - 10, 20, 20);
g.DrawRectangle(p, 5, paddleHeight, 6, 80);
g.DrawRectangle(p, 431, cpuHeight, 6, 80);
b.Color = Color.Blue;
g.FillRectangle(b, 431, cpuHeight, 6, 80);
g.FillRectangle(b, 5, paddleHeight, 6, 80);
g.DrawString(string.Format("{0} : {1}",player1score, player2score), new Font(FontFamily.GenericMonospace, 14),drawBrush,215, 10);
}
if (ingame == false)
g.DrawString(beginString, new Font(Font.FontFamily, 16), new SolidBrush(Color.Blue), new Point(140, 140), StringFormat.GenericDefault);
}
}
}
For that AI, I would suggest creating a random number to make the AI's paddle vary rather than being +-2. The AI's paddle is currently still "aligned" to the ball, its just offset by a number. Not really an AI when you are cloning the coordinates.
For the scoring system, make sure you added up the dimensions properly. It is very hard to make sense of all the different pixels plastered in the code. How big is the actual playing area?
精彩评论