Skip to content

Commit

Permalink
Add home page
Browse files Browse the repository at this point in the history
  • Loading branch information
Naunet committed Jun 16, 2020
1 parent 906358f commit af3c28c
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 58 deletions.
Binary file added img/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/vine.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
98 changes: 66 additions & 32 deletions maze.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from pygame.constants import *
import time
# local
import view, wall
import view
import wall


def handle_interaction(key):
Expand Down Expand Up @@ -70,19 +71,52 @@ def save_score(current_time): # in ms
scorefile.close()


def run():
global close_game, paused, level_end, end_time
if event.type == LOOP_SHIFT:
maze.handle_shift()
if event.type == pygame.KEYDOWN:
key_flags.append(event.key)
pygame.time.set_timer(LOOP_KEY, 200) # hold down loop
if event.type == pygame.KEYDOWN or event.type == LOOP_KEY:
for key in key_flags:
anim = handle_interaction(key)
close_game = anim['quit']
paused = pause_game(paused, anim['pause'])
maze.move(anim, paused)
if event.type == pygame.KEYUP:
key_flags.remove(event.key)
if not key_flags:
pygame.time.set_timer(LOOP_KEY, 0)

draw.header(current_level, paused, paused_time, level_end, end_time)
maze.draw()

if maze.is_end_game():
if not level_end:
end_time = pygame.time.get_ticks()-paused_time
level_end = True
paused = True
pygame.time.set_timer(LOOP_SHIFT, 0)
save_score(end_time)
draw.end(current_level, end_time)


# define macro variables
GRID_WIDTH = 20
GRID_HEIGHT = 22
if GRID_WIDTH < 5 or GRID_HEIGHT < 5:
raise Exception("Maze dimensions too small! ({}, {})".format(GRID_WIDTH, GRID_HEIGHT))
TIMER = 1000
raise Exception("Maze dimensions too small! \
({}, {})".format(GRID_WIDTH, GRID_HEIGHT))
TIMER = 400 #1000
LOOP_KEY = USEREVENT+2
LOOP_SHIFT = USEREVENT+1

# define global variables
key_flags = list()
current_level = 1
level_end = False
paused = False
paused_time = 0
pause_start = None
end_time = None
Expand All @@ -92,40 +126,40 @@ def save_score(current_time): # in ms
draw = view.View(GRID_WIDTH, GRID_HEIGHT)
maze = wall.Wall(draw, GRID_WIDTH, GRID_HEIGHT)

# screen flag
menu = True
settings = False
levels = False
game = False
sandbox = False

# program
close_game = False
paused = False
while not close_game:
for event in [pygame.event.wait()] + pygame.event.get():
if event.type == LOOP_SHIFT:
maze.handle_shift()
if event.type == pygame.KEYDOWN:
key_flags.append(event.key)
pygame.time.set_timer(LOOP_KEY, 200) # hold down loop
if event.type == pygame.KEYDOWN or event.type == LOOP_KEY:
for key in key_flags:
anim = handle_interaction(key)
close_game = anim['quit']
paused = pause_game(paused, anim['pause'])
maze.move(anim, paused)
if event.type == pygame.KEYUP:
key_flags.remove(event.key)
if not key_flags:
pygame.time.set_timer(LOOP_KEY, 0)
if event.type == pygame.QUIT:
close_game = True

draw.header(current_level, paused, paused_time, level_end, end_time)
maze.draw()

if maze.is_end_game():
if not level_end:
end_time = pygame.time.get_ticks()-paused_time
level_end = True
paused = True
pygame.time.set_timer(LOOP_SHIFT, 0)
save_score(end_time)
draw.end(current_level, end_time)

if menu:
boxes = draw.menu()
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
clicked = [index for index, b in enumerate(boxes) if b.collidepoint(pos)]
print(clicked)
if clicked:
if clicked[0]==0:
menu = False
#levels = True
game = True
if clicked[0]==1:
menu = False
sandbox = True
if clicked[0]==2:
menu = False
settings = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q or event.key == pygame.K_ESCAPE:
close_game = True
if game:
run()
pygame.display.update()
quit()
90 changes: 64 additions & 26 deletions view.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pygame

from os import path

class View:
def __init__(self, width, height):
Expand All @@ -20,83 +20,77 @@ def __init__(self, width, height):
pygame.font.init()
self.window = pygame.display.set_mode((self.RES_X, self.RES_Y))
pygame.display.set_caption("Moving labyrinth")
self.font = pygame.font.Font(pygame.font.get_default_font(), 32)

self.fonts = dict()
self.fonts['standard'] = pygame.font.Font(pygame.font.get_default_font(), 32)
self.fonts['large'] = pygame.font.Font(pygame.font.get_default_font(), 54)
self.images = dict()
self.load()

def _top_corner(self, x, y):
"""coordinates to pixels"""
x = self.SIZE*x+self.BORDER_X
y = self.SIZE*y+self.BORDER_Y
return (x, y)


