-
Notifications
You must be signed in to change notification settings - Fork 0
/
Space Invaders.py
185 lines (154 loc) · 4.11 KB
/
Space Invaders.py
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
import turtle
from turtle import Turtle, Screen
import math
import random
import winsound
import time
# Define Globals
player = Turtle()
wn = Screen()
score = 0
scorestring = 'Score: %s' %score
# Register Graphics
turtle.register_shape('enemy.gif')
turtle.register_shape('player.gif')
wn.setup(700, 700)
wn.title('Space Invaders')
wn.bgcolor('black')
wn.bgpic('background.gif')
bp = Turtle()
bp.speed(10)
bp.color('white')
bp.penup()
bp.setposition(-300, -300)
bp.pendown()
bp.pensize(3)
for side in range(4):
bp.fd(600)
bp.left(90)
bp.hideturtle()
en = 10
enemies = []
for i in range(en):
enemies.append(Turtle())
player.color('blue')
player.speed(10)
player.shape('player.gif')
player.penup()
player.setposition(0, -250)
player.setheading(90)
for enemy in enemies:
enemy.color('red')
enemy.shape('enemy.gif')
enemy.penup()
enemy.speed(10)
x = random.randint(-200, 200)
y = random.randint(100, 250)
enemyspeed = random.randint(2, 10)
enemy.setposition(x, y)
playerspeed = 15
# Create Player Weapon
bullet = Turtle()
bullet.color('yellow')
bullet.shape('triangle')
bullet.penup()
bullet.speed(10)
bullet.setheading(90)
bullet.shapesize(0.5, 0.5)
bullet.hideturtle()
bulletspeed = 20
# Define Bullet States
# ready - ready to fire
# fire - bullet is firing
bulletstate = 'ready'
# Scoring
sp = Turtle()
sp.speed(0)
sp.color('white')
sp.penup()
sp.setposition(-290, 280)
sp.write(scorestring, False, align='left', font=('Arial', 14, 'normal'))
sp.hideturtle()
# Move the player
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():
global bulletstate
if bulletstate == 'ready':
bulletstate = 'fire'
x = player.xcor()
y = player.ycor()
bullet.setposition(x, y +10)
bullet.showturtle()
winsound.PlaySound('shoot.wav', winsound.SND_ASYNC)
def isCollision(t1, t2):
distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2))+math.pow(t1.ycor()-t2.ycor(),2)
if distance < 65:
return True
else:
return False
wn.listen()
wn.onkey(move_left, 'Left')
wn.onkey(move_right, 'Right')
wn.onkey(fire, 'space')
# Main Game Loop
while True:
# Moving the enemy
for enemy in enemies:
x = enemy.xcor()
x += enemyspeed
enemy.setx(x)
if enemy.xcor() > 280:
for e in enemies:
y = e.ycor()
y -= 10
e.sety(y)
winsound.PlaySound('cartoon060.wav', winsound.SND_ASYNC)
enemyspeed *= -1
if enemy.xcor() < -280:
for e in enemies:
y = e.ycor()
y -= 10
e.sety(y)
winsound.PlaySound('cartoon060.wav', winsound.SND_ASYNC)
enemyspeed *= -1
# Bullet and enemy collision
if isCollision(bullet, enemy):
bullet.hideturtle()
bulletstate = 'ready'
bullet.setposition(0, -400)
enemy.hideturtle()
score = score + 10
scorestring = 'Score: %s' % score
sp.clear()
sp.write(scorestring, False, align='left', font=('Arial', 14, 'normal'))
winsound.PlaySound('invaderkilled.wav', winsound.SND_ASYNC)
# Player and enemy collision
if isCollision(enemy, player):
player.hideturtle()
enemy.hideturtle()
score = score - score
print('Game Over!')
winsound.PlaySound('explosion.wav', winsound.SND_ASYNC)
time.sleep(3)
exit()
# Moving the bullet
if bulletstate == 'fire':
y = bullet.ycor()
y += bulletspeed
bullet.sety(y)
# Bullet Border Checking
if bullet.ycor() > 300:
bullet.hideturtle()
bulletstate = 'ready'
wn.mainloop()