From 0004b454687df724f7e3fd12d3a0423c5a7d020f Mon Sep 17 00:00:00 2001 From: Naunet <44895138+Naunet@users.noreply.github.com> Date: Tue, 26 May 2020 18:19:38 +0200 Subject: [PATCH] Add clock --- ToDo.txt | 5 +-- maze.py | 133 ++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 89 insertions(+), 49 deletions(-) diff --git a/ToDo.txt b/ToDo.txt index 2f6ddb5..de12c32 100644 --- a/ToDo.txt +++ b/ToDo.txt @@ -1,7 +1,4 @@ -3. Add transitional movement - -4. Add clock to screen -And pause visual +4. Add transitional movement 5. Create high score diff --git a/maze.py b/maze.py index dfa5789..ac79b52 100644 --- a/maze.py +++ b/maze.py @@ -3,8 +3,28 @@ from random import random, randrange import time -def background(color): - window.fill(color) +def background(color, level): + window.fill(color) + text = font.render("Level "+str(level), True, (0,0,20)) + textRect = text.get_rect() + offset = (HEADER-textRect.height)//2 + textRect.topleft = (offset, offset) + window.blit(text, textRect) + +def draw_clock(): + text = font.render("Pause", True, (0,0,0), (255,255,255)) + if end_level: + time = end_time//1000 + string = "{0:02}:{1:02}".format(time//60, time%60) + text = font.render(string, True, (255,0,0), (255,255,255)) + if not paused: + time = (pygame.time.get_ticks() - paused_time)//1000 #convert to seconds + string = "{0:02}:{1:02}".format(time//60, time%60) + text = font.render(string, True, (0,0,0), (255,255,255)) + textRect = text.get_rect() + offset = (HEADER-textRect.height)//2 + textRect.topright = (RES_X-offset, offset) + window.blit(text, textRect) def initialize_maze(): """20*22""" @@ -29,10 +49,10 @@ def draw_rectangle(topCorner, width, height, color): Rect = pygame.Rect(topCorner[0], topCorner[1], width, height) pygame.draw.rect(window, color, Rect) -def draw_maze(grid, size, color): +def draw_maze(size, color): for j in range(GRID_HEIGHT): for i in range(GRID_WIDTH): - if (grid[j][i]): + if (maze[j][i]): draw_rectangle(top_corner(i,j), size, size, color) def draw_char(x, y): @@ -43,19 +63,25 @@ def draw_char(x, y): y += (1-scale)/2*SIZE draw_rectangle((x,y), SIZE*scale, SIZE*scale, (0,0,0,255)) -def handle_interaction(x, y, grid, key): +def draw_game(level): + background(C_PATH, level) + draw_clock() + draw_maze(SIZE, C_WALL) + draw_char(pos_x, pos_y) + +def handle_interaction(key): """records player's interaction""" anim={'move':False,'right':False,'left':False,'up':False,'down':False,'quit':False,'pause':False} - if key==pygame.K_RIGHT and x0 and not(grid[y][x-1]): + if key==pygame.K_LEFT and pos_x>0 and not(maze[pos_y][pos_x-1]): anim['move'] = True anim['left'] = True - if key==pygame.K_UP and y>0 and not(grid[y-1][x]): + if key==pygame.K_UP and pos_y>0 and not(maze[pos_y-1][pos_x]): anim['move'] = True anim['up'] = True - if key==pygame.K_DOWN and y