This repository was archived by the owner on Apr 26, 2020. It is now read-only.
forked from nickclason/Python-Final-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayscreen.py
More file actions
144 lines (121 loc) · 5.83 KB
/
playscreen.py
File metadata and controls
144 lines (121 loc) · 5.83 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
import pygame as pg
from playelements import *
from utilities import *
from definitions import *
from os import path
from pathfinding import *
class PlayScreen:
def __init__(self, parent):
self.parent = parent
self.currentLevel = 0
self.startLevel(LEVELPATHS[0]) # Start game at first level
def startLevel(self, levelFilePath):
self.gameComplete = False
self.createMapFromFile(levelFilePath)
self.player = Player(self.startLocation[0], self.startLocation[1], self.tileSize)
self.playerInterval = 0
path = breadthFirst(self.map, (self.startLocation[1], self.startLocation[0]), (self.endLocation[1], self.endLocation[0]))
self.player.movesLeft = int((len(path) * 1.2)) # Player has to take a short-ish path
self.computer = Computer(self.startLocation[0], self.startLocation[1], self.tileSize, path)
self.computerInterval = 0
#Used for scrolling maps
self.camera = Camera(WIDTH, HEIGHT, self.tileSize, self.player, self.map)
self.mapDisplay = MapDisplay(self.map, self.tileSize)
def createMapFromFile(self, filePath):
dir = path.dirname(__file__)
self.map = [] # empty list to store map data
with open(path.join(dir, 'Levels/'+filePath), 'rt') as f:
params = f.__next__().split(':') # Map settings
self.tileSize = int(params[0])
self.computerPlayRate = int(params[1])
self.playerMoveRate = int(params[2])
for line in f:
mapLine = []
for c in line.rstrip():
mapLine.append(c) # read in map character by character
self.map.append(mapLine)
for row, tiles in enumerate(self.map): # Get start and end location
for col, tile in enumerate(tiles):
if tile == '2':
self.startLocation = (col, row)
if tile == '3':
self.endLocation = (col, row)
def draw(self, screen):
screen.fill(BLACK)
self.camera.moveCamera(self.player)
# Draw order: Map < Computer < Player
self.mapDisplay.draw(screen, self.camera)
self.computer.draw(screen, self.camera)
self.player.draw(screen, self.camera)
if self.gameComplete: # Round is over, display message
font = pg.font.Font(None, 100)
if self.playerWin: # Player has reached the end
if self.currentLevel >= len(LEVELPATHS)-1: # Player has finished all levels
font = pg.font.Font(None, 70)
text = font.render("Congratulations! You beat the Game!", True, GREEN)
else:
text = font.render("You completed the level!", True, WHITE)
elif self.outOfMoves: # Player has run out of moves
text = font.render("You have run out of moves!", True, WHITE)
else: # Computer has reached the end
text = font.render("The computer has won!", True, WHITE)
text_rect = text.get_rect(center=(WIDTH/2, HEIGHT/2)) # Center text on screen
screen.blit(text, text_rect)
continueText = font.render("Press Enter to Continue.", True, WHITE)
continueText_rect = continueText.get_rect(center=(WIDTH/2, HEIGHT/2 + 100))
screen.blit(continueText, continueText_rect)
def update(self):
self.outOfMoves = self.player.movesLeft <= 0 # Player has ran out of moves
self.playerWin = self.player.checkWin(self.map)
self.computerWin = self.computer.checkWin()
if self.outOfMoves:
self.gameComplete = True
if self.playerWin:
self.gameComplete = True
if self.computerWin:
self.gameComplete = True
if not self.gameComplete:
self.playerInterval += 1
if self.playerInterval % self.playerMoveRate == 0:
self.player.move(self.map)
self.computerInterval += 1
if self.computerInterval % self.computerPlayRate == 0:
self.computerInterval = 0
self.computer.move()
def handleEvents(self, event):
if not self.gameComplete:
if event.type == pg.KEYDOWN: # Player can only move in one direction at a time
if event.key == pg.K_UP:
self.player.clearVel()
self.player.yVel = -1
if event.key == pg.K_DOWN:
self.player.clearVel()
self.player.yVel = 1
if event.key == pg.K_LEFT:
self.player.clearVel()
self.player.xVel = -1
if event.key == pg.K_RIGHT:
self.player.clearVel()
self.player.xVel = 1
if event.type == pg.KEYUP:
if event.key == pg.K_UP:
if self.player.yVel == -1:
self.player.clearVel()
if event.key == pg.K_DOWN:
if self.player.yVel == 1:
self.player.clearVel()
if event.key == pg.K_LEFT:
if self.player.xVel == -1:
self.player.clearVel()
if event.key == pg.K_RIGHT:
if self.player.xVel == 1:
self.player.clearVel()
else:
if event.type == pg.KEYUP:
if event.key == pg.K_RETURN: # Reset level or proceed to next
if self.playerWin:
self.currentLevel += 1
if self.currentLevel == len(LEVELPATHS):
self.parent.running = False
return
self.startLevel(LEVELPATHS[self.currentLevel])