-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspace.py
177 lines (141 loc) · 3.49 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
import turtle
import os
import math
import random
#initialize the screen
wn = turtle.Screen()
wn.bgpic("space_invaders_background.gif")
turtle.register_shape("invader.gif")
turtle.register_shape("player.gif")
#background colour
wn.bgcolor('black')
#title
wn.title('space invaders')
#draw border
borderPen = turtle.Turtle()
#speed should be fast
borderPen.speed(0)
#colour
borderPen.color('white')
#we dont want to draw anything
borderPen.penup()
borderPen.setposition(-300,-300)
#now we want to draw
borderPen.pendown()
#size of the pen
borderPen.pensize(3) #width of the square
#loop to draw square
for size in range(4):
borderPen.fd(600) #fd - forward
borderPen.lt(90) #lt - left
#now to hide the turtkle
borderPen.hideturtle()
#creating player
player = turtle.Turtle()
player.color('blue')
player.shape('player.gif')
player.penup() #we dont want our player(turtle) to draw anythings as it moves
player.speed(0)
player.setposition(0,-250) #keeping it at coordinates(0,-250)
player.setheading(90) #rotate the triangle by 90 degrees towards the left
playerspeed = 15
#creating enemies
#create multiple enemies
numberOfEnemies = 5
#add enemies to the list
enemies = []
#add enemies to the list
for i in range(numberOfEnemies):
#create a new 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,150)
enemy.setposition(x,y)
enemyspeed = 2
#create bullets
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
bulletstate = 'ready'
#functions
def moveLeft():
x = player.xcor()
x -= playerspeed
if x<-280:
x = -280
player.setx(x)
def moveRight():
x = player.xcor()
x += playerspeed
if x>280:
x = 280
player.setx(x)
def fireBullet():
global bulletstate
if bulletstate == 'ready':
bulletstate = 'fired'
#move bullet upwards from where it is fired
x = player.xcor()
y = player.ycor()
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
#keyboard bindings
turtle.listen()
turtle.onkey(fireBullet,'space')
turtle.onkeypress(moveLeft,'Left') #here we have used another function
turtle.onkeypress(moveRight,'Right')
while True:
for enemy in enemies:
#moving the enemy
x=enemy.xcor()
x+=enemyspeed
enemy.setx(x)
#moving the enemy down and change direction
if enemy.xcor() > 280:
y=enemy.ycor()
y-=30
enemyspeed *= -1
enemy.sety(y)
if enemy.xcor() < -280:
y=enemy.ycor()
y-=30
enemyspeed *= -1
enemy.sety(y)
#detect collisions b/w bullet and enemies
if iscollision(bullet,enemy):
#reseting the bullet
bullet.hideturtle()
bulletstate = 'ready'
bullet.setposition(0,-400)
#resetting the enemy
enemy.setposition(-200,250)
if iscollision(player,enemy):
player.hideturtle()
print('Game over')
break
#moving bullet upwards
if bulletstate == 'fired':
y = bullet.ycor()
y+=bulletspeed
bullet.sety(y)
#if bullet reaches top
if bullet.ycor()>275:
bullet.hideturtle()
bulletstate = 'ready'