-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.py
More file actions
110 lines (91 loc) · 2.42 KB
/
Copy pathsnake.py
File metadata and controls
110 lines (91 loc) · 2.42 KB
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
from threading import Thread
from shellEngine import *
import keyboard as kb
import random as r
import time as t
screen_width = 50
screen_height = 20
game = Game()
game.geometry(screen_width,screen_height)
print('Score: 0')
player = [Sprite([[block(3,'green'),block(3,'green')]])]
player[0].setx(20)
player[0].sety(10)
player[0].p = 0
length = 1
apple = []
for _ in range(2):
a = Sprite([[block(3,'red'),block(3,'red')]])
a.wall_physics = False
apple.append(a)
def move_apple(num):
apple[num].x = r.randint(0,(screen_width-1)//2)*2
apple[num].y = r.randint(0,screen_height-1)
apple[num].setx(0)
apple[num].sety(0)
game.changed = True
for i in range(len(apple)): move_apple(i)
def move(dir:int):
global length, extratext
positions = set()
for i in player:
i.p += 1
if i.p > length:
player.remove(i)
sprites.remove(i)
continue
positions.add((i.x,i.y))
# Player check
if len(positions) < len(player):
game.running = False
print_('You Died!')
return
player.insert(0,Sprite([[block(3,'green'),block(3,'green')]]))
player[0].p = 0
if dir == 0: # up
player[0].setx(player[1].x)
player[0].sety(player[1].y-1)
elif dir == 1: # left
player[0].setx(player[1].x-2)
player[0].sety(player[1].y)
elif dir == 2: # down
player[0].setx(player[1].x)
player[0].sety(player[1].y+1)
elif dir == 3: # right
player[0].setx(player[1].x+2)
player[0].sety(player[1].y)
# Apple check
for i,a in enumerate(apple):
if (a.x,a.y) in positions:
move_apple(i)
print(f'Score: {length}')
length += 1
queue = []
def on_press(key:kb.KeyboardEvent):
global direction
key = key.name
if key == 'w':
queue.append(0)
elif key == 'a':
queue.append(1)
elif key == 's':
queue.append(2)
elif key == 'd':
queue.append(3)
elif key == 'q':
game.running = False
direction = 0
last = 0
def gameloop():
global last
while game.running:
start = t.perf_counter()
if queue: last = queue.pop(0)
move(last)
game.changed = True
end = t.perf_counter()
duration = end-start
t.sleep(max(max(0.2-length/120,0.1)-duration,0))
kb.on_press(on_press,True)
Thread(target=gameloop,daemon=True).start()
game.run()