-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_no-sound.py
145 lines (145 loc) · 2.85 KB
/
game_no-sound.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
import turtle
import os
import math
import random
global ending
ending = "playing"
nofenm = int(input("select how many rows of enemies you want to have: ")) * 10
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("Space Invaders")
wn.bgpic("space_invaders_background.gif")
turtle.register_shape("invader.gif")
turtle.register_shape("player.gif")
wn.tracer(0)
bp = turtle.Turtle()
bp.speed(0)
bp.color("white")
bp.penup()
bp.setposition(-300,-300)
bp.pendown()
bp.pensize(3)
for side in range(4):
bp.fd(600)
bp.lt(90)
bp.hideturtle()
scr = 0
sp = turtle.Turtle()
sp.speed(0)
sp.color("white")
sp.penup()
sp.setposition(-290, 280)
scstr = "Score %s" %scr
sp.write(scstr, False, align="left", font=("Arial", 14, "normal"))
sp.hideturtle()
plr = turtle.Turtle()
plr.color("blue")
plr.shape("player.gif")
plr.penup()
plr.speed(0)
plr.setposition(0,-256)
plr.setheading(90)
plrspd = 10
enemies = []
enstx = -225
ensty = 250
ennum = 0
for i in range(nofenm):
enemies.append(turtle.Turtle())
for enemy in enemies:
enemy.color("red")
enemy.shape("invader.gif")
enemy.penup()
enemy.speed(0)
x = enstx + (50 * ennum)
y = ensty
enemy.setposition(x,y)
ennum += 1
if ennum == 10:
ensty -= 50
ennum = 0
enemyspeed = 0.10
blt = turtle.Turtle()
blt.color("yellow")
blt.shape("triangle")
blt.penup()
blt.speed(0)
blt.setheading(90)
blt.shapesize(0.5,0.5)
blt.hideturtle()
bltspd = 5
bltstt = "ready"
def fb():
global bltstt
if bltstt == "ready":
bltstt = "fire"
x = plr.xcor()
y = plr.ycor() + 10
blt.setposition(x,y)
blt.showturtle()
def move_left():
x = plr.xcor()
x -= plrspd
if x < -280:
x = - 280
plr.setx(x)
def move_right():
x = plr.xcor()
x += plrspd
if x > 280:
x = 280
plr.setx(x)
def ic(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
turtle.listen()
turtle.onkey(move_left, "Left")
turtle.onkey(move_right, "Right")
turtle.onkey(fb, "space")
while True:
wn.update()
for enemy in enemies:
x = enemy.xcor()
x += enemyspeed
enemy.setx(x)
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 ic(blt, enemy):
blt.hideturtle()
bltstt = "ready"
blt.setposition(0, -400)
enemy.setposition(0,10000)
scr += 1
scstr = "Score %s" %scr
sp.clear()
sp.write(scstr, False, align="left", font=("Arial", 14, "normal"))
if ic(plr, enemy):
plr.hideturtle()
enemy.hideturtle()
ending = "abc"
if bltstt == "fire":
y = blt.ycor()
y += bltspd
blt.sety(y)
if blt.ycor() > 275:
blt.hideturtle()
bltstt = "ready"
if ending == "abc":
end = input("game over, press enter to exit")
break
if scr == nofenm:
end = input("You Won!!!, press enter to exit")
break