-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingleplay.py
More file actions
167 lines (147 loc) · 6.38 KB
/
Copy pathsingleplay.py
File metadata and controls
167 lines (147 loc) · 6.38 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import sys
import random
import time
from constant import HEIGHT, RestartGameSingle, WIDTH, GAMETICK, MODULO_SCREEN, Orientation
from save import Score, addNewScore, loadingGame, saveGame
#! Class use to handle Apple behavior
class Apple:
def __init__(self, x, y):
self.x = x
self.y = y
# ? Allow to make respawn a new apple
def respawn(self, state):
xApple, yApple = -1, -1
for c in range(0, 8):
xApple, yApple = random.randint(0, 39), random.randint(0, 39)
for el in state:
if el['x'] == xApple and el['y'] == yApple:
xApple, yApple = -1, -1
continue
break
if xApple == -1 and yApple == -1:
for xApple in range(0, 39):
for yApple in range(0, 39):
for el in state:
if el['x'] == xApple and el['y'] == yApple:
continue
self.x = xApple
self.y = yApple
return
self.x = xApple
self.y = yApple
#! Class use to handle Player behavior
class Player:
#!Class Constructor
#! Size is the snacke lenght (use to calculate the score)
#! State is a list contain the snake's part (exemple: [{'x': 19, 'y': 19, 'look': 'up'}, {'x': 19, 'y': 18, 'look': 'left'}])
def __init__(self, size, state):
self.size = size
self.state = state
#! move forward
def move(self):
save = self.state[0]['look']
for i in range(len(self.state)):
if (self.state[i]['look'] == 'up'):
self.state[i]['y'] -= 1
if (self.state[i]['look'] == 'down'):
self.state[i]['y'] += 1
if (self.state[i]['look'] == 'left'):
self.state[i]['x'] -= 1
if (self.state[i]['look'] == 'right'):
self.state[i]['x'] += 1
if i != 0:
tmp = self.state[i]['look']
self.state[i]['look'] = save
save = tmp
#! Change The direction of the Snacke
def changeOrientation(self, newOrientation):
# print('go ', newOrientation)
if (newOrientation == 'up' and (self.size == 1 or self.state[0]['look'] != 'down')):
self.state[0]['look'] = 'up'
if (newOrientation == 'down' and (self.size == 1 or self.state[0]['look'] != 'up')):
self.state[0]['look'] = 'down'
if (newOrientation == 'left' and (self.size == 1 or self.state[0]['look'] != 'right')):
self.state[0]['look'] = 'left'
if (newOrientation == 'right' and (self.size == 1 or self.state[0]['look'] != 'left')):
self.state[0]['look'] = 'right'
#! Check If the Snacke Head can eat the Apple
def CheckEatApple(self, apple, lastX, lastY, lastLook):
if apple.x == self.state[0]['x'] and apple.y == self.state[0]['y']:
apple.respawn(self.state)
self.state.append({'x': lastX, 'y': lastY, 'look': lastLook})
self.size += 1
#! Check If the Snacke is alive (return True) or Dead (return False)
def isAlive(self):
if self.state[0]['x'] < 0 or self.state[0]['y'] < 0 or self.state[0]['x'] > 39 or self.state[0]['y'] > 39:
return False
for i in range(len(self.state)):
for i2 in range(len(self.state)):
if i != i2 and self.state[i]['x'] == self.state[i2]['x'] and self.state[i]['y'] == self.state[i2]['y']:
return False
return True
#! Handle All Display part (and a little more)
def displayGame(pygame, screen, player, apple):
lastX, lastY, lastLook = player.state[len(player.state) - 1]['x'], player.state[len(
player.state) - 1]['y'], player.state[len(player.state) - 1]['look']
player.move()
for i in range(len(player.state)):
z = pygame.image.load("textures/snackeBody.png")
if i == 0:
z = pygame.image.load("textures/snackeHead.png")
if i == len(player.state) - 1 and i != 0:
z = pygame.image.load("textures/snackeTail.png")
z = pygame.transform.rotate(z, Orientation[player.state[i]['look']])
screen.blit(
z, (player.state[i]['x']*MODULO_SCREEN, player.state[i]['y']*MODULO_SCREEN))
player.CheckEatApple(apple, lastX, lastY, lastLook)
a = pygame.image.load("textures/Apple.png")
screen.blit(a, (apple.x*MODULO_SCREEN, apple.y*MODULO_SCREEN))
pygame.display.update()
pass
#! singlePlay function is use to handle the snacke game
#! pygame => lib
#! screen => pygame window
#! loadSave => bool for knowing if loading Save Or not
def singlePlay(pygame, screen, menu, loadSave):
clock = pygame.time.Clock()
size = 1 # default Value
xApple, yApple = random.randint(
0, 39), random.randint(0, 39) # default Value
state = [{'x': 19, 'y': 19, 'look': 'up'}] # default Value
RestartGameSingle = False
bg = pygame.image.load("textures/GameBackground.jpg")
bg = pygame.transform.scale(bg, (WIDTH, HEIGHT))
# Use saved game data
if loadSave:
size, state, xApple, yApple = loadingGame()
# Create Resource
player = Player(size, state)
apple = Apple(xApple, yApple)
while player.isAlive() and not RestartGameSingle:
pygame.display.update()
clock.tick(GAMETICK)
screen.blit(bg, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.changeOrientation('left')
if event.key == pygame.K_DOWN:
player.changeOrientation('down')
if event.key == pygame.K_UP:
player.changeOrientation('up')
if event.key == pygame.K_RIGHT:
player.changeOrientation('right')
if event.key == pygame.K_ESCAPE:
RestartGameSingle = menu.pauseMenuSinglePlayer(
pygame, player, apple, menu)
displayGame(pygame, screen, player, apple)
# End the game
if not RestartGameSingle and player.state[0]['x'] != -5:
menu.displayGameOver(player.size)
addNewScore(Score("Solo Player", player.size))
time.sleep(2)
return RestartGameSingle