-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspace_invader.py
More file actions
195 lines (161 loc) · 4.3 KB
/
space_invader.py
File metadata and controls
195 lines (161 loc) · 4.3 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
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
import turtle
import os
import math
import random
#Set up the screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Space Invaders")
#register the shapes
turtle.register_shape("invaders.gif")
#Draw border
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300,-300)
border_pen.pendown()
border_pen.pensize(3)
for side in range(4):
border_pen.fd(600)
border_pen.lt(90)
border_pen.hideturtle()
#set the score to 0
score = 0
#draw the score
score_pen = turtle.Turtle()
score_pen.speed(0)
score_pen.color("white")
score_pen.penup()
score_pen.setposition(-290,280)
scorestring = "Score: %s" %score
score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
score_pen.hideturtle()
#Create the player turtle
player = turtle.Turtle()
player.color("blue")
player.shape("triangle")
player.penup()
player.speed(0)
player.setposition(0, -280)
player.setheading(90)
playerspeed = 15
#create the enemy
#choose a number of enemies
number_of_enemies = 5
#create an empty list of enemies
enemies = []
#add enemies to the list
for i in range(number_of_enemies):
enemies.append(turtle.Turtle())
enemy = turtle.Turtle()
for enemy in enemies:
enemy.color("red")
enemy.shape("invaders.gif")
enemy.penup()
enemy.speed(0)
x = random.randint(-200, 200)
y = random.randint(100, 150)
enemy.setposition(x, y)
enemyspeed = 2
#Create the player's bullet
bullet = turtle.Turtle()
bullet.color("yellow")
bullet.shape("triangle")
bullet.penup()
bullet.speed(0)
bullet.setheading(90)
bullet.shapesize(0.5,0.5)
bulletspeed = 20
#define bullet state
#ready - ready to fire
#fire - bullet is firing
bulletstate = "ready"
#move the player left and right
def move_left():
x = player.xcor()
x -= playerspeed
if x < -280:
x = -280
player.setx(x)
def move_right():
x = player.xcor()
x+=playerspeed
if x > 280:
x = 280
player.setx(x)
def fire_bullet():
#declare bulletstate as a global if it needs change
global bulletstate
#move the bullet just above the player
if bulletstate == "ready":
bulletstate = "fire"
x = player.xcor()
y = player.ycor() + 10
bullet.setposition(x,y)
bullet.showturtle()
def isCollision(t1, t2):
distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2) + math.pow(t1.ycor()-t2.ycor(),2))
if distance < 15:
return True
else:
return False
#Create keyboard bindings
turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(fire_bullet, "space")
#Main game loop
while True:
for enemy in enemies:
#Move the enemy
x = enemy.xcor()
x += enemyspeed
enemy.setx(x)
#Move the enemy back and down
if enemy.xcor() > 280:
#move all enemies down
for e in enemies:
y = e.ycor()
y -= 40
e.sety(y)
#change the direction
enemyspeed *= -1
if enemy.xcor() < -280:
#move all enemies down
for e in enemies:
y = e.ycor()
y -= 40
e.sety(y)
#change the direction
enemyspeed *= -1
#check the collision between the bullet and the enemy
if isCollision(bullet, enemy):
#reset the bullet
bullet.hideturtle()
bulletstate = "ready"
bullet.setposition(0, -400)
#reset the enemy
enemy.setposition(-200, 250)
x = random.randint(-200, 200)
y = random.randint(100, 150)
enemy.setposition(x, y)
score += 10
scorestring = "Score: %s" %score
score_pen.clear()
score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
if isCollision(player, enemy):
player.hideturtle()
enemy.hideturtle()
print("Game Over")
break
#move the bulllet
if bulletstate == "fire":
y = bullet.ycor()
y += bulletspeed
bullet.sety(y)
#check to see if the bullet has gone to the top
if bullet.ycor() > 275:
bullet.hideturtle()
bulletstate = "ready"
delay = input("Press enter to finish")