snakeware

Python-based Linux distro for x86-64 and RPi
git clone git://squid-tech.com/snakeware.git
Log | Files | Refs | README

commit aa1d6f4a3417fa6e47fc7fd492b48fa72e65c728
parent ae70cdf957e47e1213b193f584db104b2e855682
Author: Josh Moore <jxm5210@g.rit.edu>
Date:   Sat,  6 Jun 2020 15:36:20 -0400

Merge pull request #83 from ChaosKid42/python_maze

add a very simple maze game
Diffstat:
Asnakewm/apps/games/maze/README.txt | 4++++
Asnakewm/apps/games/maze/__init__.py | 9+++++++++
Asnakewm/apps/games/maze/assets/floor0.png | 0
Asnakewm/apps/games/maze/assets/floor1.png | 0
Asnakewm/apps/games/maze/assets/floor2.png | 0
Asnakewm/apps/games/maze/assets/player_down.jpg | 0
Asnakewm/apps/games/maze/assets/player_left.jpg | 0
Asnakewm/apps/games/maze/assets/player_right.jpg | 0
Asnakewm/apps/games/maze/assets/player_up.jpg | 0
Asnakewm/apps/games/maze/assets/wall.png | 0
Asnakewm/apps/games/maze/maze.py | 192+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asnakewm/apps/games/maze/mazewindow.py | 36++++++++++++++++++++++++++++++++++++
12 files changed, 241 insertions(+), 0 deletions(-)

