-
Notifications
You must be signed in to change notification settings - Fork 0
/
level3.py
178 lines (145 loc) · 3.69 KB
/
level3.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
import pygame
from pygame.locals import *
from sys import exit
from random import randint
counter=0
def main():
while True:
b=[]
def update():
global counter
counter=(counter+1)%7
def blast(w,h):
image=pygame.image.load("explosed-sprite.png").convert_alpha()
width,height=image.get_size()
for i in range(int(width/w)):
b.append(image.subsurface((i*w,0,w,h)))
up=1
down=2
right=3
left=4
step=20
block=[20,20]
x=randint(1,20)
y=randint(2,22)
applexy=[]
x3=randint(1,25)
check=[x3*20,-20]
snakexy=[int(x*20),int(y*20)]
snakelist=[[x*20,y*20],[(x-20)*20,(y*20)]]
apple=0
dead=0
grow=0
direction=right
score=0
start=0
draw=[]
screen=pygame.display.set_mode((640,480),0,24)
clock=pygame.time.Clock()
#game loop
while not dead:
pressed=pygame.key.get_pressed()
for i in pygame.event.get():
if i.type==QUIT or pressed[K_q]:
exit()
image2=pygame.image.load("stone.png").convert_alpha()
pygame.transform.scale(image2,(20,20))
check[1]=check[1]+step
if check[1]>480:
x2=randint(1,25)
check[0]=x2*20
check[1]=0
if pressed[K_LEFT] and direction!=right:
direction=left
elif pressed[K_RIGHT] and direction!=left:
direction=right
elif pressed[K_UP] and direction!=down:
direction=up
elif pressed[K_DOWN] and direction!=up:
direction=down
if direction==right:
snakexy[0]=snakexy[0]+step
if snakexy[0]>=640:
snakexy[0]=0
elif direction==left:
snakexy[0]=snakexy[0]-step
if snakexy[0]<0:
snakexy[0]=620
elif direction==up:
snakexy[1]=snakexy[1]-step
if snakexy[1]<0:
snakexy[1]=460
elif direction==down:
snakexy[1]=snakexy[1]+step
if snakexy[1]>=480:
snakexy[1]=0
if snakelist.count(snakexy)>0:
for i in snakelist:
if i==snakexy:
draw.append(i)
break
dead=1
if apple==0:
x1=randint(1,31)
y1=randint(2,22)
applexy=[int(x1*step),int(y1*step)]
apple=1
snakelist.insert(0,list(snakexy))
if snakexy[0]==applexy[0] and snakexy[1]==applexy[1]:
apple=0
score=score+1
else:
snakelist.pop()
if snakelist.count(check)>0:
for i in snakelist:
if i==check:
draw.append(i)
break
dead=1
#display on the screen
screen.fill((0,0,0))
scr=pygame.font.SysFont("comicsansms",20)
text4=scr.render("Score : %d"%score,True,(0,255,0))
screen.blit(text4,(500,10))
screen.blit(image2,check)
pygame.draw.rect(screen,(255,0,0),Rect(applexy,block),0)
for i in snakelist:
pygame.draw.rect(screen,(0,255,0),Rect(i,block))
pygame.display.flip()
clock.tick(15)
if dead==1:
blast(20,20)
crash_sound = pygame.mixer.Sound("crash.wav")
pygame.mixer.Sound.play(crash_sound)
for i in range(7):
screen.blit(b[counter],(tuple(draw)))
update()
pygame.display.update()
clock.tick(10)
# Game over display
screen.fill((0,0,0))
over=pygame.font.SysFont("comicsansms",40)
s1=pygame.font.SysFont("comicsansms",30)
s2=pygame.font.SysFont("comicsansms",30)
s3 = pygame.font.SysFont("comicsansms", 30)
text5=over.render("GAME OVER",True,(0,255,0))
screen.blit(text5,(50,50))
screen.blit(text4,(200,200))
screen.blit(s1.render("Press s To Play Again",True,(0,255,0)),(50,250))
screen.blit(s2.render("Press l For Level Selection ",True,(0,255,0)),(50,300))
screen.blit(s3.render("Press q For Quit ", True, (0, 255, 0)), (50, 350))
pygame.display.flip()
while True:
for i in pygame.event.get():
if i.type==QUIT:
exit()
pressed=pygame.key.get_pressed()
if pressed[K_q]:
exit()
if pressed[K_s]:
main()
if pressed[K_l]:
break
break
if __name__=='__main__':
main()