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.
|
@ -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()
|
After Width: | Height: | Size: 264 B |
After Width: | Height: | Size: 360 B |
After Width: | Height: | Size: 372 B |
After Width: | Height: | Size: 308 B |
Before Width: | Height: | Size: 148 KiB |
After Width: | Height: | Size: 215 B |
After Width: | Height: | Size: 216 B |
After Width: | Height: | Size: 378 B |
After Width: | Height: | Size: 360 B |
After Width: | Height: | Size: 219 B |
BIN
img/grid-800.png
Before Width: | Height: | Size: 3.6 KiB |
After Width: | Height: | Size: 334 B |
Before Width: | Height: | Size: 3.6 KiB |
BIN
img/perso.png
Before Width: | Height: | Size: 9.3 KiB |
After Width: | Height: | Size: 1.4 KiB |
|
@ -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()
|