-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_arcade_snake.py
109 lines (82 loc) · 3.44 KB
/
python_arcade_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
import arcade
from random import randrange
from time import sleep
SCREEN_WIDTH = 380
SCREEN_HEIGHT = 400
class MyWindow(arcade.Window):
def __init__(self):
super().__init__(SCREEN_WIDTH, SCREEN_HEIGHT, 'Snake Game', update_rate=0.09)
self.score = 0
self.gameOn = None
self.moved = None
self.snake_image = arcade.Sprite('snake.png')
self.snake_coords = []
self.snake_move_x = 0
self.snake_move_y = 20
self.head_image = arcade.Sprite('head.png')
self.snake_head = None
self.new_head_position = None
self.direction = [0,1]
self.food_image = arcade.Sprite('food.png')
self.food_coord = []
def setup(self):
self.snake_coords = [
[90,90],
[90,70],
[90,50]
]
self.snake_head = self.snake_coords[0]
self.food_coord = [randrange(10,370,20), randrange(10,350,20)]
self.gameOn = True
def on_key_press(self, symbol: int, modifiers: int):
self.moved = True
if symbol == arcade.key.D and self.direction[0] != -1:
self.snake_move_x = 20;self.snake_move_y = 0
self.direction = [1,0]
if symbol == arcade.key.A and self.direction[0] != 1:
self.snake_move_x = -20;self.snake_move_y = 0
self.direction = [-1,0]
if symbol == arcade.key.W and self.direction[1] != -1:
self.snake_move_y = 20;self.snake_move_x = 0
self.direction = [0,1]
if symbol == arcade.key.S and self.direction[1] != 1:
self.snake_move_y = -20;self.snake_move_x = 0
self.direction = [0,-1]
def on_draw(self):
arcade.start_render()
self.food_image.center_x = self.food_coord[0]
self.food_image.center_y = self.food_coord[1]
self.food_image.draw()
self.head_image.center_x = self.snake_coords[0][0]
self.head_image.center_y = self.snake_coords[0][1]
self.head_image.draw()
for x, y in self.snake_coords[1:]:
self.snake_image.center_x = x;self.snake_image.center_y = y
self.snake_image.draw()
arcade.draw_text(f'Score: {self.score}', 20, SCREEN_HEIGHT-20, arcade.csscolor.WHITE, 12, font_name='arial')
def update(self, delta_time: float):
if self.snake_head == self.food_coord:
self.score += 1
self.food_coord = [randrange(10, 370, 20), randrange(10, 350, 20)]
self.snake_coords.append(self.snake_coords[-1][0]+20)
if self.moved:
if self.snake_head[0] < 10:
sleep(1);quit()
elif self.snake_head[0] > SCREEN_WIDTH - 10:
sleep(1);quit()
elif self.snake_head[1] < 10:
sleep(1);quit()
elif self.snake_head[1] > SCREEN_HEIGHT - 10:
sleep(1);quit()
else:
self.snake_head = self.snake_coords[0]
self.new_head_position = [self.snake_head[0]+self.snake_move_x, self.snake_head[1]+self.snake_move_y]
self.snake_coords = ([self.new_head_position] + self.snake_coords[:-1])
if self.new_head_position in self.snake_coords[1:]:
sleep(1);quit()
def main():
win = MyWindow()
win.setup()
arcade.run()
if __name__ == '__main__':
main()