feat: Added basic program loop

This commit is contained in:
Jager72 2024-03-13 23:25:49 +01:00
commit f65bbc1b79
11 changed files with 338 additions and 0 deletions

160
.gitignore vendored Normal file
View File

@ -0,0 +1,160 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

BIN
Chef64new.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

1
READ.ME Normal file
View File

@ -0,0 +1 @@
Projekt na Sztuczną Inteligencje, semstr letnim, 2024

70
app.py Normal file
View File

@ -0,0 +1,70 @@
import pygame
import prefs
import random
from pygame.locals import K_w, K_s, K_a, K_d
from classes.cell import Cell
from classes.agent import Agent
pygame.init()
window = pygame.display.set_mode((prefs.WIDTH, prefs.HEIGHT))
pygame.display.set_caption("Game Window")
def initBoard():
global cells
cells = []
for i in range(prefs.GRID_SIZE):
row = []
for j in range(prefs.GRID_SIZE):
cell = Cell(i, j)
row.append(cell)
cells.append(row)
global agent
agent = Agent(prefs.SPAWN_POINT[0], prefs.SPAWN_POINT[1], cells)
# Na potrzeby prezentacji tworzę sobie prostokatne sciany na które nie da się wejść
x1 = 3
y1 = 2
for i in range(x1, x1+4):
for j in range(y1, y1+2):
cells[i][j].prepareTexture("wall.png")
cells[i][j].blocking_movement = True
def draw_grid(window, cells):
for i in range(prefs.GRID_SIZE):
for j in range(prefs.GRID_SIZE):
cells[i][j].draw(window)
initBoard()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# takie głupie kontrolki do usunięcia potem, tylko do preznetacji
keys = pygame.key.get_pressed()
if keys[K_w] and not agent.moved:
agent.move_up()
if keys[K_s] and not agent.moved:
agent.move_down()
if keys[K_a] and not agent.moved:
agent.move_left()
if keys[K_d] and not agent.moved:
agent.move_right()
if not any([keys[K_w], keys[K_s], keys[K_a], keys[K_d]]):
agent.moved = False
window.fill((255, 0, 0))
draw_grid(window, cells)
agent.draw(window)
pygame.display.update()
pygame.quit()

38
classes/agent.py Normal file
View File

@ -0,0 +1,38 @@
import pygame
import prefs
class Agent:
def __init__(self, x, y, cells):
self.sprite = pygame.image.load("Chef64new.png").convert_alpha()
self.sprite = pygame.transform.scale(self.sprite, (prefs.CELL_SIZE, prefs.CELL_SIZE))
self.current_cell = cells[x][y]
self.moved=False
self.last_move_time=pygame.time.get_ticks()
self.cells = cells
def move_up(self):
if pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.Y > 0 and not self.cells[self.current_cell.X][self.current_cell.Y-1].blocking_movement:
self.current_cell = self.cells[self.current_cell.X][self.current_cell.Y-1]
self.moved=True
self.last_move_time=pygame.time.get_ticks()
def move_down(self):
if pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.Y < prefs.GRID_SIZE-1 and not self.cells[self.current_cell.X][self.current_cell.Y+1].blocking_movement:
self.current_cell = self.cells[self.current_cell.X][self.current_cell.Y+1]
self.moved=True
self.last_move_time=pygame.time.get_ticks()
def move_left(self):
if pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.X > 0 and not self.cells[self.current_cell.X-1][self.current_cell.Y].blocking_movement:
self.current_cell = self.cells[self.current_cell.X-1][self.current_cell.Y]
self.moved=True
self.last_move_time=pygame.time.get_ticks()
def move_right(self):
if pygame.time.get_ticks()-self.last_move_time > 125 and self.current_cell.X < prefs.GRID_SIZE-1 and not self.cells[self.current_cell.X+1][self.current_cell.Y].blocking_movement:
self.current_cell = self.cells[self.current_cell.X+1][self.current_cell.Y]
self.moved=True
self.last_move_time=pygame.time.get_ticks()
def draw(self, surface):
surface.blit(self.sprite, (self.current_cell.X * prefs.CELL_SIZE,
self.current_cell.Y * prefs.CELL_SIZE))

25
classes/cell.py Normal file
View File

@ -0,0 +1,25 @@
import pygame
import prefs
class Cell:
def __init__(self, x, y, blocking_movement=False, interactableItem=False, texture=None):
self.X = x
self.Y = y
self.rect = pygame.Rect(x * prefs.CELL_SIZE, y * prefs.CELL_SIZE, prefs.CELL_SIZE, prefs.CELL_SIZE)
self.blocking_movement = blocking_movement
self.interactableItem = interactableItem
if texture:
self.prepareTexture(texture)
else:
self.texture = texture
def prepareTexture(self, texture):
preparedTexture = pygame.image.load(texture).convert_alpha()
preparedTexture = pygame.transform.scale(preparedTexture, (prefs.CELL_SIZE, prefs.CELL_SIZE))
self.texture = preparedTexture
def draw(self,window):
if(self.texture):
window.blit(self.texture, self.rect)
else:
pygame.draw.rect(window, prefs.COLORS[(self.X*prefs.GRID_SIZE+self.Y)%len(prefs.COLORS)], self.rect)
pygame.draw.line(window, (0, 0, 0), (self.rect.left, self.rect.top), (self.rect.right, self.rect.top))
pygame.draw.line(window, (0, 0, 0), (self.rect.left, self.rect.top), (self.rect.left, self.rect.bottom))

7
prefs.py Normal file
View File

@ -0,0 +1,7 @@
WIDTH = 600
HEIGHT = WIDTH
GRID_SIZE = 12
CELL_SIZE = WIDTH // GRID_SIZE
SPAWN_POINT = (5, 5)
COLORS = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255), (128, 0, 128), (255, 165, 0), (0, 128, 128), (128, 128, 0)]

BIN
sprites/Chef64new.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
sprites/wall.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 711 B

37
test.py Normal file
View File

@ -0,0 +1,37 @@
# Assuming you have an image file named "sprite.png" in the same directory
import pygame
# Initialize Pygame
pygame.init()
# Set up the display
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Sprite Example")
# Load the image
sprite_image = pygame.image.load("Chef64new.png").convert_alpha()
# Scale the image to 200x200
sprite_image = pygame.transform.scale(sprite_image, (200, 200))
# Get the rect of the sprite image for positioning
sprite_rect = sprite_image.get_rect()
# Main game loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Clear the screen
screen.fill((255, 255, 255))
# Draw the sprite image onto the screen at position (250, 150)
screen.blit(sprite_image, (250, 150))
# Update the display
pygame.display.flip()
pygame.quit()
pygame.quit()

BIN
wall.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 711 B