-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scene.cs
158 lines (151 loc) · 4.36 KB
/
Scene.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BallCollision
{
public class Scene
{
public List<Ball> Balls { get; set; }
public Ball PlayerBall { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public readonly Random random = new Random();
public bool GameOver { get; set; }
public int Points { get; set; }
public int numBalls { get; set; }
public Scene(int width,int height)
{
Balls = new List<Ball>();
this.Width = width;
this.Height = height;
GameOver = false;
}
public void AddPlayerBall(Point location)
{
Ball ball = new Ball(location);
if(!DetectCollision(ball) && PlayerBall == null)
{
PlayerBall = ball;
PlayerBall.Radius = 16; // starting size
PlayerBall.Color = Color.Blue;
}
}
public void GenerateBalls()
{
int size = random.Next(20, 35);
for(int i=0;i<size;i++)
{
Point location = GenerateLocation();
Ball ball = new Ball(location);
if(!DetectCollision(ball))
Balls.Add(ball);
}
}
public Point GenerateLocation()
{
return new Point(random.Next(Ball.maxRadius * 2, Width - Ball.maxRadius * 2),
random.Next(Ball.maxRadius * 2, Height - Ball.maxRadius * 2));
}
public void DrawAll (Graphics graphics)
{
foreach(Ball ball in Balls)
{
ball.Draw(graphics);
}
if (PlayerBall == null)
return;
PlayerBall.Draw(graphics);
GameLogic();
}
public bool DetectCollision(Ball target)
{
foreach(Ball ball in Balls)
{
if(ball.CheckCollision(target))
{
return true;
}
}
return false;
}
public bool CheckClick(Point location)
{
foreach(Ball ball in Balls)
{
if (ball.IsClicked(location))
return true;
}
return false;
}
public void GameLogic()
{
foreach(Ball ball in Balls)
{
if(ball.CheckCollision(PlayerBall))
{
if(PlayerBall.Radius >= ball.Radius)
{
Points++;
PlayerBall.Radius += (int) (ball.Radius * 0.30);
ball.ToBeDeleted = true;
}
else
{
End();
}
}
}
for(int i=Balls.Count - 1; i >= 0; i--)
{
if (Balls[i].ToBeDeleted)
Balls.RemoveAt(i);
}
if(Balls.Count < 5)
{
GenerateBalls();
}
}
public void End()
{
PlayerBall.Center = Point.Empty;
PlayerBall.Radius = 0;
GameOver = true;
}
public void GenerateBall()
{
Ball ball = new Ball(GenerateLocation());
ball.Radius = 10;
if(!DetectCollision(ball))
Balls.Add(ball);
}
public void Shrink()
{
if (PlayerBall != null)
{
if (PlayerBall.Radius <= 0)
End();
PlayerBall.Shrink();
}
}
//MOVING
public void MoveLeft()
{
PlayerBall.Move(new Point(PlayerBall.Center.X - 5, PlayerBall.Center.Y));
}
public void MoveRight()
{
PlayerBall.Move(new Point(PlayerBall.Center.X + 5, PlayerBall.Center.Y));
}
public void MoveUp()
{
PlayerBall.Move(new Point(PlayerBall.Center.X, PlayerBall.Center.Y - 5));
}
public void MoveDown()
{
PlayerBall.Move(new Point(PlayerBall.Center.X, PlayerBall.Center.Y + 5));
}
}
}