-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
161 lines (147 loc) · 5.13 KB
/
snake.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
import random
from pygame import *
pygame.init()
pygame.display.set_caption("Snake")
win = pygame.display.set_mode((1280,640))
pos=[32,32]
clock = pygame.time.Clock()
font = pygame.font.SysFont(None, 55)
def text_screen(x):
screen_text = font.render(x, True, (255,0,0))
win.blit(screen_text,(0,0))
def plot_snake():
for x,y in snk_list:
pygame.draw.rect(win, (255,255,255), [x, y,32 ,32])
pygame.draw.rect(win, (0,0,0), [snk_list[len(snk_list)-1][0],snk_list[len(snk_list)-1][1] ,32 ,32])
if list[4] is True:
pygame.draw.rect(win,(255,255,255),(pos[0],pos[1],32,32))
pygame.draw.circle(win,(0,0,0),(pos[0]+26,pos[1]+8),2)
pygame.draw.circle(win,(0,0,0),(pos[0]+26,pos[1]+24),2)
pygame.draw.rect(win,(255,0,0),(pos[0]+32,pos[1]+12,6,8))
if list[1] is True:
pygame.draw.rect(win,(255,255,255),(pos[0],pos[1],32,32))
pygame.draw.circle(win,(0,0,0),(pos[0]+8,pos[1]+8),2)
pygame.draw.circle(win,(0,0,0),(pos[0]+8,pos[1]+24),2)
pygame.draw.rect(win,(255,0,0),(pos[0],pos[1]+12,-4,8))
if list[2] is True:
pygame.draw.rect(win,(255,255,255),(pos[0],pos[1],32,32))
pygame.draw.circle(win,(0,0,0),(pos[0]+8,pos[1]+8),2)
pygame.draw.circle(win,(0,0,0),(pos[0]+24,pos[1]+8),2)
pygame.draw.rect(win,(255,0,0),(pos[0]+12,pos[1]-6,8,8))
if list[3] is True:
pygame.draw.rect(win,(255,255,255),(pos[0],pos[1],32,32))
pygame.draw.circle(win,(0,0,0),(pos[0]+8,pos[1]+26),2)
pygame.draw.circle(win,(0,0,0),(pos[0]+24,pos[1]+26),2)
pygame.draw.rect(win,(255,0,0),(pos[0]+12,pos[1]+32,8,8))
snk_list = []
fpos=[224,224]
list=[True,False,False,False,True,False,4,0,10]
# run,left ,up ,down,right,eaten,speed,list[7]
def dead():
pygame.display.set_caption("Snake-You Loose")
list[0]=False
def draw():
pygame.draw.rect(win,(0,0,0),(0,0,1280,640))
pygame.draw.rect(win,(255,0,0),(fpos[0],fpos[1],32,32))
def move():
if list[2]:
pos[1]-=list[6]
if [pos[0],pos[1]-32] in snk_list[:-1] :
dead()
if list[3]:
pos[1]+=list[6]
if [pos[0]+32,pos[1]+32] in snk_list[:-1] :
dead()
if list[1]:
pos[0]-=list[6]
if [pos[0]-32,pos[1]] in snk_list[:-1] :
dead()
if list[4]:
pos[0]+=list[6]
if [pos[0]+32,pos[1]] in snk_list[:-1] :
dead()
return pos[0],pos[1]
def fruit():
if pos[0]==fpos[0] :
if list[3] is True and pos[1]==fpos[1]:
list[5]=True
if list[2] is True and pos[1]==fpos[1]:
list[5]=True
if pos[1]==fpos[1]:
if list[4] is True and pos[0]==fpos[0]:
list[5] =True
if list[1] is True and pos[0]==fpos[0]:
list[5]=True
if list[5]:
list[8]+=12
while list[5]:
fpos[1]=random.randint(0,640)
fpos[0]=random.randint(0,1280)
if fpos[0]%32==0 and fpos[1]%32==0 and fpos[0]!=1280 and fpos[1]!=640 and [fpos[0],fpos[1]] not in snk_list:
list[5]=False
list[7]+=10
def action():
keys = pygame.key.get_pressed()
pos[0],pos[1]=move()
if keys [pygame.K_UP]:
if list[3] is False :
if list[1] is True or list[4] is True:
list[2]=True
if pos[0]%32!=0:
pos[0]=int(pos[0]/32)
if list[4] is True:
pos[0]=(pos[0]+1)*32
if list[1] is True:
pos[0]=(pos[0])*32
list[4]=list[1]=False
elif keys [pygame.K_DOWN]:
if list[2] is False :
if list[1] is True or list[4] is True:
list[3]=True
if pos[0]%32!=0:
pos[0]=int(pos[0]/32)
if list[4]:
pos[0]=(pos[0]+1)*32
else:
pos[0]=(pos[0])*32
list[1]=list[4]=False
elif keys[pygame.K_RIGHT]:
if list[1] is False :
if list[2] is True or list[3] is True:
list[4]=True
if pos[1]%32!=0:
pos[1]=int(pos[1]/32)
if list[3]:
pos[1]=(pos[1]+1)*32
else:
pos[1]=(pos[1])*32
list[2]=list[3]=False
elif keys[pygame.K_LEFT]:
if list[4] is False :
if list[2] is True or list[3] is True:
list[1]=True
if pos[1]%32!=0:
pos[1]=int(pos[1]/32)
if list[3]:
pos[1]=(pos[1]+1)*32
else:
pos[1]=(pos[1])*32
list[3]=list[2]=list[4]=False
if pos[0]>1248 or pos[1]>608 or pos[0]<0 or pos[1]<0:
dead()
while list[0]:
pygame.display.update()
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
list[0] = False
fruit()
action()
draw()
snk_list.append([pos[0],pos[1]])
if len(snk_list)>list[8]:
del snk_list[0]
plot_snake()
text_screen("Score: " + str(list[7]))
pygame.quit()