-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
127 lines (124 loc) · 4.08 KB
/
Game.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//Import SFML
using SFML.Audio;
using SFML.Graphics;
using SFML.Window;
namespace _4HJ1
{
class Game
{
//Declare Variables and Assets
public static RenderWindow window;
public static int MouseX;
public static int MouseY;
public static List<Point> points = new List<Point>();
public int W, H;
public static Sprite pointSprite;
public static int Score;
public static Sprite BG;
public static Font F;
public static int spawnTimeout = 60;
public static Music m;
public static bool lose;
public Game(uint W, uint H, string Title)
{
window = new RenderWindow(new VideoMode(W,H), Title);
this.W = (int)W;
this.H = (int)H;
}
public void Start()
{
//Load Assets and Set Variables
lose = false;
m = new Music("Content/mixdown.ogg");
m.Loop = true;
m.Play();
F = new Font("Content/Animated.ttf");
BG = new Sprite(new Texture(new Image("Content/BG.png")), new IntRect(0, 0, W, H));
pointSprite = new Sprite(new Texture(new Image("Content/Point.png")), new IntRect(0,0,16,16));
window.MouseMoved += MouseInput;
window.KeyPressed += KeyboardInput;
window.KeyReleased += KeyboardReleaseInput;
Player.Load();
while(window.IsOpen())
{
window.DispatchEvents();
window.Clear(new Color(0,0,0));
Draw();
window.Display();
}
}
public void Draw()
{
if(lose == true)
{
Score = 0;
Point.Speed = .01f;
}
Point.Speed += 0.0001f;
//Spawn Random Runner
if (spawnTimeout > 1)
{
spawnTimeout--;
}
else
{
spawnTimeout = 100;
points.Add(new Point(0, new Random(DateTime.Now.Millisecond).Next(0, 600)));
}
window.Draw(BG);
for (int l = points.Count - 2; l >= 0; l--)
{
Point C = points[l];
if (C.X + 14 > Player.X && C.X < Player.X + 64 && C.Y + 14 > Player.Y && C.Y < Player.Y + 64)
{
points.Remove(points[l]);
Score = Score + 1;
}
if(C.X > W)
{
points.Remove(points[l]);
}
if(C.X > 725)
{
lose = true;
}
points[l].updatePosition();
pointSprite.Position = new Vector2f((int)points[l].X,(int)points[l].Y);
window.Draw(pointSprite);
}
Player.updatePosition();
window.Draw(Player.sprite);
window.Draw(new Text("Score: " + Score, F));
}
public void MouseInput(Object sender, MouseMoveEventArgs e)
{
MouseX = e.X;
MouseY = e.Y;
}
public void KeyboardInput(Object sender, KeyEventArgs e)
{
switch(e.Code)
{
case Keyboard.Key.W: Player.verticalSpeed = -1; break;
case Keyboard.Key.S: Player.verticalSpeed = +1; break;
case Keyboard.Key.A: Player.horizontalSpeed = -1; break;
case Keyboard.Key.D: Player.horizontalSpeed = +1; break;
}
}
public void KeyboardReleaseInput(Object sender, KeyEventArgs e)
{
switch(e.Code)
{
case Keyboard.Key.W: Player.verticalSpeed = 0; break;
case Keyboard.Key.S: Player.verticalSpeed = 0; break;
case Keyboard.Key.A: Player.horizontalSpeed = 0; break;
case Keyboard.Key.D: Player.horizontalSpeed = 0; break;
}
}
}
}