diff --git a/snakewm/apps/games/maze/README.txt b/snakewm/apps/games/maze/README.txt @@ -0,0 +1,4 @@ +This Maze game was adapted by ChasoKid42 from this: +https://github.com/ChaosKid42/python-maze/ + +Player moves with left/right/up. diff --git a/snakewm/apps/games/maze/__init__.py b/snakewm/apps/games/maze/__init__.py @@ -0,0 +1,9 @@ +from .mazewindow import MazeWindow + +def load(manager, params): + pos = (100, 100) + + if params is not None and len(params) > 0: + pos = params[0] + + MazeWindow(pos, manager) diff --git a/snakewm/apps/games/maze/assets/floor0.png b/snakewm/apps/games/maze/assets/floor0.png Binary files differ. diff --git a/snakewm/apps/games/maze/assets/floor1.png b/snakewm/apps/games/maze/assets/floor1.png Binary files differ. diff --git a/snakewm/apps/games/maze/assets/floor2.png b/snakewm/apps/games/maze/assets/floor2.png Binary files differ. diff --git a/snakewm/apps/games/maze/assets/player_down.jpg b/snakewm/apps/games/maze/assets/player_down.jpg Binary files differ. diff --git a/snakewm/apps/games/maze/assets/player_left.jpg b/snakewm/apps/games/maze/assets/player_left.jpg Binary files differ. diff --git a/snakewm/apps/games/maze/assets/player_right.jpg b/snakewm/apps/games/maze/assets/player_right.jpg Binary files differ. diff --git a/snakewm/apps/games/maze/assets/player_up.jpg b/snakewm/apps/games/maze/assets/player_up.jpg Binary files differ. diff --git a/snakewm/apps/games/maze/assets/wall.png b/snakewm/apps/games/maze/assets/wall.png Binary files differ. diff --git a/snakewm/apps/games/maze/maze.py b/snakewm/apps/games/maze/maze.py @@ -0,0 +1,192 @@ +import pygame +import os +from random import randrange, shuffle, random + +class Maze: + dirToDelta = { + 0: ( 0, -1), + 1: ( 1, 0), + 2: ( 0, 1), + 3: (-1, 0), + } + + def __init__(self, size): + + app_path = os.path.dirname(os.path.abspath(__file__)) + assets_path = app_path + "/assets" + + self.size = size + self.sprite_size = (32, 32) + self.width = int(self.size[0]/self.sprite_size[0])-8 + self.height = int(self.size[1]/self.sprite_size[1])-2 + self.font_color = (255, 255, 255) + self.font_bgcolor = (0, 0, 0) + self.background = pygame.Surface(size) # make a background surface + self.background = self.background.convert() + self.background.fill((0, 0, 0)) + + self.player = [] + self.player.append(pygame.image.load(assets_path + "/player_up.jpg").convert()) + self.player.append(pygame.image.load(assets_path + "/player_right.jpg").convert()) + self.player.append(pygame.image.load(assets_path + "/player_down.jpg").convert()) + self.player.append(pygame.image.load(assets_path + "/player_left.jpg").convert()) + self.floor = [] + self.floor.append(pygame.image.load(assets_path + "/floor0.png").convert()) + self.floor.append(pygame.image.load(assets_path + "/floor1.png").convert()) + self.floor.append(pygame.image.load(assets_path + "/floor2.png").convert()) + self.wall = pygame.image.load(assets_path + "/wall.png").convert() + + self.font = pygame.font.Font(None, 24) + self.font_height = self.font.size("")[1] + self.font_xpos = (self.width + 2) * self.sprite_size[1] + self.font_width = self.size[0] - self.font_xpos + + self.maze = [[-1 for x in range(self.height)] for x in range(self.width)] + + self.walls = self.height*self.width - 1 + self.moves = 0 + self.direction = 0 + self.x, self.y = randrange(self.width), randrange(self.height) + + self.maze[self.x][self.y] = 1 + self.visited = 1 + self.visited2 = 0 + self.unvisited = 0 + self.bumps = 0 + + self.generate() + + def process_event(self, event): + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_LEFT: + self.turn(-1) + elif event.key == pygame.K_RIGHT: + self.turn() + elif event.key == pygame.K_UP: + self.move() + else: + return False + return True + return False + + def draw(self, surface): + surface.blit(self.background, (0, 0)) + for y in range(-1, self.height+1): + self.draw_wall(surface, -1, y) + self.draw_wall(surface, self.width, y) + for x in range(-1, self.width+1): + self.draw_wall(surface, x, -1) + self.draw_wall(surface, x, self.height) + self.draw_stats_table(surface) + for y in range(self.height): + for x in range(self.width): + if self.block_free((x,y)): + self.draw_empty(surface, x, y) + else: + self.draw_wall(surface, x, y) + self.draw_player(surface) + + def draw_wall(self, surface, x, y): + rect = (((x+1)*self.sprite_size[0], (y+1)*self.sprite_size[1]), (self.sprite_size)) + surface.blit(self.wall, rect) + + def draw_empty(self, surface, x, y): + rect = (((x+1)*self.sprite_size[0], (y+1)*self.sprite_size[1]), (self.sprite_size)) + surface.blit(self.floor[self.maze[x][y]], rect) + + def render_stats_text(self, surface, text, y): + text = self.font.render(text, 1, self.font_color) + textpos = text.get_rect() + textpos.move_ip(self.font_xpos, y * self.font_height) + textpos.width = self.font_width + textpos.right + surface.fill(self.font_bgcolor, textpos) + surface.blit(text, textpos) + + def draw_player(self, surface): + rect = (((self.x+1)*self.sprite_size[0], (self.y+1)*self.sprite_size[1]), (self.sprite_size)) + surface.blit(self.player[self.direction], rect) + + def draw_stats_table(self, surface): + self.render_stats_text(surface, "Maze", 0) + self.render_stats_text(surface, "Dimensions", 2) + self.render_stats_text(surface, "{} x {}".format(self.width, + self.height), 3) + self.render_stats_text(surface, "Walls", 5) + self.render_stats_text(surface, "{}".format(self.walls), 6) + self.render_stats_text(surface, "Unvisited", 8) + self.render_stats_text(surface, "{}".format(self.unvisited), 9) + self.render_stats_text(surface, "Visited", 11) + self.render_stats_text(surface, "{}".format(self.visited), 12) + self.render_stats_text(surface, "2nd Vis.", 14) + self.render_stats_text(surface, "{}".format(self.visited2), 15) + self.render_stats_text(surface, "Bumps", 17) + self.render_stats_text(surface, "{}".format(self.bumps), 18) + + def generate(self, deep=True, loop_prob = 0.05): + x, y = self.x, self.y + self.loop_prob = loop_prob + ends = self.walled_neigbour_blocks((x, y)) + while ends: + if deep: + x, y = ends.pop() + else: + x, y = ends.pop(randrange(len(ends))) + if self.block_removeable((x, y)): + self.maze[x][y] = 0 + self.walls -= 1 + self.unvisited += 1 + ends += self.walled_neigbour_blocks((x, y)) + + def block_free(self, coord): + x, y = coord[0], coord[1] + return self.maze[x][y] != -1 + + def in_bounds(self, coord): + x, y = coord[0], coord[1] + return ( x >= 0 and x < self.width and + y >= 0 and y < self.height) + + def block_removeable(self, coord): + if self.block_free(coord): + return False + + x, y = coord[0], coord[1] + bl = [(x-1, y), (x+1, y), (x, y-1), (x, y+1)] + n = 0 + for i in bl: + if self.in_bounds(i) and self.block_free(i): + n += 1 + return n <= 1 or random() < self.loop_prob + + def walled_neigbour_blocks(self, coord): + x, y = coord[0], coord[1] + bl = [(x-1, y), (x+1, y), (x, y-1), (x, y+1)] + shuffle(bl) + rbl = [] + for i in bl: + if self.in_bounds(i) and not self.block_free(i): + rbl.append(i) + return rbl + + def turn(self, n = 1): + self.direction += n + self.direction %= 4 + + def move(self): + x, y = self.x, self.y + nx = x + self.dirToDelta[self.direction][0] + ny = y + self.dirToDelta[self.direction][1] + + if self.in_bounds((nx, ny)) and self.block_free((nx, ny)): + if self.maze[nx][ny] == 0: + self.maze[nx][ny] = 1 + self.visited += 1 + self.unvisited -= 1 + elif self.maze[nx][ny] == 1: + self.maze[nx][ny] = 2 + self.visited2 += 1 + self.x, self.y = nx, ny + self.moves += 1 + else: + self.bumps += 1 diff --git a/snakewm/apps/games/maze/mazewindow.py b/snakewm/apps/games/maze/mazewindow.py @@ -0,0 +1,36 @@ +import pygame +import pygame_gui + +from pygame_gui.ui_manager import UIManager +from pygame_gui.elements.ui_window import UIWindow +from pygame_gui.elements.ui_image import UIImage + +from .maze import Maze + +class MazeWindow(UIWindow): + def __init__(self, position, ui_manager): + super().__init__( + pygame.Rect(position, (640, 480)), + ui_manager, + window_display_title="maze", + object_id="#maze_window", + ) + + game_surface_size = self.get_container().get_size() + self.game_surface_element = UIImage( + pygame.Rect((0, 0), game_surface_size), + pygame.Surface(game_surface_size).convert(), + manager=ui_manager, + container=self, + parent_element=self, + ) + + self.maze = Maze(game_surface_size) + + def process_event(self, event): + super().process_event(event) + return self.maze.process_event(event) + + def update(self, time_delta): + super().update(time_delta) + self.maze.draw(self.game_surface_element.image)