-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhad1.py
80 lines (48 loc) · 1.53 KB
/
had1.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
import pyglet
from pyglet.window.key import LEFT, RIGHT, UP, DOWN
window = pyglet.window.Window()
TILE_WIDTH = 64
TILE_HEIGHT = 64
FIELD_WIDTH = 10
FIELD_HEIGHT = 10
window.width = TILE_WIDTH*FIELD_WIDTH
window.height = TILE_HEIGHT*FIELD_HEIGHT
square = pyglet.image.load('green.png')
snake = pyglet.sprite.Sprite(square)
position_x = 5 #position označuje buňky
position_y = 5
snake_tiles = [(1,1), (2,1), (2,2)]#last is head
food = pyglet.image.load('apple.png')
apple = pyglet.sprite.Sprite(food)
apple.scale = 0.25 #zmenšení obrázku
food_position = (7,7)
def move_snake(direction):
head = snake_tiles[-1]
if direction == LEFT:
head = head[0] -1 , head[1]
elif direction == RIGHT:
head = head[0] + 1, head[1]
elif direction == UP:
head = head[0], head[1] + 1
elif direction == DOWN:
head = head[0], head[1] - 1
if head in snake_tiles:
raise ValueError('srážka s hadem')
if head[0] <= 0 or head[0] >= FIELD_WIDTH or head[1] >= FIELD_HEIGHT:
raise ValueError('náraz do zdi')
snake_tiles.append(head)
del snake_tiles[0]
def drawing():
window.clear()
for tile in snake_tiles:
snake.x = tile[0] * TILE_WIDTH
snake.y = tile[-1] * TILE_HEIGHT
snake.draw()
apple.x = food_position[0] * TILE_WIDTH
apple.y = food_position[1] * TILE_HEIGHT
apple.draw()
def press_button(symbol, mode):
move_snake(symbol)
#def move()
window.push_handlers(on_draw = drawing, on_key_release = press_button)
pyglet.app.run()