forked from Rkhajuriwala/Space-Warrior
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spacewar.py
223 lines (184 loc) · 5.5 KB
/
spacewar.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
213
214
215
216
217
218
219
220
221
222
223
import os
import random
import turtle
import time
# turtle.fd(0)
turtle.speed(0)
turtle.bgcolor("black")
turtle.title("Space warrior for Stroke Rehab")
turtle.bgpic("starfield.gif")
turtle.ht()
turtle.setundobuffer(1)
turtle.tracer(0)# turtle drawing speed
class Sprite(turtle.Turtle):
def __init__(self,spriteshape,color,startx,starty):
turtle.Turtle.__init__(self,shape = spriteshape)
self.speed(0) #speed of animation
self.penup()
self.color(color)
self.fd(0)
self.goto(startx,starty)
self.speed = 1
def move(self):
self.fd(self.speed)
if self.xcor() >390:
self.setx(390)
self.rt(60)
if self.xcor() <-390:
self.setx(-390)
self.rt(60)
if self.ycor() >390:
self.sety(390)
self.rt(60)
if self.ycor() <-390:
self.sety(-390)
self.rt(60)
def collision(self,other):
if (self.xcor() >=(other.xcor()-20)) and \
(self.xcor() <=(other.xcor()+20)) and \
(self.ycor() >=(other.ycor()-20)) and \
(self.ycor() <=(other.ycor()+20)):
return True
else:
return False
class Player(Sprite):
def __init__(self,spriteshape,color,startx,starty):
Sprite.__init__(self,spriteshape,color,startx,starty)
self.shapesize(stretch_wid=1.1,stretch_len = 2.1, outline = None)
self.speed = 4
self.lives = 10#change accordingly
def move_left(self):
self.lt(45)
def move_right(self):
self.rt(45)
def speed_up(self):
self.speed+=1
def speed_down(self):
self.speed-=1
class Enemy(Sprite):
def __init__(self,spriteshape,color,startx,starty):
Sprite.__init__(self,spriteshape,color,startx,starty)
self.speed = 6
self.setheading(random.randint(0,360))
class Friend(Sprite):
def __init__(self,spriteshape,color,startx,starty):
Sprite.__init__(self,spriteshape,color,startx,starty)
self.speed = 10
self.setheading(random.randint(0,360))
def move(self):
self.fd(self.speed)
if self.xcor() >390:
self.setx(390)
self.lt(60)
if self.xcor() <-390:
self.setx(-390)
self.lt(60)
if self.ycor() >390:
self.sety(390)
self.lt(60)
if self.ycor() <-390:
self.sety(-390)
self.lt(60)
class Missile(Sprite):
def __init__(self,spriteshape,color,startx,starty):
Sprite.__init__(self,spriteshape,color,startx,starty)
self.shapesize(stretch_wid = 0.2,stretch_len = 0.4,outline= None)
self.speed = 20
self.status = "ready"
self.goto(-1000,1000)
def fire(self):
if self.status == "ready":#self.goto(-1000,1000)
self.goto(player.xcor(),player.ycor())
self.setheading(player.heading())
self.status ="shoot"
def move(self):
if self.status == "ready":
self.goto(-1000,1000)
if self.status == "shoot":
self.fd(self.speed)
if self.xcor()>390 or \
self.xcor()<-390 or \
self.ycor()>390 or \
self.ycor()<-390:
self.goto(-1000,1000)
self.status = "ready"
class Game():
def __init__(self):
self.level = 1
self.score = 0
self.state = "playing"
self.pen = turtle.Turtle()
self.lives = 100
def border(self):
self.pen.speed(0)
self.pen.color("white")
self.pen.pensize(3)
self.pen.penup()
self.pen.goto(-400,400)
self.pen.pendown()
for side in range(4):
self.pen.fd(800)
self.pen.rt(90)
self.pen.penup()
self.pen.ht()
self.pen.pendown()
def status(self):
self.pen.undo()
message = "Score: %s" %(self.score)
self.pen.penup()
self.pen.goto(-400,410)
self.pen.write(message,font=("Arial",15,"normal"))
game = Game()
game.border()
game.status()
#sprite
player = Player("triangle","white",0,0)
#enemy = Enemy("circle","red",-100,0)
missile = Missile("triangle","yellow",0,0)
#friend = Friend("square","blue",0,0)
enemies=[]
for i in range(3):
enemies.append(Enemy("circle","red",-100,0))
friends = []
for i in range(3):
friends.append(Friend("square","blue",100,0))
#KEYBOARD
turtle.listen()
turtle.onkey(player.move_left,"Left")
turtle.onkey(player.move_right,"Right")
turtle.onkey(player.speed_up,"Up")
turtle.onkey(player.speed_down,"Down")
turtle.onkey(missile.fire,"space")
while True:
turtle.update()
time.sleep(0.02)
player.move()
#enemy.move()
missile.move()
#friend.move()
#player.move_left()
for enemy in enemies:
enemy.move()
if player.collision(enemy):
x = random.randint(-350,350)
y = random.randint(-350,350)
enemy.goto(x,y)
game.score -=100
game.status()
if missile.collision(enemy):
x = random.randint(-350,350)
y = random.randint(-350,350)
enemy.goto(x,y)
missile.status ="ready"
game.score+=100
game.status()
for friend in friends:
friend.move()
if missile.collision(friend):
x = random.randint(-350,350)
y = random.randint(-350,350)
friend.goto(x,y)
missile.status ="ready"
game.score-=50
game.status()
delay = raw_input("press enter")