blob: cde3c313c2e93e343a5e4703460bb5835d67dc0b (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ShiftOS.Main.Apps
{
public partial class Pong : UserControl
{
bool goUp = false;
bool goDown = false;
int speed = 5;
float ballX = 5;
float ballY = 5;
public Pong()
{
InitializeComponent();
gameTimer.Start();
ResetToRest();
}
void ResetToRest()
{
ball.Location = new Point((int)Math.Round(this.Width / 2d, 0), (int)Math.Round(this.Height / 2d, 0));
playerPaddle.Location = new Point(playerPaddle.Location.X, (int)Math.Round(this.Height / 2d, 0));
cpuPaddle.Location = new Point(cpuPaddle.Location.X, (int)Math.Round(this.Height / 2d, 0));
}
private void Pong_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W) goUp = true;
if (e.KeyCode == Keys.S) goDown = true;
}
private void Pong_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W) goUp = false;
if (e.KeyCode == Keys.S) goDown = false;
}
private void gameTimer_Tick(object sender, EventArgs e)
{
ball.Top -= (int)ballY;
ball.Left -= (int)ballX;
//CPU
if (ballX < 0)
{
if (ball.Top < cpuPaddle.Top + 51)
{
cpuPaddle.Top -= 5;
}
if(ball.Top > cpuPaddle.Top + 51)
{
cpuPaddle.Top += 5;
}
}
if (ball.Left < 0)
{
ResetToRest();
ballX = -ballX;
}
if (ball.Left + ball.Width > this.Width)
{
ResetToRest();
ballX = -ballX;
}
if (ball.Top < 0 || ball.Top + ball.Height > this.Height) ballY = -ballY;
if (ball.Bounds.IntersectsWith(playerPaddle.Bounds) || ball.Bounds.IntersectsWith(cpuPaddle.Bounds)) ballX = -ballX;
if (goUp && playerPaddle.Top > 0) playerPaddle.Top -= 8;
if (goDown && playerPaddle.Top < this.Height) playerPaddle.Top += 8;
}
}
}
|