def background(self, color, level):
self.window.fill(color)
self.word("Level "+str(level),
(self.RES_X//2, self.HEADER//2),
pygame.Color('black'))

(self.RES_X//2, self.HEADER//2))

def clock(self, paused, paused_time, is_level_end, end_time):
text = self.font.render("Pause", True, (0, 0, 0), (255, 255, 255))
text = self.fonts['standard'].render("Pause", True, (0, 0, 0), (255, 255, 255))
if is_level_end:
time = end_time//1000
string = "{0:02}:{1:02}".format(time//60, time % 60)
text = self.font.render(string, True, (255, 0, 0), (255, 255, 255))
text = self.fonts['standard'].render(string, True, (255, 0, 0), (255, 255, 255))
if not paused:
time = (pygame.time.get_ticks() - paused_time)//1000 # in seconds
string = "{0:02}:{1:02}".format(time//60, time % 60)
text = self.font.render(string, True, (0, 0, 0), (255, 255, 255))
text = self.fonts['standard'].render(string, True, (0, 0, 0), (255, 255, 255))
textRect = text.get_rect()
offset = (self.HEADER-textRect.height)//2
textRect.topright = (self.RES_X-offset, offset)
self.window.blit(text, textRect)


def rectangle(self, topCorner, width, height, color):
Rect = pygame.Rect(topCorner[0], topCorner[1], width, height)
pygame.draw.rect(self.window, color, Rect)

return Rect

def maze(self, maze, size, color):
for j in range(self.GRID_HEIGHT):
for i in range(self.GRID_WIDTH):
if maze[j][i]:
self.rectangle(self._top_corner(i, j), 25, 25, color)


def char(self, x, y):
# To be improved
x, y = self._top_corner(x, y)
scale = 0.7
x += (1-scale)/2*self.SIZE
y += (1-scale)/2*self.SIZE
self.rectangle((x, y), self.SIZE*scale,
self.SIZE*scale, (0, 0, 0, 255))

self.SIZE*scale, (0, 0, 0, 255))

def header(self, level, paused, paused_time, is_level_end, end_time):
self.background(self.C_PATH, level)
self.clock(paused, paused_time, is_level_end, end_time)


def game(self, maze, pos_x, pos_y):
self.maze(maze, self.SIZE, self.C_WALL)
self.char(pos_x, pos_y)


def word(self, text, pos, color, background=None):
word_surface = self.font.render(text, True, color, background)
def word(self, text, pos, color=pygame.Color('black'), background=None, font='standard'):
word_surface = self.fonts[font].render(text, True, color, background)
rect = word_surface.get_rect()
rect.center = pos
self.window.blit(word_surface, rect)
return rect.width, rect.height


def text(self, surface, height, text):
words = [word.split(' ') for word in text.splitlines()]
max_width, _ = surface.get_size()
space = self.font.size(' ')[0] # width of a space
space = self.fonts['standard'].size(' ')[0] # width of a space
while words:
while words and not words[0]:
words.pop(0)
Expand All @@ -105,7 +99,7 @@ def text(self, surface, height, text):
line = ""
size = 0
word = words[0].pop(0)
text = self.font.render(word, True, (0, 0, 0), (255, 255, 255))
text = self.fonts['standard'].render(word, True, (0, 0, 0), (255, 255, 255))
width, tmp = text.get_size()
middle = tmp
while size + space + width < max_width:
Expand All @@ -114,16 +108,15 @@ def text(self, surface, height, text):
if not words[0]:
break
word = words[0].pop()
text = self.font.render(word, True, (0, 0, 0), (255, 255, 255))
text = self.fonts['standard'].render(word, True, (0, 0, 0), (255, 255, 255))
width, tmp = text.get_size()
if tmp < middle:
tmp = middle
height += middle//2
_, tmp = self.word(line, (max_width//2, height),
pygame.Color('black'), pygame.Color('white'))
background=pygame.Color('white'))
height += tmp//2


def end(self, current_level, end_time):
# read highscores
scorefile = open('highscores.txt', 'r+')
Expand All @@ -139,5 +132,50 @@ def end(self, current_level, end_time):
string = "Level Complete!\n"
if end_time//1000 <= highscore:
string += "New "
string += "Highscore:\n{0:02}:{1:02}s".format(highscore//60, highscore % 60)
string += "Highscore:\n{0:02}:{1:02}s".format(
highscore//60, highscore % 60)
self.text(self.window, self.RES_Y//3, string)

def load(self):
# main menu
background = pygame.image.load(path.join('img', 'tree.png'))
#80 = header + fraction of vine
scale = min(self.RES_X/background.get_width(), (self.RES_Y-80)/background.get_height())
background = pygame.transform.scale(background,
(int(background.get_width()*scale), int(background.get_height()*scale)))
self.images['background'] = background
vine = pygame.image.load(path.join('img', '2.png'))
factor = self.RES_X/vine.get_width()
vine = pygame.transform.scale(vine, (self.RES_X, int(vine.get_height()*factor)))
self.images['vine'] = vine

def buttons(self, values, pos, background, color=pygame.Color('black')):
# find variables
size, height = self.fonts['standard'].size(values[0])
for text in values:
size = max(size, self.fonts['standard'].size(text)[0])
size += 30
height += 20
left = (self.RES_X-size)//2
# draw buttons
boxes = list()
x, y = pos
for text in values:
box = self.rectangle((left,y-height//2), size, height, background)
boxes.append(box) #(left,left+size,y-height//2,y+height//2)
self.word(text, (x, y),
color)
y += 80
return boxes #(x1, x2, y1, y2)

def menu(self):
self.window.fill((255,231,122)) #(153, 221, 255)
rect = self.images['background'].get_rect()
rect.center = (self.RES_X//2,self.RES_Y//2+80//2)
self.window.blit(self.images['background'], rect)
self.window.blit(self.images['vine'], (0,20))
self.word("Labyrinth", (self.RES_X//2, 70), font='large')
C_LEAF = (111,133,97) # (44,95,45)
boxes = self.buttons(["Level select", "Sandbox", "Settings"],
(self.RES_X//2, 180), C_LEAF)
return boxes

0 comments on commit af3c28c

Please sign in to comment.