-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm2.cs
More file actions
206 lines (166 loc) · 6.07 KB
/
Form2.cs
File metadata and controls
206 lines (166 loc) · 6.07 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace PongProject
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
btnStart.Text = "Start";
}
//Old Code
/*private bool up = false;
private bool left = false; */
//variable decleration
private int score = 0;
private int XCord = 5, YCord = 5;
//Main game
private void tmrGame_Tick_1(object sender, EventArgs e)
{
int x = ovsBall.Location.X;
int y = ovsBall.Location.Y;
#region OldCode
//move ovsBall
/*if (ovsBall.Location.Y + ovsBall.Size.Height < playGround.Size.Height && !up)
{ {
y += 20;
up = false;
}
}
//border abouve
else if (ovsBall.Location.Y > 0 && up)
{
y -= 20;
up = true;
}
else
{
up = !up;
}
//border right
if (ovsBall.Location.X + ovsBall.Size.Width < playGround.Size.Width && !left)
{
x += 20;
left = false;
}
//else if (ovsBall.Location.X > 0 && left)
//{
// x -= 20;
//left = true;
//}
else
{
left = !left;
}
//pctRacket
if (ovsBall.Location.X + ovsBall.Size.Width >= pctRacket.Location.X)
{
if (ovsBall.Location.Y + (ovsBall.Size.Height / 2) > pctRacket.Location.Y + pctRacket.Size.Height)
{
tmrGame.Stop();
Form newForm = new Form3();
newForm.Show(this);
}
else if (ovsBall.Location.Y + (ovsBall.Size.Height / 2) < pctRacket.Location.Y)
{
tmrGame.Stop();
Form newForm = new Form3();
newForm.Show(this);
}
else
{
x -= 20;
left = true;
}
}*/
#endregion
if(x + ovsBall.Size.Width > pctRacket.Location.X + pctRacket.Size.Width)
{
//Stop Game, Highscore (Form3) opens
tmrGame.Stop();
Form newForm= new Form3();
newForm.Show(this);
return;
}
if(y < 0 || y + ovsBall.Size.Height > playGround.Size.Height)
{
YCord *= -1;
}
//Define Racket
if (x < 0 || ( x + ovsBall.Size.Width >= pctRacket.Location.X &&
ovsBall.Location.Y + ovsBall.Size.Height/2 > pctRacket.Location.Y &&
ovsBall.Location.Y + ovsBall.Size.Height/2 < pctRacket.Location.Y + pctRacket.Size.Height))
{
XCord *= -1;
//Give points +10 every time the racket gets the ball
if (x + ovsBall.Size.Width >= pctRacket.Location.X &&
ovsBall.Location.Y + ovsBall.Size.Height / 2 > pctRacket.Location.Y &&
ovsBall.Location.Y + ovsBall.Size.Height / 2 < pctRacket.Location.Y + pctRacket.Size.Height)
{
score += 10;
this.lblPoints.Text = score.ToString();
}
}
//new ovsBall position
ovsBall.Location = new Point(x + XCord, y + YCord);
}
//Pause game
private void btnStart_Click(object sender, EventArgs e)
{
if (btnStart.Text == "Start")
{
tmrGame.Start();
btnStart.Text = "Pause";
}
else
{
tmrGame.Stop();
btnStart.Text = "Start";
}
}
//New game
private void btnNew_Click(object sender, EventArgs e)
{
ovsBall.Location = new Point(0, 0); //Ball restart in the left corner
btnStart.Focus();
lblPoints.Text = score.ToString(); //Reset points
}
//Ball, change direction with the arrow keys
private void btnUp_MouseClick(object sender, MouseEventArgs e)
{
ovsBall.Location = new Point(ovsBall.Location.X, ovsBall.Location.Y - 10);
}
private void btnLeft_MouseClick(object sender, MouseEventArgs e)
{
ovsBall.Location = new Point(ovsBall.Location.X - 10, Location.Y);
}
private void btnDown_MouseClick(object sender, MouseEventArgs e)
{
ovsBall.Location = new Point(ovsBall.Location.X, ovsBall.Location.Y + 10);
}
private void btnRight_MouseClick(object sender, MouseEventArgs e)
{
ovsBall.Location = new Point(ovsBall.Location.X + 10, ovsBall.Location.Y);
}
//Racket; change direction with w & s
private void Form2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 'w' && pctRacket.Location.Y > 0)
{
pctRacket.Location = new Point(pctRacket.Location.X, pctRacket.Location.Y - 25);
}
else if (e.KeyChar == 's' && pctRacket.Location.Y + pctRacket.Size.Height + 1 < playGround.Size.Height)
{
pctRacket.Location = new Point(pctRacket.Location.X, pctRacket.Location.Y + 25);
}
}
}
}