Skip to content

Commit

Permalink
standardization
Browse files Browse the repository at this point in the history
  • Loading branch information
1024Adam committed Jul 5, 2022
1 parent 1ce4b2c commit eeca29e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 76 deletions.
16 changes: 13 additions & 3 deletions infinite_maze/Clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ def __init__(self):
self.time = pygame.time.Clock()
self.startTime = self.time.get_time()

self.milliseconds = 0
self.prevMillis = 0
self.millis = 0
self.millisPaused = 0
self.ticks = 0

def update(self):
self.prevMillis = self.millis
self.time.tick()
self.millis += self.time.get_time()

self.tick()

def getTimeString(self):
minutes = int(self.millis / 60000)
seconds = int((self.millis / 1000) % 60)
Expand All @@ -26,15 +29,22 @@ def reset(self):
self.time = pygame.time.Clock()
self.startTime = self.time.get_time()

self.prevMillis = 0
self.millis = 0
self.ticks = 0

def getPrevMillis(self):
return self.prevMillis

def getMillis(self):
return (self.millis)
return self.millis

def getSeconds(self):
return int((self.millis / 1000) % 60)

def getPrevSeconds(self):
return int((self.prevMillis / 1000) % 60)

def rollbackMillis(self, rollback):
self.millis -= rollback

Expand Down
99 changes: 36 additions & 63 deletions infinite_maze/Game.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,37 @@
from Line import Line

class Game:
# Display config
WIDTH = 640
HEIGHT = 480
X_MIN = 80
Y_MIN = 40

X_MAX = (WIDTH / 2)
Y_MAX = (HEIGHT - 32)

SCORE_INCREMENT = 1

BG_COLOR = pygame.Color(255, 255, 255)
FG_COLOR = pygame.Color(0, 0, 0)

def __init__(self):
pygame.init()

self.width = 640
self.height = 480
self.xMin = 80
self.xMax = (self.width / 2)
self.yMin = 40
self.yMax = (self.height - 32)

self.screen = pygame.display.set_mode((self.width, self.height))

# Font Config
self.font = pygame.font.SysFont('', 20)

self.screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT))

self.icon = pygame.Surface((32, 32))
iconImage = pygame.image.load('img/icon.png')
self.icon.blit(iconImage, (0, 0))

pygame.display.set_caption('Infinite Maze')
pygame.display.set_icon(self.icon)

# Font Config
self.font = pygame.font.SysFont('', 20)
self.bgColor = pygame.Color(255, 255, 255)
self.fgColor = pygame.Color(0, 0, 0)

self.pace = 0
self.score = 0
self.scoreIncrement = 1

self.paused = False
self.over = False
Expand All @@ -40,46 +44,42 @@ def __init__(self):

def updateScreen(self, player, lines):
# Paint Screen
self.screen.fill(self.bgColor)
self.screen.fill(self.BG_COLOR)
self.screen.blit(player.getCursor(), player.getPosition())
for line in lines:
pygame.draw.line(self.getScreen(), self.fgColor, line.getStart(), line.getEnd(), 1)
pygame.draw.line(self.getScreen(), self.FG_COLOR, line.getStart(), line.getEnd(), 1)

prevMillis = self.clock.getMillis()
prevSeconds = self.clock.getSeconds()
# Update Clock/Ticks
self.clock.tick()
self.clock.update()

if self.paused:
self.clock.rollbackMillis(self.clock.getMillis() - prevMillis)
self.clock.rollbackMillis(self.clock.getMillis() - self.clock.getPrevMillis())

# Update Pace
if not self.paused and self.clock.getMillis() > 10000 and self.clock.getSeconds() % 30 == 0 and prevSeconds != self.clock.getSeconds():
if not self.paused and self.clock.getMillis() > 10000 and self.clock.getSeconds() % 30 == 0 and self.clock.getPrevSeconds() != self.clock.getSeconds():
self.pace += 1

# Print Border
pygame.draw.line(self.getScreen(), self.fgColor, (self.xMin, self.yMin), (self.width, self.yMin), 2)
pygame.draw.line(self.getScreen(), self.fgColor, (self.xMin, self.yMax + 10), (self.width, self.yMax + 10), 2)
pygame.draw.line(self.getScreen(), self.fgColor, (80, self.yMin), (80, self.yMax + 10), 2)
pygame.draw.line(self.getScreen(), self.FG_COLOR, (self.X_MIN, self.Y_MIN), (self.WIDTH, self.Y_MIN), 2)
pygame.draw.line(self.getScreen(), self.FG_COLOR, (self.X_MIN, self.Y_MAX + 10), (self.WIDTH, self.Y_MAX + 10), 2)
pygame.draw.line(self.getScreen(), self.FG_COLOR, (80, self.Y_MIN), (80, self.Y_MAX + 10), 2)

# Print Display Text
timeText = self.font.render('Time: ' + self.clock.getTimeString(), 1, self.fgColor)
timeText = self.font.render('Time: ' + self.clock.getTimeString(), 1, self.FG_COLOR)
self.screen.blit(timeText, (10, 10))
scoreText = self.font.render('Score: ' + str(self.score), 1, self.fgColor)
scoreText = self.font.render('Score: ' + str(self.score), 1, self.FG_COLOR)
self.screen.blit(scoreText, (10, 25))

