-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
187 lines (165 loc) · 3.71 KB
/
game.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
import turtle
import os
import math
import random
# Set the score to Zero
score=0
# Score Pen
score_p=turtle.Turtle()
score_p.speed(0)
score_p.color("white")
score_p.penup()
score_p.setposition(-290,280)
Scorestring="Score: %s" %score
score_p.write(Scorestring,False,align="left")
score_p.hideturtle()
##################################################
# maintaing high score
highscore=0
hscore_p=turtle.Turtle()
hscore_p.speed(0)
hscore_p.color("white")
hscore_p.penup()
hscore_p.setposition(-290,260)
hScorestring="High Score: %s" %highscore
hscore_p.write(hScorestring,False,align="left")
hscore_p.hideturtle()
#############################################
#Screen Of The game
screen=turtle.Screen()
screen.bgcolor("brown")
#screen.bgpic("giphy.gif")
screen.title("My First Game")
############################################
# Gaming region
pen1=turtle.Turtle()
pen1.speed(0)
pen1.penup()
pen1.color("red")
pen1.setposition(-300,-300)
pen1.pendown()
pen1.pensize(4)
for i in range(4):
pen1.fd(600)
pen1.lt(90)
pen1.hideturtle()
############################################
# Registering an image
turtle.register_shape("player.gif")
#############################################
# Now draw the player pen
penp=turtle.Turtle()
penp.pensize(3)
penp.shape("triangle")
penp.color("blue")
penp.penup()
penp.speed(0)
penp.setposition(0,-250)
penp.setheading(90)
playerspeed=10
###############################################
#bullet turtle
bullet=turtle.Turtle()
bullet.shape("triangle")
bullet.color("yellow")
bullet.penup()
bullet.speed(0)
bullet.shapesize(0.5,0.5)
bullet.setheading(90)
bulletspeed=20
state=0
bullet.hideturtle()
################################################
#All the important functions
def fire():#When bullet is fired
global state
if state ==0:
state=1
bullet.setposition(penp.xcor(),penp.ycor())
bullet.showturtle()
#Move player
def ml():#Move left
x=penp.xcor()
x-=playerspeed
if x < -290:
x=-290
penp.setx(x)
def mr():#Move right
x=penp.xcor()
x+=playerspeed
if x > 290:
x=290
penp.setx(x)
def collision(t1,t2):#check collision
dis=math.sqrt((math.pow(t1.xcor()-t2.xcor(),2))+math.pow(t2.ycor()-t1.ycor(),2))
if dis <= 15:
return True
return False
turtle.listen()
turtle.onkey(ml,"Left")
turtle.onkey(mr,"Right")
turtle.onkey(fire,"space")
##############################################################
#CPU turtles
n=int(raw_input("Enter The Number Of Enemies You Want?"))
enemies=[]
for i in range(n):
enemies.append(turtle.Turtle())
for pene in enemies:
x=random.randint(-200,200)
y=random.randint(100,250)
pene.shape("circle")
pene.color("black")
pene.penup()
pene.speed(0)
pene.setposition(x,y)
enemyspeed=2
##################################################
#Main Loop
while 1:
for pene in enemies:
x=pene.xcor()
x+=enemyspeed
pene.setx(x)
if x>290:
for i in enemies:
y=i.ycor()
y-=40
i.sety(y)
enemyspeed*=-1
if x<-290:
for i in enemies:
y=i.ycor()
y-=40
i.sety(y)
enemyspeed*=-1
if state==1:
yb=bullet.ycor()
yb+=bulletspeed
bullet.sety(yb)
if bullet.ycor() >= 290:
state=0
bullet.hideturtle()
for pene in enemies:
if (collision(bullet,pene)):
score+=100
if highscore < score:
highscore=score
bullet.hideturtle()
bullet.setposition(0,-400)
pene.setposition(-200,250)
state=0
Scorestring="Score: %s" %score
score_p.clear()
score_p.write(Scorestring,False,align="left")
hScorestring="High Score: %s" %highscore
hscore_p.clear()
hscore_p.write(hScorestring,False,align="left")
if (collision(penp,pene)):
penp.hideturtle()
pene.hideturtle()
print "Game Over"
break
####################################################################
# Keep the o/p stable
delay=raw_input("Press Enter to Esc")