/
space.py
201 lines (159 loc) · 4.08 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
# Space Invaders
# set up the screen
import turtle
import os
import math
import random
# set up the screen
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("vinod Space War")
wn.bgpic("Space.gif")
#registers the shape
turtle.register_shape("enemy.gif")
turtle.register_shape("shooter.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 a player for turtle
player = turtle.Turtle()
player.color("blue")
player.shape("triangle")
player.penup()
player.speed(0)
player.setposition(0,-250)
player.setheading(90)
playerspeed = 15
#Choose the number of enemy
number_of_enemies = 5
#create an empty list of enemy list
enemies =[]
#Add enemies in the list
for i in range(number_of_enemies):
enemies.append(turtle.Turtle())
for enemy in enemies:
#create the enemy
enemy.color("red")
enemy.shape("circle")
enemy.penup()
enemy.speed(0)
x = random.randint(-200,200)
y = random.randint(100,250)
enemy.setposition(x,y)
enemyspeed = 2
#Create a 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 to 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():
global bulletstate
if bulletstate == "ready":
bulletstate = "fire"
#move teh bullet
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
#ccreate key board listen
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:
for e in enemies:
y = e.ycor()
y -= 40
e.sety(y)
enemyspeed *= -1
if enemy.xcor() < -280:
for e in enemies:
y = e.ycor()
y -= 40
e.sety(y)
enemyspeed *= -1
if isCollision(bullet,enemy):
#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"))
if isCollision(player,enemy):
player.hideturtle()
enemy.hideturtle()
print("Game over")
break
#Move the bullet
if bulletstate == "fire":
y = bullet.ycor()
y += bulletspeed
bullet.sety(y)
#check if the bullet gone to top
if bullet.ycor() > 275:
bullet.hideturtle()
bulletstate = "ready"
delay = input("press enter to finish")