movement #3

Merged
s473616 merged 4 commits from movement into master 2023-03-12 16:51:41 +01:00
3 changed files with 33 additions and 8 deletions
Showing only changes of commit 853b025a84 - Show all commits

View File

@ -1,5 +1,12 @@
import pygame
class GameContext:
dust_car_speed = 20
dust_car_position_x = 0
dust_car_position_y = 0
stick_man_pygame = None
canvas = 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

@ -2,8 +2,23 @@ import pygame
from gameContext import GameContext
def handle_game_event(event, game_context: GameContext):
update_canvas(game_context)
dust_car_movement(event, game_context)
return
def update_canvas(game_context: GameContext):
game_context.canvas.blit(game_context.stick_man_pygame, (game_context.dust_car_position_x, game_context.dust_car_position_y))
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))

11
main.py
View File

@ -1,6 +1,7 @@
import pygame
from gameEventHandler import handle_game_event
from gameContext import GameContext
from gameContext import startup
from PIL import Image
pygame.init()
@ -10,9 +11,11 @@ canvas = pygame.display.set_mode((800, 800))
pygame.display.set_caption("Inteligentna śmieciarka")
stick_man_pil = Image.open('imgs/sickMan.jpg')
gameContext = GameContext()
gameContext.stick_man_pygame = pygame.image.frombuffer(stick_man_pil.tobytes(), stick_man_pil.size, 'RGB')
gameContext.canvas = canvas
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
@ -22,6 +25,6 @@ while not exit:
if event.type == pygame.QUIT:
exit = True
else:
handle_game_event(event, gameContext)
handle_game_event(event, game_context)
pygame.display.update()