add stick man movement
This commit is contained in:
parent
9f7bc7f3e1
commit
853b025a84
@ -1,5 +1,12 @@
|
|||||||
|
import pygame
|
||||||
|
|
||||||
class GameContext:
|
class GameContext:
|
||||||
|
dust_car_speed = 20
|
||||||
dust_car_position_x = 0
|
dust_car_position_x = 0
|
||||||
dust_car_position_y = 0
|
dust_car_position_y = 0
|
||||||
stick_man_pygame = None
|
stick_man_pygame = None
|
||||||
|
stick_man_pil = None
|
||||||
canvas = 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))
|
||||||
|
@ -2,8 +2,23 @@ import pygame
|
|||||||
from gameContext import GameContext
|
from gameContext import GameContext
|
||||||
|
|
||||||
def handle_game_event(event, game_context: GameContext):
|
def handle_game_event(event, game_context: GameContext):
|
||||||
update_canvas(game_context)
|
dust_car_movement(event, game_context)
|
||||||
return
|
return
|
||||||
|
|
||||||
def update_canvas(game_context: GameContext):
|
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))
|
game_context.canvas.blit(game_context.stick_man_pygame, (game_context.dust_car_position_x, game_context.dust_car_position_y))
|
11
main.py
11
main.py
@ -1,6 +1,7 @@
|
|||||||
import pygame
|
import pygame
|
||||||
from gameEventHandler import handle_game_event
|
from gameEventHandler import handle_game_event
|
||||||
from gameContext import GameContext
|
from gameContext import GameContext
|
||||||
|
from gameContext import startup
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
pygame.init()
|
pygame.init()
|
||||||
@ -10,9 +11,11 @@ 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/sickMan.jpg')
|
stick_man_pil = Image.open('imgs/sickMan.jpg')
|
||||||
|
|
||||||
gameContext = GameContext()
|
game_context = GameContext()
|
||||||
gameContext.stick_man_pygame = pygame.image.frombuffer(stick_man_pil.tobytes(), stick_man_pil.size, 'RGB')
|
game_context.stick_man_pil = stick_man_pil
|
||||||
gameContext.canvas = canvas
|
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
|
||||||
@ -22,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, gameContext)
|
handle_game_event(event, game_context)
|
||||||
|
|
||||||
pygame.display.update()
|
pygame.display.update()
|
Loading…
Reference in New Issue
Block a user