-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
114 lines (88 loc) · 3.27 KB
/
game.py
File metadata and controls
114 lines (88 loc) · 3.27 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
111
112
113
114
import pygame
import sys
import random
pygame.init()
SW, SH = 800, 800
BLOCK_SIZE = 50
FONT = pygame.font.Font("font.ttf", BLOCK_SIZE*2)
screen = pygame.display.set_mode((800, 800))
pygame.display.set_caption("Snake!")
clock = pygame.time.Clock()
class Snake:
def __init__(self):
self.x, self.y = BLOCK_SIZE, BLOCK_SIZE
self.xdir = 1
self.ydir = 0
self.head = pygame.Rect(self.x, self.y, BLOCK_SIZE, BLOCK_SIZE)
self.body = [pygame.Rect(self.x-BLOCK_SIZE, self.y, BLOCK_SIZE, BLOCK_SIZE)]
self.dead = False
def update(self):
global apple
for square in self.body:
if self.head.x == square.x and self.head.y == square.y:
self.dead = True
if self.head.x not in range(0, SW) or self.head.y not in range(0, SH):
self.dead = True
if self.dead:
self.x, self.y = BLOCK_SIZE, BLOCK_SIZE
self.head = pygame.Rect(self.x, self.y, BLOCK_SIZE, BLOCK_SIZE)
self.body = [pygame.Rect(self.x-BLOCK_SIZE, self.y, BLOCK_SIZE, BLOCK_SIZE)]
self.xdir = 1
self.ydir = 0
self.dead = False
apple = Apple()
self.body.append(self.head)
for i in range(len(self.body)-1):
self.body[i].x, self.body[i].y = self.body[i+1].x, self.body[i+1].y
self.head.x += self.xdir * BLOCK_SIZE
self.head.y += self.ydir * BLOCK_SIZE
self.body.remove(self.head)
class Apple:
def __init__(self):
self.x = int(random.randint(0, SW)/BLOCK_SIZE) * BLOCK_SIZE
self.y = int(random.randint(0, SH)/BLOCK_SIZE) * BLOCK_SIZE
self.rect = pygame.Rect(self.x, self.y, BLOCK_SIZE, BLOCK_SIZE)
def update(self):
pygame.draw.rect(screen, "orange", self.rect)
def drawGrid():
for x in range(0, SW, BLOCK_SIZE):
for y in range(0, SH, BLOCK_SIZE):
rect = pygame.Rect(x, y, BLOCK_SIZE, BLOCK_SIZE)
pygame.draw.rect(screen, "#3c3c3b", rect, 1)
score = FONT.render("1", True, "white")
score_rect = score.get_rect(center=(SW/2, SH/20))
drawGrid()
snake = Snake()
apple = Apple()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_DOWN:
snake.ydir = 1
snake.xdir = 0
elif event.key == pygame.K_UP:
snake.ydir = -1
snake.xdir = 0
elif event.key == pygame.K_RIGHT:
snake.ydir = 0
snake.xdir = 1
elif event.key == pygame.K_LEFT:
snake.ydir = 0
snake.xdir = -1
snake.update()
screen.fill('black')
drawGrid()
apple.update()
score = FONT.render(f"{len(snake.body) + 1}", True, "white")
pygame.draw.rect(screen, "green", snake.head)
for square in snake.body:
pygame.draw.rect(screen, "green", square)
screen.blit(score, score_rect)
if snake.head.x == apple.x and snake.head.y == apple.y:
snake.body.append(pygame.Rect(square.x, square.y, BLOCK_SIZE, BLOCK_SIZE))
apple = Apple()
pygame.display.update()
clock.tick(5)