-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgameForm.cs
More file actions
207 lines (181 loc) · 7.1 KB
/
gameForm.cs
File metadata and controls
207 lines (181 loc) · 7.1 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
207
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 SnakeEvo
{
public partial class gameForm : Form
{
bool buttonPressed = false;
int score = 0;
bool gameover = true;
Timer gameLoop = new Timer();
Timer snakeLoop = new Timer();
Snake oneSnake = new Snake(10);
public gameForm()
{
InitializeComponent();
gameLoop.Tick += new EventHandler(Update);
snakeLoop.Tick += new EventHandler(UpdateSnake);
gameLoop.Interval = 1000 / 60;
snakeLoop.Interval = 1000/10;
gameLoop.Start();
snakeLoop.Start();
}
private void formGame_KeyDown(object sender, KeyEventArgs e)
{
Input.ChangeState(e.KeyCode, true);
}
private void formGame_KeyUp(object sender, KeyEventArgs e)
{
Input.ChangeState(e.KeyCode, false);
}
private void pbCanvas_Paint(object sender, PaintEventArgs e)
{
//Graphics canvas = e.Graphics;
Draw(e.Graphics);
}
private void StartGame()
{
gameover = false;
score = 0;
}
private void Update(object sender, EventArgs e)
{
if (gameover)
{
if (Input.Press(Keys.Enter))
{
StartGame();
}
}
else
{
if (Input.Press(Keys.Left))
{
if(oneSnake.vel.X == 0 && (oneSnake.vel.Y == 1 || oneSnake.vel.Y == -1))
{
oneSnake.vel.X = -1;
oneSnake.vel.Y = 0;
}
}
else if (Input.Press(Keys.Right))
{
if (oneSnake.vel.X == 0 && (oneSnake.vel.Y == 1 || oneSnake.vel.Y == -1))
{
oneSnake.vel.X = 1;
oneSnake.vel.Y = 0;
}
}
else if (Input.Press(Keys.Up))
{
if ((oneSnake.vel.X == 1 || oneSnake.vel.X == -1) && oneSnake.vel.Y == 0)
{
oneSnake.vel.X = 0;
oneSnake.vel.Y = -1;
}
}
else if (Input.Press(Keys.Down))
{
if ((oneSnake.vel.X == 1 || oneSnake.vel.X == -1) && oneSnake.vel.Y == 0)
{
oneSnake.vel.X = 0;
oneSnake.vel.Y = 1;
}
}
}
pbCanvas.Invalidate();
}
private void UpdateSnake(object sender, EventArgs e)
{
if (buttonPressed == true && gameover == false)
{
if (oneSnake.GoingToDie(oneSnake.head.X + oneSnake.vel.X, oneSnake.head.Y + oneSnake.vel.Y))
{
GameOver();
}
if ((oneSnake.head.X + oneSnake.vel.X) == oneSnake.food.X && (oneSnake.head.Y + oneSnake.vel.Y) == oneSnake.food.Y)
{
oneSnake.Eat();
score++;
scoreLabel.Text = "Score: " + score;
}
if (oneSnake.growCount > 0)
{
oneSnake.growCount--;
oneSnake.Grow();
}
else
{
int count = oneSnake.tail.Count;
for (int i = count - 1; i > 0; i--)
{
oneSnake.tail[i].X = oneSnake.tail[i - 1].X;
oneSnake.tail[i].Y = oneSnake.tail[i - 1].Y;
}
oneSnake.tail[0].X = oneSnake.head.X;
oneSnake.tail[0].Y = oneSnake.head.Y;
}
oneSnake.head.Add(oneSnake.vel);
}
}
private void Draw(Graphics canvas)
{
Font font = this.Font;
if (!buttonPressed)
{
SizeF message = canvas.MeasureString("Press Enter to Start a New Game", font);
canvas.DrawString("Press Enter to Start a New Game", font, Brushes.White, new PointF(200 - message.Width / 2, 160));
message = canvas.MeasureString("Snake EVO", font);
canvas.DrawString("Snake EVO", font, Brushes.White, new PointF(200 - message.Width / 2, 180));
message = canvas.MeasureString("Završni rad", font);
canvas.DrawString("Završni rad", font, Brushes.White, new PointF(200 - message.Width / 2, 200));
message = canvas.MeasureString("Student: Ivan Pešić", font);
canvas.DrawString("Student: Ivan Pešić", font, Brushes.White, new PointF(200 - message.Width / 2, 220));
}
else
{
if (gameover)
{
SizeF message = canvas.MeasureString("Game Over", font);
canvas.DrawString("Game Over", font, Brushes.White, new PointF(200 - message.Width / 2, 120));
message = canvas.MeasureString("Final Score " + score.ToString(), font);
canvas.DrawString("Final Score " + score.ToString(), font, Brushes.White, new PointF(200 - message.Width / 2, 140));
message = canvas.MeasureString("Press Enter to Start a New Game", font);
canvas.DrawString("Press Enter to Start a New Game", font, Brushes.White, new PointF(200 - message.Width / 2, 160));
}
else
{
canvas.FillRectangle(new SolidBrush(Color.Orange), new Rectangle(oneSnake.food.X * 8, oneSnake.food.Y * 8, 8, 8));
for (int i = 0; i < oneSnake.tail.Count; i++)
{
SnakePart currentPart = oneSnake.tail[i];
canvas.FillRectangle(new SolidBrush(Color.Black), new Rectangle(currentPart.X * 8, currentPart.Y * 8, 8, 8));
}
canvas.FillRectangle(new SolidBrush(Color.Red), new Rectangle(oneSnake.head.X * 8, oneSnake.head.Y * 8, 8, 8));
}
}
}
private void GameOver()
{
gameover = true;
gameStartButton.Enabled = true;
oneSnake = new Snake(100);
}
private void gameStartButton_Click(object sender, EventArgs e)
{
gameStartButton.Enabled = false;
Focus();
gameover = false;
buttonPressed = true;
oneSnake = new Snake(100);
StartGame();
scoreLabel.Text = "Score: " + score.ToString();
}
}
}