-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (58 loc) · 2.01 KB
/
main.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
import sys
import pygame
from settings import Settings
from snack import Snake
from food import Food
#from button import Button
from scoreboard import Scoreboard
def levelup(score):
k = score / 150
speed = 10
if k > 0:
speed = k + 10
return speed
def clock(speed):
pygame.time.Clock().tick(speed)
def run_game():
pygame.init()
score = 0
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Gluttonous Snake")
snake = Snake()
food = Food()
active = True
'''play_button = Button(ai_settings, screen, "Play")
if not active:
play_button.draw_button()'''
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
snake.changedirection(event.key)
if event.key == pygame.K_SPACE and not active:
return run_game()
elif event.key == pygame.K_q:
sys.exit()
screen.fill(ai_settings.bg_color)
if active:
snake.move()
for rect in snake.body:
pygame.draw.rect(screen, (140, 0, 100), rect, 0)
if not active:
Scoreboard.printS(screen, (350, 300), 'YOU ARE DEAD!', (230, 0, 0), False, 100)
Scoreboard.printS(screen, (500, 420), 'press space to try again', (0, 0, 50), False, 30)
Scoreboard.printS(screen, (513, 445), 'press Q to exit game', (0, 0, 50), False, 30)
if food.rect == snake.body[0]:
score = Scoreboard.addscore(score)
food.remove()
snake.addnode()
food.set(ai_settings)
pygame.draw.rect(screen, (0, 150, 21), food.rect, 0)
Scoreboard.printS(screen, (50, 620), 'Scores: ' + str(score), (0, 0, 0))
speed = levelup(score)
active = snake.judge(ai_settings)
pygame.display.update()
clock(speed)
run_game()