if (self.paused):
scoreText = self.font.render('Paused (press space to continue)', 1, self.fgColor)
scoreText = self.font.render('Paused (press space to continue)', 1, self.FG_COLOR)
self.screen.blit(scoreText, (100, 10))

pygame.display.flip()

def printEndDisplay(self):
# Paint Screen
self.screen.fill(self.bgColor)
endText = self.font.render('Continue? (y/n)', 1, self.fgColor)
scoreText = self.font.render('Score: ' + str(self.score), 1, self.fgColor)
self.screen.fill(self.BG_COLOR)
endText = self.font.render('Continue? (y/n)', 1, self.FG_COLOR)
scoreText = self.font.render('Score: ' + str(self.score), 1, self.FG_COLOR)

# Print Display Text
self.screen.blit(endText, (10, 10))
Expand Down Expand Up @@ -122,10 +122,10 @@ def updateScore(self, amount):
self.score += amount

def incrementScore(self):
self.score += self.scoreIncrement
self.score += self.SCORE_INCREMENT

def decrementScore(self):
self.score -= self.scoreIncrement if self.score > 0 else 0
self.score -= self.SCORE_INCREMENT if self.score > 0 else 0

def setScore(self, newScore):
self.score = newScore
Expand All @@ -136,39 +136,12 @@ def isPaused(self):
def changePaused(self, player):
self.paused = not self.paused
if (self.paused):
self.fgColor = pygame.Color(128, 128, 128)
self.FG_COLOR = pygame.Color(128, 128, 128)
player.setCursor('img/player_paused.png')
else:
self.fgColor = pygame.Color(0, 0, 0)
self.FG_COLOR = pygame.Color(0, 0, 0)
player.setCursor('img/player.png')

def getWidth(self):
return (self.width)

def getHeight(self):
return (self.height)

def getXMin(self):
return (self.xMin)

def getYMin(self):
return (self.yMin)

def getXMax(self):
return (self.xMax)

def getYMax(self):
return (self.yMax)

def getFont(self):
return (self.font)

def getBGColor(self):
return (self.bgColor)

def getFGColor(self):
return (self.fgColor)

def getPace(self):
return (self.pace)

Expand Down
8 changes: 4 additions & 4 deletions infinite_maze/Line.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ def generateMaze(game, width, height):
sideA = (19 * x) + 1
sideB = sideA + 1

xPos = (22 * x) + game.getXMax()
xPos = (22 * x) + game.X_MAX
for y in range(1, height - 1):
yPos = (22 * y) + game.getYMin()
yPos = (22 * y) + game.Y_MIN
lines.append(Line((xPos, yPos), (xPos + 22, yPos), sideA, sideB))
sideA = sideB
sideB += 1
Expand All @@ -90,9 +90,9 @@ def generateMaze(game, width, height):
sideA = y + 1
sideB = sideA + 19

yPos = (22 * y) + game.getYMin()
yPos = (22 * y) + game.Y_MIN
for x in range(1, width * 2):
xPos = (22 * x) + game.getXMax()
xPos = (22 * x) + game.X_MAX
lines.append(Line((xPos, yPos), (xPos, yPos + 22), sideA, sideB))
sideA = sideB
sideB += 19
Expand Down
18 changes: 12 additions & 6 deletions infinite_maze/infinite_maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def maze():
lines = Line.generateMaze(game, 15, 20)

game.getClock().reset()
millis = 0
keys = pygame.key.get_pressed()
while (game.isPlaying()):
while (game.isActive()):
Expand Down Expand Up @@ -116,15 +117,15 @@ def maze():
line.setXEnd(line.getXEnd() - game.getPace())

# Position Adjustments (to prevent screen overflow)
if (player.getX() < game.getXMin()):
if (player.getX() < game.X_MIN):
game.end()
if (player.getX() > game.getXMax()):
player.setX(game.getXMax())
if (player.getX() > game.X_MAX):
player.setX(game.X_MAX)
for line in lines:
line.setXStart(line.getXStart() - player.getSpeed())
line.setXEnd(line.getXEnd() - player.getSpeed())
player.setY(max(player.getY(), game.getYMin()))
player.setY(min(player.getY(), game.getYMax()))
player.setY(max(player.getY(), game.Y_MIN))
player.setY(min(player.getY(), game.Y_MAX))

# Reposition lines that have been passed
xMax = Line.getXMax(lines)
Expand Down Expand Up @@ -156,14 +157,19 @@ def maze():
if any(event.type == pygame.QUIT for event in pygame.event.get()):
game.end()

# Process FPS
processTime = int(game.getClock().getMillis() - game.getClock().getPrevMillis())
if (processTime <= 16):
time.delay(16 - processTime)

# Game has ended
game.printEndDisplay()

# Quit Events
keys = pygame.key.get_pressed()
if (keys[pygame.K_y]):
game.reset();
player.reset(80, (game.getHeight() / 2))
player.reset(80, (game.HEIGHT / 2))

# Maze Details
lines = Line.generateMaze(game, 15, 20)
Expand Down

0 comments on commit eeca29e

Please sign in to comment.