-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathForm1.cs
More file actions
131 lines (112 loc) · 3.39 KB
/
Form1.cs
File metadata and controls
131 lines (112 loc) · 3.39 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SpaceInvaders
{
public partial class Form1 : Form
{
private Game game;
private List<Keys> keysPressed = new List<Keys>();
private bool gameOver = true;
private int frame = 0;
private int cell = 0;
public Form1()
{
InitializeComponent();
Constructor();
}
private void Constructor()
{
animationTimer.Interval = 100;
animationTimer.Enabled = true;
gameTimer.Enabled = false;
gameTimer.Interval = 50;
game = new Game(this);
game.GameOver += new EventHandler<GameOverArgs>(game_GameOver);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
frame++;
if (frame >= 6)
frame = 0;
switch (frame)
{
case 0: cell = 0; break;
case 1: cell = 1; break;
case 2: cell = 2; break;
case 3: cell = 3; break;
case 4: cell = 2; break;
case 5: cell = 1; break;
default: cell = 0; break;
}
game.Draw(e.Graphics, cell);
}
private void animationTimer_Tick(object sender, EventArgs e)
{
game.Twinkle();
this.Refresh();
}
private void gameTimer_Tick(object sender, EventArgs e)
{
game.Go();
game.GameState();
foreach (Keys key in keysPressed)
{
if (key == Keys.Left)
{
game.MovePlayer(Direction.Left);
return;
}
else if (key == Keys.Right)
{
game.MovePlayer(Direction.Right);
return;
}
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Q)
Application.Exit();
if (gameOver)
if (e.KeyCode == Keys.S)
{
gameTimer.Enabled = true;
animationTimer.Enabled = true;
gameOver = false;
}
else if (e.KeyCode == Keys.R)
{
Constructor();
}
else
return;
if (e.KeyCode == Keys.Space)
game.FireShot();
if (keysPressed.Contains(e.KeyCode))
keysPressed.Remove(e.KeyCode);
keysPressed.Add(e.KeyCode);
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (keysPressed.Contains(e.KeyCode))
keysPressed.Remove(e.KeyCode);
}
void game_GameOver(object sender, GameOverArgs e)
{
GameOverArgs args = e as GameOverArgs;
this.gameOver = args.gameOver;
if (gameOver == true)
gameTimer.Enabled = false;
}
private void Form1_Resize(object sender, EventArgs e)
{
game = new Game(this);
}
}
}