-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlay.cs
223 lines (206 loc) · 7.99 KB
/
Play.cs
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using FaceRecognition;
namespace MYFACE
{
public partial class Play : Form
{
public Play()
{
InitializeComponent();
}
int score { get; set; }
int lives { get; set; }
private void reset()
{
lives = 4;
score = 0;
pictureBox_heart1.Visible = true;
pictureBox_heart2.Visible = true;
pictureBox_heart3.Visible = true;
pictureBox_heart4.Visible = true;
}
FaceRec_Location FaceRec_Location = new FaceRec_Location();
private void Play_Load(object sender, EventArgs e)
{
FaceRec_Location.openCamera(pictureBox1,false);
}
PictureBox basket = new PictureBox();
PictureBox pictureBox_basket_top_layer = new PictureBox();
private void loadBasket(PictureBox pictureBox_basket)
{
pictureBox_basket.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox_basket.BringToFront();
pictureBox_basket.Image = Properties.Resources.basket;
pictureBox_basket.Width = 75;
pictureBox_basket.Height = 40;
pictureBox1.Controls.Add(pictureBox_basket);
// Adding the basket to collect fruits
pictureBox1.Controls.Add(pictureBox_basket_top_layer);
pictureBox_basket_top_layer.BringToFront();
pictureBox_basket_top_layer.BackColor = Color.Silver;
pictureBox_basket_top_layer.Height = 5;
pictureBox_basket_top_layer.Width = 75;
}
private void basket_movement(PictureBox pictureBox_basket)
{
pictureBox_basket.Location = new Point(FaceRec_Location.getFaceLocation_X, FaceRec_Location.getFaceLocation_Y-60);
pictureBox_basket_top_layer.Location = new Point(FaceRec_Location.getFaceLocation_X, FaceRec_Location.getFaceLocation_Y - 60);
}
private void basket_collision(PictureBox pictureBox_fruit)
{
if (pictureBox_basket_top_layer.Bounds.IntersectsWith(pictureBox_fruit.Bounds)) // Fruit goes inside the basket
{
loadFruits(pictureBox_fruit);
score++;
label1.Text = "Score : " + score.ToString();
}
}
private void loadFruits(PictureBox pictureBox_fruit)
{
// Set the image properties
pictureBox_fruit.Width = 50;
pictureBox_fruit.Height = 50;
pictureBox_fruit.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox_fruit.BringToFront();
// Set location of fruits
Random randomX = new Random();
Random randomY = new Random();
int locationX = randomX.Next(0,320);
int locationY = randomY.Next(100,500);
pictureBox_fruit.Location = new Point(locationX, -locationY);
// Set random image
Random randomImage = new Random();
int image = randomX.Next(0, 7);
if(image == 0)
{
pictureBox_fruit.Image = Properties.Resources.apple;
}
if (image == 1)
{
pictureBox_fruit.Image = Properties.Resources.banana;
}
if (image == 2)
{
pictureBox_fruit.Image = Properties.Resources.cherry;
}
if (image == 3)
{
pictureBox_fruit.Image = Properties.Resources.grapes;
}
if (image == 4)
{
pictureBox_fruit.Image = Properties.Resources.orrange;
}
if (image == 5)
{
pictureBox_fruit.Image = Properties.Resources.strawberry;
}
if (image == 6)
{
pictureBox_fruit.Image = Properties.Resources.water_melon;
}
pictureBox1.Controls.Add(pictureBox_fruit);
pictureBox1.BackColor = Color.Transparent;
}
PictureBox fruits1 = new PictureBox();
PictureBox fruits2 = new PictureBox();
PictureBox fruits3 = new PictureBox();
PictureBox fruits4 = new PictureBox();
PictureBox fruits5 = new PictureBox();
int fruit_speed = 3;
private void timer_play_Tick(object sender, EventArgs e)
{
fruit_movement(fruits1);
fruit_movement(fruits2);
fruit_movement(fruits3);
fruit_movement(fruits4);
fruit_movement(fruits5);
fruit_collision();
basket_movement(basket);
basket_collision(fruits1);
basket_collision(fruits2);
basket_collision(fruits3);
basket_collision(fruits4);
basket_collision(fruits5);
gameOver();
}
private void fruit_movement(PictureBox pictureBox_fruit)
{
if (pictureBox_fruit.Top < 500)
{
pictureBox_fruit.Top += fruit_speed;
}
else
{
loadFruits(pictureBox_fruit);
lives--;
}
}
private void fruit_collision()
{
if (fruits1.Bounds.IntersectsWith(fruits2.Bounds) || fruits1.Bounds.IntersectsWith(fruits3.Bounds) || fruits1.Bounds.IntersectsWith(fruits4.Bounds) || fruits1.Bounds.IntersectsWith(fruits5.Bounds))
{
loadFruits(fruits1);
}
if (fruits2.Bounds.IntersectsWith(fruits1.Bounds) || fruits2.Bounds.IntersectsWith(fruits3.Bounds) || fruits2.Bounds.IntersectsWith(fruits4.Bounds) || fruits2.Bounds.IntersectsWith(fruits5.Bounds))
{
loadFruits(fruits2);
}
if (fruits3.Bounds.IntersectsWith(fruits1.Bounds) || fruits3.Bounds.IntersectsWith(fruits2.Bounds) || fruits3.Bounds.IntersectsWith(fruits4.Bounds) || fruits3.Bounds.IntersectsWith(fruits5.Bounds))
{
loadFruits(fruits3);
}
if (fruits4.Bounds.IntersectsWith(fruits1.Bounds) || fruits4.Bounds.IntersectsWith(fruits2.Bounds) || fruits4.Bounds.IntersectsWith(fruits3.Bounds) || fruits4.Bounds.IntersectsWith(fruits5.Bounds))
{
loadFruits(fruits4);
}
if (fruits5.Bounds.IntersectsWith(fruits1.Bounds) || fruits5.Bounds.IntersectsWith(fruits2.Bounds) || fruits5.Bounds.IntersectsWith(fruits3.Bounds) || fruits5.Bounds.IntersectsWith(fruits4.Bounds))
{
loadFruits(fruits5);
}
}
private void button_start_Click(object sender, EventArgs e)
{
reset();
loadFruits(fruits1);
loadFruits(fruits2);
loadFruits(fruits3);
loadFruits(fruits4);
loadFruits(fruits5);
loadBasket(basket);
timer_play.Enabled = true;
button_start.Visible = false;
}
private void gameOver()
{
if(lives == 3)
{
pictureBox_heart4.Visible = false;
}
if (lives == 2)
{
pictureBox_heart3.Visible = false;
}
if (lives == 1)
{
pictureBox_heart2.Visible = false;
}
if (lives == 0)
{
pictureBox_heart1.Visible = false;
timer_play.Enabled = false;
button_start.Visible = true;
MessageBox.Show(this, "Game Over", "Message",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
}
}