-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsnake.py
More file actions
117 lines (97 loc) · 3.55 KB
/
Copy pathsnake.py
File metadata and controls
117 lines (97 loc) · 3.55 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
115
116
117
import pygame
import random
import sys
from pygame.locals import *
class SnakeGame(object):
""" Snake Game adapted from site: http://pygame.org/project-Snake+in+35+lines-818-.html
Requires pygame to work
"""
def __init__(self, human_player=True):
self.human_player = human_player
self.xs = [290, 290, 290, 290, 290]
self.ys = [290, 270, 250, 230, 210]
self.dirs = 0
self.score = 0
self.applepos = (random.randint(0, 590), random.randint(0, 590))
pygame.init()
self.s = pygame.display.set_mode((600, 600))
pygame.display.set_caption('Snake')
self.appleimage = pygame.Surface((10, 10))
self.appleimage.fill((0, 255, 0))
self.img = pygame.Surface((20, 20))
self.img.fill((255, 0, 0))
self.f = pygame.font.SysFont('Arial', 20)
self.clock = pygame.time.Clock()
def collide(self, x1, x2, y1, y2, w1, w2, h1, h2):
if x1 + w1 > x2 and x1 < x2 + w2 and y1 + h1 > y2 and y1 < y2 + h2:
return True
else:
return False
def die(self, screen, score):
self.f = pygame.font.SysFont('Arial', 30)
t = self.f.render('Your score was: ' + str(score), True, (0, 0, 0))
screen.blit(t, (10, 270))
pygame.display.update()
pygame.time.wait(2000)
sys.exit(0)
def get_input(self):
for e in pygame.event.get():
if e.type == QUIT:
sys.exit(0)
elif e.type == KEYDOWN:
if e.key == K_UP and self.dirs != 0:
self.dirs = 2
elif e.key == K_DOWN and self.dirs != 2:
self.dirs = 0
elif e.key == K_LEFT and self.dirs != 1:
self.dirs = 3
elif e.key == K_RIGHT and self.dirs != 3:
self.dirs = 1
def check_collision(self):
i = len(self.xs) - 1
while i >= 2:
if self.collide(self.xs[0], self.xs[i], self.ys[0], self.ys[i], 20, 20, 20, 20):
self.die(self.s, self.score)
i -= 1
if self.collide(self.xs[0], self.applepos[0], self.ys[0], self.applepos[1], 20, 10, 20, 10):
self.score += 1
self.xs.append(700)
self.ys.append(700)
self.applepos = (random.randint(0, 590), random.randint(0, 590))
if self.xs[0] < 0 or self.xs[0] > 580 or self.ys[0] < 0 or self.ys[0] > 580:
self.die(self.s, self.score)
def move_snake(self):
i = len(self.xs) - 1
while i >= 1:
self.xs[i] = self.xs[i - 1]
self.ys[i] = self.ys[i - 1]
i -= 1
if self.dirs == 0:
self.ys[0] += 20
elif self.dirs == 1:
self.xs[0] += 20
elif self.dirs == 2:
self.ys[0] -= 20
elif self.dirs == 3:
self.xs[0] -= 20
def update_screen(self):
self.s.fill((255, 255, 255))
for i in range(0, len(self.xs)):
self.s.blit(self.img, (self.xs[i], self.ys[i]))
self.s.blit(self.appleimage, self.applepos)
t = self.f.render(str(self.score), True, (0, 0, 0))
self.s.blit(t, (10, 10))
pygame.display.update()
def next_step(self):
if self.human_player:
self.clock.tick(10)
self.get_input()
self.check_collision()
self.move_snake()
self.update_screen()
def run(self):
while True:
self.next_step()
if __name__ == "__main__":
sg = SnakeGame(human_player=True)
sg.run()