Compare commits

..

5 Commits

Author SHA1 Message Date
ca41c47dd9 Merge pull request 'movement' (#3) from movement into master
Reviewed-on: #3
2023-03-12 16:51:41 +01:00
Pawel Felcyn
b8b2e5e319 rename stick man file 2023-03-12 16:49:32 +01:00
Pawel Felcyn
853b025a84 add stick man movement 2023-03-12 16:47:18 +01:00
Pawel Felcyn
9f7bc7f3e1 display stick man in window 2023-03-12 16:08:54 +01:00
Pawel Felcyn
03e2aea285 add stick man img 2023-03-11 15:17:35 +01:00
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
from gameContext import GameContext
def handle_game_event(event):
return
def handle_game_event(event, game_context: GameContext):
dust_car_movement(event, game_context)
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
from gameEventHandler import handle_game_event
from gameContext import GameContext
from gameContext import startup
from PIL import Image
pygame.init()
canvas = pygame.display.set_mode((800, 800))
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
while not exit:
@ -13,6 +25,6 @@ while not exit:
if event.type == pygame.QUIT:
exit = True
else:
handle_game_event(event)
handle_game_event(event, game_context)
pygame.display.update()