garbageCan

This commit is contained in:
Wiktor Szynaka 2023-03-26 14:33:33 +02:00
commit cb890c028c
9 changed files with 72 additions and 47 deletions

View File

@ -1,2 +1,3 @@
# sztuczna_inteligencja_2023_smieciarka
Symulacja inteligentnej śmieciarki. Śmieciarka zbiera śmieci z kubłów wystawionych przez mieszkańców, samodzielnie je segreguje i zawozi na wysypisko.

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
dust_car_pygame = None
dust_car_pil = None
canvas = None
def startup(game_context: GameContext):
game_context.canvas.blit(game_context.dust_car_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.dust_car_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.dust_car_pygame, (game_context.dust_car_position_x, game_context.dust_car_position_y))

21
garbage.py Normal file
View File

@ -0,0 +1,21 @@
from enum import Enum
class GarbageType (Enum):
PAPER = 0,
PLASTIC_AND_METAL = 1
GLASS = 3
BIO = 4
MIXED = 5
class Garbage:
img: str
def __init__(self, img: str) -> None:
self.img = img
class RecognizedGarbage (Garbage):
garbage_type: GarbageType
def __init__(self, src: Garbage, garbage_type: GarbageType) -> None:
super().__init__(src.img)
self.garbage_type = garbage_type

16
garbageCan.py Normal file
View File

@ -0,0 +1,16 @@
from garbage import RecognizedGarbage
from typing import List, Tuple
class GarbageCan:
position: Tuple[float, float]
garbage: List[RecognizedGarbage]
def __init__(self, position: Tuple[float, float]) -> None:
self.position = position
self.garbage = []
def add_garbage(self, garbage: RecognizedGarbage) -> None:
self.garbage.append(garbage)
def remove_garbage(self, garbage: RecognizedGarbage) -> None:
self.garbage.remove(garbage)

BIN
imgs/container.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 331 B

BIN
imgs/dust_car.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 348 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.3 KiB

45
main.py
View File

@ -1,45 +0,0 @@
from gameEventHandler import handle_game_event
import pygame
from PIL import Image
pygame.init()
window_size = (800, 600)
screen = pygame.display.set_mode(window_size)
# wczytanie mapy i ludzika (jezeli chcemy robic to bez przeksztłcania pilem i uzyc samego pygame to trzeba miec img w tym samym
# folderze co gra)
mapa_pil = Image.open('imgs/house.jpg')
mapa_pygame = pygame.image.frombuffer(mapa_pil.tobytes(), mapa_pil.size, 'RGB')
ludzik_pil = Image.open('imgs/a.jpg')
ludzik_pygame = pygame.image.frombuffer(ludzik_pil.tobytes(), ludzik_pil.size, 'RGB')
# pozycja ludzika
ludzik_x = 0
ludzik_y = 0
# główna pętla gry
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
ludzik_x -= 10
elif event.key == pygame.K_RIGHT:
ludzik_x += 10
elif event.key == pygame.K_UP:
ludzik_y -= 10
elif event.key == pygame.K_DOWN:
ludzik_y += 10
screen.fill((255, 255, 255))
# wyświetlenie mapy i ludzika
screen.blit(mapa_pygame, (0, 0))
screen.blit(ludzik_pygame, (ludzik_x, ludzik_y))
# odświeżenie ekranu
pygame.display.update()