-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.py
63 lines (63 loc) · 2.16 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
import random
import arcade
import time
class Fake(arcade.Sprite):
def __init__(self,x,y):
super().__init__("src/apple.png")
self.center_x=x
self.center_y=y
self.width=1
self.height=1
class Snake(arcade.Sprite):
def __init__(self,speed):
super().__init__()
self.center_x=random.randint(0,25)*20
self.center_y=random.randint(0,25)*20
self.width=20
self.height=20
self.change_x=0
self.change_y=0
self.speed=speed
self.score=0
self.body=[]
self.c=0
self.num=1
self.time=0
self.num_eat=0
self.color=arcade.color.ORANGE
def move(self):
self.body.append({"x":self.center_x,"y":self.center_y})
if len(self.body)>self.score:
self.body.pop(0)
self.center_x+=self.change_x*self.speed
self.center_y+=self.change_y*self.speed
def draw(self):
arcade.draw_rectangle_filled(self.center_x, self.center_y,20,20, arcade.color.AZURE_MIST)
for i in self.body:
self.num+=1
if self.num%2==0:
arcade.draw_rectangle_filled(i["x"],i["y"],20,20,arcade.color.AMARANTH_PINK)
else:
arcade.draw_rectangle_filled(i["x"],i["y"],20,20,arcade.color.AQUA)
def Eat(self,food):
self.num_eat+=1
self.score+=food.score
def game_over(self):
if self.score<0:
if self.c==0:
self.c=2
self.time=time.time()
self.over=arcade.load_texture("src/REN.png")
arcade.draw_lrwh_rectangle_textured(0,0,600,600,self.over)
self.speed=0
for i in self.body:
self.fake=Fake(i["x"]-10*self.change_x,i["y"]-10*self.change_y)
if arcade.check_for_collision(self,self.fake):
if self.body.index(i) > len(self.body)-3:
...
else:
self.score=-1
if self.center_x>600 or self.center_x<0:
self.score=-1
if self.center_y>600 or self.center_y<0:
self.score=-1