-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.py
More file actions
131 lines (114 loc) · 4.58 KB
/
Game.py
File metadata and controls
131 lines (114 loc) · 4.58 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import pygame
import Grid
import Snake
import Animal
# Note: pygame.display.update() - updates a portion of the screen that it needs to
# pygame.display.flip() - updates the entire screen
class Game:
def __init__(self, window):
pygame.init()
self.winWidth = 1280
self.winHeight = 720
self.window = window
self.window.fill((0,0,0))
pygame.display.set_caption("PySnake")
self.black=(0,0,0)
self.white = (200,200,200)
self.grid = Grid.Grid(self.window)
# grid.drawGrid(window)
self.snake = Snake.Snake() #must remain outside of game loop - otherwise direction is always reset to right
self.animal = Animal.Animal(self.grid.steps)
self.loop = 0
self.run = True
def isGameOver(self):
#check if snake head meets snake body
sx,sy = self.snake.getSnakeHead();
# print("coordinate length",len(self.snake.bodyCoordinates))
for i in range(1,len(self.snake.bodyCoordinates)):
if self.snake.bodyCoordinates[i] == [sx,sy]:
# print("i",i)
# print("snake body:", self.snake.bodyCoordinates[i], "sx,sy:",[sx,sy])
return True
else:
return False
def endGame(self):
# draw_game()
self.displayEndMessage()
self.run = False
def displayEndMessage(self):
# prepare text
font1 = pygame.font.Font(None, 100)
font2 = pygame.font.Font(None, 35)
text1 = font1.render("Game Over! Score:" + str(self.snake.length),True,self.white)
text2 = font2.render('Press "Spacebar" on the keyboard to restart/play again', True, self.white)
#text position values
text1X = 550
text1Y = 50
text2X = 550
text2Y = 150
# display text
self.window.blit(text1,(text1X,text1Y))
# IMPORTANT #TODO Uncomment when you figure out how to restart the game.
# self.window.blit(text2,(text2X,text2Y))
# check if animal should be moved (happens after being eaten by snake)
def reassignAnimalBool(self):
# print("Just Checking")
x,y = self.animal.bodyCoordinates
# print("x:",x,"y:",y)
sx,sy = self.snake.getSnakeHead()
# print("sx:",sx,"sy:",sy)
# snake-x (sx) * steps because that equates to how many squares you move across (on a 500px/22px = 22.7 GRID, so I'll just approxitamte and use the steps variables which is 22)
if x == sx*self.grid.steps and y == sy*self.grid.steps :
return True
# reassign the animal
def reassignAnimal(self):
self.animal = Animal.Animal(self.grid.steps)
self.animal.drawAnimal(self.grid)
pygame.display.flip()
return self.animal
def draw_game(self):
self.grid.drawGrid(self.grid)
self.snake.turnSnakeHead()
self.snake.drawSnake(self.grid)
self.animal.drawAnimal(self.grid)
self.snake.displayScore(self.window)
pygame.display.update()
# pygame.draw.rect(window, (), (10,10,10,10))
def draw_game_end(self):
self.grid.drawGrid(self.grid.steps)
self.snake.drawSnake(self.grid)
self.animal.drawAnimal(self.grid)
self.displayEndMessage()
pygame.display.update()
def resetGame(self):
self.snake = Snake.Snake()
self.loop = 0
def playGame(self):
# Note: for some reason exiting the game loop
# exists the window
while self.run:
self.window.fill((0,0,0))
# print("loop:",self.loop)
pygame.time.delay(90)
if self.loop == 0 or self.isGameOver() == False:
self.loop += 1
self.snake.move()#game doesn't end on collision if not in this order
self.draw_game()
else:
self.draw_game_end()
#reassign Animal if eaten
if self.reassignAnimalBool():
self.snake.eat(self.animal)
print("Snake length:"+str(self.snake.length))
print("NEW ANIMAL")
self.animal = self.reassignAnimal()
pygame.display.update()
# Quiting the game
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.run = False
if event.type == pygame.K_SPACE:#doesn't get triggered for some reason
print('Spacebar pressed')
self.resetGame()
# pygame.display.flip()
# pygame.display.update()