-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_on_py.py
executable file
·123 lines (97 loc) · 3.14 KB
/
py_on_py.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
#!/bin/python3
import pygame, sys,random
from pygame.math import Vector2
class APPLE:
def __init__(self):
self.apple_img = pygame.image.load('sprites/apple2.png')
self.rand_placement()
def draw(self):
apple_rect = pygame.Rect(self.pos.x * cell_s,self.pos.y * cell_s, cell_s, cell_s)
self.apple_img = pygame.transform.scale(self.apple_img,(cell_s,cell_s))
screen.blit(self.apple_img,apple_rect)
def rand_placement(self):
self.x = random.randint(0, cell_a - 1)
self.y = random.randint(0, cell_a - 1)
self.pos = Vector2(self.x, self.y)
class SNACK:
def __init__(self):
self.body = [Vector2(6,8),Vector2(6,9),Vector2(6,10)]
self.direction = Vector2(1,0)
self.new_block = False
def draw(self):
for part in self.body:
x_pos = int(part.x * cell_s)
y_pos = int(part.y * cell_s)
snack_rect = pygame.Rect(x_pos,y_pos,cell_s,cell_s)
pygame.draw.rect(screen,pygame.Color('green'),snack_rect)
def move(self):
if self.new_block:
body_copy = self.body[:]
else:
body_copy = self.body[:-1]
body_copy.insert(0,body_copy[0] + self.direction)
self.body = body_copy[:]
self.new_block = False
self.head = self.body[0]
self.tail = self.body[1:]
def make_me_longer(self):
self.new_block = True
class MAIN:
def __init__(self):
self.snack = SNACK()
self.apple = APPLE()
def update(self):
self.snack.move()
self.check_endgame()
self.check_omnomnom()
def draw(self):
screen.fill(pygame.Color('black'))
self.apple.draw()
self.snack.draw()
def check_omnomnom(self):
if self.apple.pos == self.snack.head:
self.apple.rand_placement()
self.snack.make_me_longer()
def check_snack_collision(self):
for part in self.snack.tail:
if part == self.snack.body[0]:
self.you_lost()
def check_endgame(self):
head_pos = self.snack.head
if (head_pos.x not in range(0, cell_a)) or (head_pos.y not in range(0, cell_a)):
self.you_lost()
self.check_snack_collision()
def you_lost(self):
print('GAME OVER!!!!!!!!!')
raise SystemExit
pygame.init()
cell_a = 20
cell_s = 40
screen = pygame.display.set_mode((cell_a * cell_s,cell_a * cell_s))
clock = pygame.time.Clock()
SCREEN_UPDATE = pygame.USEREVENT
pygame.time.set_timer(SCREEN_UPDATE,150)
main_game = MAIN()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == SCREEN_UPDATE:
main_game.update()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
if main_game.snack.direction.y == -1: continue
main_game.snack.direction = Vector2(0,1)
if event.key == pygame.K_UP:
if main_game.snack.direction.y == 1: continue
main_game.snack.direction = Vector2(0,-1)
if event.key == pygame.K_LEFT:
if main_game.snack.direction.x == 1: continue
main_game.snack.direction = Vector2(-1,0)
if event.key == pygame.K_RIGHT:
if main_game.snack.direction.x == -1: continue
main_game.snack.direction = Vector2(1,0)
main_game.draw()
pygame.display.update()
clock.tick(60)