This repository has been archived by the owner on Jun 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
121 lines (101 loc) · 4.14 KB
/
game.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
import sys
import pygame
import time
import random
import settings as C
from snake import Snake
import requests
class SnakeGame:
def __init__(self):
self.wall = []
self.apple = ()
pygame.init()
self.screen = pygame.display.set_mode(C.RES)
self.snake = Snake(self.screen)
pygame.display.set_caption(C.WINDOW_CAPTION)
def post_score(self, score):
# leaderboard URL
url ="http://88.99.84.62:5001/scores/"
# what we want to post
payload = {
'name': C.NAME,
'value': score
}
# send the request and receive a response
response = requests.post(url, data=payload)
# HTTP status code 200 is a successful request
# HTTP status code 201 means that a record has been created
if response.status_code == 201:
return True
return False
def generate_walls(self):
for n in range(20, C.RES[0], 20):
pygame.draw.circle(self.screen, (0, 0, 255), (n, 20), 10)
self.wall.append([n, 20])
pygame.draw.circle(self.screen, (0, 0, 255), (n, C.RES[1] - 20), 10)
self.wall.append([n, C.RES[1] - 20])
for n in range(20, C.RES[1], 20):
pygame.draw.circle(self.screen, (0, 0, 255), (20, n), 10)
self.wall.append([20, n])
pygame.draw.circle(self.screen, (0, 0, 255), (C.RES[0] - 20, n), 10)
self.wall.append([C.RES[0] - 20, n])
pygame.display.flip()
def create_apple(self):
self.apple = ()
while (list(self.apple) in self.wall) or (list(self.apple) in self.snake.elements) or (not self.apple):
self.apple = (random.randrange(40, C.RES[0] - 40, 20),
(random.randrange(40, C.RES[1] - 40, 20)))
pygame.draw.circle(self.screen, (255, 0, 0), self.apple, C.RADIUS)
pygame.display.flip()
def event_loop(self):
while True:
time.sleep(C.WAIT)
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.exit_game()
elif event.type == pygame.KEYDOWN:
if (event.key == pygame.K_DOWN) and \
(self.snake.speed != [0, -2 * C.RADIUS]):
self.snake.speed = [0, 2 * C.RADIUS]
elif (event.key == pygame.K_UP) and \
(self.snake.speed != [0, 2 * C.RADIUS]):
self.snake.speed = [0, -2 * C.RADIUS]
elif (event.key == pygame.K_RIGHT) and \
(self.snake.speed != [-2 * C.RADIUS, 0]):
self.snake.speed = [2 * C.RADIUS, 0]
elif (event.key == pygame.K_LEFT) and \
(self.snake.speed != [2 * C.RADIUS, 0]):
self.snake.speed = [-2 * C.RADIUS, 0]
elif event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
self.snake.move(cbk_dead=self.check_dead,
cbk_apple=self.check_apple)
def exit_dead(self):
score = (len(self.snake.elements) - C.START_LENGTH + 1) * C.DIFFICULTY
print("Difficulty:\t%d" % C.DIFFICULTY)
print("Bugs eaten:\t%d" % (len(self.snake.elements) - C.START_LENGTH + 1))
print("Score:\t\t%d" % score)
# post the score on the leaderboard
if self.post_score(score):
print("Your score has been posted on the leaderboard.")
else:
print("Your score could not be posted.")
time.sleep(1)
self.exit_game()
def exit_game(self):
pygame.quit()
sys.exit()
def check_dead(self):
if list(self.snake.get_head()) in self.snake.elements[1:]:
self.exit_dead()
if list(self.snake.get_head()) in self.wall:
self.exit_dead()
def check_apple(self):
if self.snake.get_head() == self.apple:
self.snake.eat()
self.create_apple()
def run(self):
self.generate_walls()
self.create_apple()
self.event_loop()