-
Notifications
You must be signed in to change notification settings - Fork 10
/
space.py
212 lines (181 loc) · 5.5 KB
/
space.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Space Invader by Mahesh Sawant
import turtle
import winsound
import os
import math
import random
# Set up the screen
wn = turtle.Screen()
wn.bgcolor("blue")
wn.title("Space Invaders by Mahesh Sawant")
wn.bgpic("background.gif")
# Register the shape
turtle.register_shape("invader.gif")
turtle.register_shape("player.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 pen
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("player.gif")
player.penup()
player.speed(0)
player.setposition(0,-250)
player.setheading(90)
playerspeed = 15
# Choose a number of enemies
number_of_enemies = 5
# Creat an empty list of enemies
enemies = []
# Add enemies to the list
for i in range(number_of_enemies):
# create the enemy
enemies.append(turtle.Turtle())
for enemy in enemies:
#enemy.color("Red")
enemy.shape("invader.gif")
enemy.penup()
enemy.speed(0)
x = random.randint(-200, 200)
y = random.randint(100, 250)
enemy.setposition(x, y)
enemyspeed = 2
# Creat 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)
bullet.hideturtle()
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 changed
global bulletstate
if bulletstate == "ready":
bulletstate = "fire"
# Move the bullet to the just above the player
winsound.PlaySound("laser.wav", winsound.SND_ASYNC) #sound for windows
#os.system("aplay laser.wav&") #sound for linux
x = player.xcor()
y = player.ycor() + 10
bullet.setposition(x,y)
bullet.showturtle()
# For collision between enemy and bullet
def isCollision_enemy_bullet(t1, t2):
distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
if distance < 25:
return True
else:
return False
# For collision between enemy and player
def isCollision_enemy_player(t1, t2):
distance = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
if distance < 40:
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() > 270:
# Move all enemies down
for e in enemies:
y = e.ycor()
y -= 40
e.sety(y)
# Change enemy direction
enemyspeed *= -1
if enemy.xcor() < -270:
# Move all enemies down
for e in enemies:
y = e.ycor()
y -= 40
e.sety(y)
# Change enemy direction
enemyspeed *= -1
# check for a collision between the bullet and the enemy
if isCollision_enemy_bullet(bullet, enemy):
winsound.PlaySound("explosion-e+b.wav", winsound.SND_ASYNC) #sound for windows
#os.system("aplay explosion-e+b.wav&") # sound for linux
# Reset the bullet
bullet.hideturtle()
bulletstate = "ready"
bullet.setposition(0, -400)
# Reset the enemy
x = random.randint(-200, 200)
y = random.randint(100, 250)
enemy.setposition(x, y)
# update the score
score += 10
scorestring = "Score: %s" %score
score_pen.clear()
score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
# check for a collision between the player and enemy
if isCollision_enemy_player(player, enemy):
#winsound.PlaySound("explosion-e+p.wav", winsound.SND_ASYNC) #sound for windows
# os.system("aplay explosion-e+p.wav&") # sound for linux
player.hideturtle()
for e in enemies:
e.hideturtle()
wn.bgpic("end.gif")
break
# Move the bullet
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")