Merge pull request 'movement' (#3) from movement into master

Reviewed-on: #3
This commit is contained in:
Paweł Felcyn 2023-03-12 16:51:41 +01:00
commit ca41c47dd9
4 changed files with 47 additions and 3 deletions

12
gameContext.py Normal file
View File

@ -0,0 +1,12 @@
import pygame
class GameContext:
dust_car_speed = 20
dust_car_position_x = 0
dust_car_position_y = 0
stick_man_pygame = None
stick_man_pil = None
canvas = None
def startup(game_context: GameContext):
game_context.canvas.blit(game_context.stick_man_pygame, (game_context.dust_car_position_x, game_context.dust_car_position_y))

View File

@ -1,4 +1,24 @@
import pygame import pygame
from gameContext import GameContext
def handle_game_event(event): def handle_game_event(event, game_context: GameContext):
dust_car_movement(event, game_context)
return return
def dust_car_movement(event, game_context:GameContext):
if event.type != pygame.KEYDOWN:
return
(width, height) = game_context.stick_man_pil.size
if event.key == pygame.K_LEFT:
pygame.draw.rect(game_context.canvas, (0, 0, 0), (game_context.dust_car_position_x, game_context.dust_car_position_y, width, height))
game_context.dust_car_position_x -= game_context.dust_car_speed
elif event.key == pygame.K_RIGHT:
pygame.draw.rect(game_context.canvas, (0, 0, 0), (game_context.dust_car_position_x, game_context.dust_car_position_y, width, height))
game_context.dust_car_position_x += game_context.dust_car_speed
elif event.key == pygame.K_UP:
pygame.draw.rect(game_context.canvas, (0, 0, 0), (game_context.dust_car_position_x, game_context.dust_car_position_y, width, height))
game_context.dust_car_position_y -= game_context.dust_car_speed
elif event.key == pygame.K_DOWN:
pygame.draw.rect(game_context.canvas, (0, 0, 0), (game_context.dust_car_position_x, game_context.dust_car_position_y, width, height))
game_context.dust_car_position_y += game_context.dust_car_speed
game_context.canvas.blit(game_context.stick_man_pygame, (game_context.dust_car_position_x, game_context.dust_car_position_y))

BIN
imgs/stickMan.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

14
main.py
View File

@ -1,11 +1,23 @@
import pygame import pygame
from gameEventHandler import handle_game_event from gameEventHandler import handle_game_event
from gameContext import GameContext
from gameContext import startup
from PIL import Image
pygame.init() pygame.init()
canvas = pygame.display.set_mode((800, 800)) canvas = pygame.display.set_mode((800, 800))
pygame.display.set_caption("Inteligentna śmieciarka") pygame.display.set_caption("Inteligentna śmieciarka")
stick_man_pil = Image.open('imgs/stickMan.jpg')
game_context = GameContext()
game_context.stick_man_pil = stick_man_pil
game_context.stick_man_pygame = pygame.image.frombuffer(stick_man_pil.tobytes(), stick_man_pil.size, 'RGB')
game_context.canvas = canvas
startup(game_context)
exit = False exit = False
while not exit: while not exit:
@ -13,6 +25,6 @@ while not exit:
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
exit = True exit = True
else: else:
handle_game_event(event) handle_game_event(event, game_context)
pygame.display.update() pygame.display.update()