Massive refactoring to use tile in background

Use pygame's display to setup a 15 cells square grid, with a player
moving on it. Do not clean the trail, border collision.
Next step: build an gui object for representing the maze.
This commit is contained in:
Fred Z 2018-03-20 23:10:09 +01:00
parent 38a9834da6
commit 11347f967d
17 changed files with 74 additions and 66 deletions

74
gui.py Normal file
View File

@ -0,0 +1,74 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Author: freezed <freezed@users.noreply.github.com> 2018-03-15
Version: 0.1
Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/
This file is part of [_ocp3_ project](https://github.com/freezed/ocp3)
"""
import pygame
from pygame.locals import (
K_UP, K_DOWN, K_RIGHT, K_LEFT, KEYDOWN, QUIT, RESIZABLE
)
MAZE_SIZE_CEL = 15
CELL_SIZE_PX = 50
BACK_TILE_IMG = 'img/blue-transp-50.png'
PLAYER_TILE_IMG = 'img/player-50.png'
WINDOW_SIZE_PX = CELL_SIZE_PX * MAZE_SIZE_CEL
pygame.init()
WINDOW = pygame.display.set_mode(
(WINDOW_SIZE_PX, WINDOW_SIZE_PX),
RESIZABLE
)
GRID = range(MAZE_SIZE_CEL ** 2)
back_tiles = []
for cell in GRID:
back_tiles.append(pygame.image.load(BACK_TILE_IMG).convert())
x = (cell % MAZE_SIZE_CEL) * CELL_SIZE_PX
y = (cell // MAZE_SIZE_CEL) * CELL_SIZE_PX
WINDOW.blit(back_tiles[cell], (x, y))
PLAYER = pygame.image.load(PLAYER_TILE_IMG).convert_alpha()
player_pos = PLAYER.get_rect()
# Player position
WINDOW.blit(PLAYER, player_pos)
# Refresh
pygame.display.flip()
loop = True
while loop:
for event in pygame.event.get():
if event.type == QUIT:
loop = False
if event.type == KEYDOWN:
if event.key == K_DOWN:
player_pos = player_pos.move(0, CELL_SIZE_PX)
if event.type == KEYDOWN:
if event.key == K_LEFT:
player_pos = player_pos.move(-CELL_SIZE_PX, 0)
if event.type == KEYDOWN:
if event.key == K_UP:
player_pos = player_pos.move(0, -CELL_SIZE_PX)
if event.type == KEYDOWN:
if event.key == K_RIGHT:
player_pos = player_pos.move(CELL_SIZE_PX, 0)
# New player position
# WINDOW.blit(back_tiles, (0,0))
WINDOW.blit(PLAYER, player_pos)
# Refresh
pygame.display.flip()

BIN
img/1-blue-transp-50.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 264 B

BIN
img/2-blue-transp-50.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 360 B

BIN
img/3-blue-transp-50.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 372 B

BIN
img/4-blue-transp-50.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 308 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 148 KiB

BIN
img/blue-transp-50.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 215 B

BIN
img/blue-white-50.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 216 B

BIN
img/g-orange-transp-50.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 378 B

BIN
img/grey-transp-50.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 360 B

BIN
img/grey-white-50.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 219 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.6 KiB

BIN
img/m-orange-transp-50.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 334 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.3 KiB

BIN
img/player-50.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -1,66 +0,0 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Author: freezed <freezed@users.noreply.github.com> 2018-03-15
Version: 0.1
Licence: `GNU GPL v3` GNU GPL v3: http://www.gnu.org/licenses/
The path of the righteous man is beset on all sides by the iniquities
of the selfish and the tyranny of evil men. Blessed is he who, in the
name of charity and good will, shepherds the weak through the valley of
darkness, for he is truly his brother's keeper and the finder of lost
children. And I will strike down upon thee with great vengeance and
furious anger those who would attempt to poison and destroy My brothers.
And you will know My name is the Lord when I lay My vengeance upon thee.
"""
import pygame
from pygame.locals import *
pygame.init()
#Ouverture de la fenêtre Pygame
fenetre = pygame.display.set_mode((800, 800))
#Chargement et collage du fond
fond = pygame.image.load("img/grid-800.png").convert()
fenetre.blit(fond, (0,0))
#Chargement et collage du personnage
perso = pygame.image.load("img/perso-100.png").convert_alpha()
position_perso = perso.get_rect()
fenetre.blit(perso, position_perso)
#Rafraîchissement de l'écran
pygame.display.flip()
#BOUCLE INFINIE
continuer = 1
while continuer:
for event in pygame.event.get(): #Attente des événements
if event.type == QUIT:
continuer = 0
# Down
if event.type == KEYDOWN:
if event.key == K_DOWN:
position_perso = position_perso.move(0,100)
#
if event.type == KEYDOWN:
if event.key == K_LEFT:
position_perso = position_perso.move(-100,0)
#
if event.type == KEYDOWN:
if event.key == K_UP:
position_perso = position_perso.move(0,-100)
#
if event.type == KEYDOWN:
if event.key == K_RIGHT:
position_perso = position_perso.move(100,0)
#Re-collage
fenetre.blit(fond, (0,0))
fenetre.blit(perso, position_perso)
#Rafraichissement
pygame.display.flip()