-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
129 lines (94 loc) · 3.22 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
121
122
123
124
125
126
127
128
129
import pygame
import random
from Snake import Snake
from SnakePT import SnakePT
from Food import Food
pygame.init()
#Display size options
width = 600
height = 600
display = pygame.display.set_mode((width, height))
pygame.display.update()
#Window caption
pygame.display.set_caption("Snake")
#Colors
blue = (0, 0, 255)
red = (255, 0, 0)
green = (0, 255, 0)
white = (255, 255, 255)
black = (0, 0, 0)
clock = pygame.time.Clock()
#Gamechanging variables
snakeBlock = 30
snakeSpeed = 15
font = pygame.font.SysFont(None, 50)
#Color options
boardColor = blue
snakeColor = black
foodColor = green
fontColor = red
class Game:
def Message(self, msg, color):
mesg = font.render(msg, True, color)
display.blit(mesg, [100, 100])
def DisplayScore(self, score, color):
scoreNumber = font.render(("Score: " + str(score)), True, color)
display.blit(scoreNumber, [20, 20])
def Game(self):
#snake and food initiation
snake = Snake(width, height, snakeBlock)
food = Food(width, height, snakeBlock)
#Must-be-like-that variables
gameOver = False
gameClose = False
#Score counter
score = 0
#Game loop
while not gameOver:
snake.frameIteration += 1
#Beggining screen/End screen inputs
while gameClose == True:
self.Game()
"""display.fill(boardColor)
self.DisplayScore(score, fontColor)
self.Message("Q - to quit | C - to continue", fontColor)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
gameOver = True
gameClose = False
if event.key == pygame.K_c:
self.Game()"""
#Game inputs/Snake movement
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameOver = True
elif event.type == pygame.KEYDOWN:
snake.Movement(event.key)
snake.x += snake.xChange
snake.y += snake.yChange
#Checking if board border has been touched
gameClose = snake.BorderTouch()
display.fill(boardColor)
#Respawning food
food.DropFood(display, foodColor)
#Checking if head ate its tail
if gameClose != True:
gameClose = snake.HeadMovement()
#Regenerating snake
snake.Snake(snakeBlock=snakeBlock, display=display, snakeColor=snakeColor)
self.DisplayScore(score, fontColor)
pygame.display.update()
#Checking if food has been eaten
if snake.x == food.foodX and snake.y == food.foodY:
#If true generating new food positon and increasing snake's length/score
food.RandomizePositon()
snake.snakeLength += 1
score += 1
snake.reward += 10
clock.tick(snakeSpeed)
pygame.quit()
quit()
gra = Game()
gra.Game()