-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cs
78 lines (72 loc) · 2.19 KB
/
Player.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
namespace seainvaders
{
class Player : Entity
{
public char[,] graphic =
{
{ '.','.','.','.','.','.','0','.','.','.','.','.','.'},
{ '.','.','.','.','.','0','0','0','.','.','.','.','.'},
{ '.','.','.','.','.','0','0','0','.','.','.','.','.'},
{ '.','0','0','0','0','0','0','0','0','0','0','0','.'},
{ '0','0','0','0','0','0','0','0','0','0','0','0','0'},
{ '0','0','0','0','0','0','0','0','0','0','0','0','0'},
{ '0','0','0','0','0','0','0','0','0','0','0','0','0'},
{ '0','0','0','0','0','0','0','0','0','0','0','0','0'},
};
public int vx;
public Bullet bullet;
public int lives = 3;
public Player() : base()
{
vx = 0;
y = 211;
x = 105;
SetGraphic(graphic);
}
public override void Update()
{
if (Game.keys.Contains("left") && x > 0)
{
vx = -2;
}
else if (Game.keys.Contains("right") && x < 223)
{
vx = 2;
}
else
{
vx = 0;
}
x += vx;
// movement logic
if (Game.keys.Contains("space"))
{
if (bullet == null)
{
bullet = new Bullet();
bullet.x = x + 6; // to align to centre of player
bullet.y = y - 4; // because we want the top of the bullet at the 'start' of the player
bullet.Deleted += Bullet_Deleted; //Bullet_Deleted is an event listener
}
}
// shooting logic
}
private void Bullet_Deleted(object sender, EventArgs e)
{
bullet = null; //this deletes the bullet
}
public void Hit()
{
if (--lives <= 0)
{
Delete();
}
}
}
}