Files
pacman/main.py
Christian Bobe 9b7e03b865 Added collision check for gameobjects
Some cleanup
2024-07-21 22:13:32 +02:00

186 lines
5.4 KiB
Python

import pygame
import subprocess
import random
from modules.gameobjects import *
pygame.init()
MAINSCREEN_SIZE = (1440,900)
MAINSCREEN = pygame.display.set_mode(MAINSCREEN_SIZE, pygame.RESIZABLE)
#Setup for fullscreen
#MAINSCREEN = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
CENTER_X = MAINSCREEN_SIZE[0]/2
CENTER_Y = MAINSCREEN_SIZE[1]/2
debug_mode = True
pygame.display.set_caption("PACMAN")
clock = pygame.time.Clock()
WEISS = (255,255,255)
SCHWARZ = (0,0,0)
GRUEN = (0,255,0)
ROT = (255,0,0)
font1 = pygame.font.SysFont("Noto Sans 10pt", 25)
PLAYGROUND = pygame.Surface((1024,728))
gamestate = True
#Mapfiles for Mapgen
map_file = open("maps/map_1.txt")
map_texture_map1 = pygame.image.load("models/wall_1.png")
map_space_size = 30
map_wall_size = 30
#PACMAN_CREATURE_CONSTANTS
pacman = pygame.image.load("models/pacman_1.png")
pacman_posx = 0
pacman_posy = 0
pacman_movespeed = 5
pacman_startpoint = {0,0}
pacman_size = 26
pacman_animation = list()
pacman_animation.append(pygame.image.load("models/pacman_1_1.png"))
pacman_animation.append(pygame.image.load("models/pacman_1_2.png"))
pacman_animation.append(pygame.image.load("models/pacman_1_3.png"))
pacman_animation.append(pygame.image.load("models/pacman_1_4.png"))
pacman_animation.append(pygame.image.load("models/pacman_1_5.png"))
model1 = "models/pacman_1_1.png"
#ITEMS
fruits = list()
fruits.append(pygame.image.load("models/fruit_1_1_scaled_2.png"))
fruits_size = fruits[0].get_size()
#TODO Pacman Animation has to been added
#Labels
short_hash = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode('ascii').strip()
hash_label = font1.render(F"git-hash: {short_hash}", 1, SCHWARZ)
#Movement
movedirection = {"up":False, "down":False, "left":False, "right":False}
map1 = list()
movement_map = list()
pacman = GameObject("Test", 600, 600, 50,50, model1 ,MAINSCREEN)
def generate_map(*mapfile):
#Generate row
startpos_x = 0
startpos_y = 0
id = 0
for line in map_file:
for byte in line:
if(byte == "#"):
map1.append({"id":id,"object": pygame.image.load("models/wall_1.png"), "x":startpos_x, "y":startpos_y})
if(byte == " "):
movement_map.append({"id":id,"object": pygame.image.load("models/wall_1.png"), "x":startpos_x, "y":startpos_y})
startpos_x += map_wall_size
id += 1
startpos_x = 0
startpos_y += map_wall_size
generate_map()
movement_map_center = (map_space_size-pacman_size)/2
print(F"Center correction: {movement_map_center}")
#Spawn pacman on top of the map
pacman.set_position(movement_map[0]["x"]+movement_map_center, movement_map[0]["y"]+movement_map_center)
while(gamestate==True):
MAINSCREEN.fill(WEISS)
#PLAYGROUND.fill(WEISS)
for event in pygame.event.get():
if (event.type == pygame.QUIT):
print("Programm wird geschlossen!")
gamestate = False
if (event.type == pygame.KEYDOWN):
match event.key:
case pygame.K_ESCAPE:
print("ESC... stopping Game!")
gamestate = False
case pygame.K_UP:
movedirection["up"] = True
movedirection["down"] = False
print("hoch")
case pygame.K_DOWN:
movedirection["down"] = True
movedirection["up"] = False
print("runter")
case pygame.K_RIGHT:
movedirection["right"] = True
movedirection["left"] = False
print("rechts")
case pygame.K_LEFT:
movedirection["left"] = True
movedirection["right"] = False
print("links")
#TODO Check for wall-collision!!!!
if(movedirection["up"] == True):
if(pacman.get_position()[1] >= 0):
pacman.move(0, -pacman_movespeed)
if(movedirection["down"] == True):
if(pacman.get_position()[1] <= (MAINSCREEN_SIZE[1]-pacman_size)):
pacman.move(0, pacman_movespeed)
if(movedirection["right"] == True):
if(pacman.get_position()[0] <= (MAINSCREEN_SIZE[0]-pacman_size)):
pacman.move(pacman_movespeed, 0)
if(movedirection["left"] == True):
if(pacman.get_position()[0] >= 0):
pacman.move(-pacman_movespeed, 0)
pacman.draw()
for row in map1:
MAINSCREEN.blit(row["object"], (row["x"],row["y"]))
if debug_mode:
#Adds visual character for vaild movementpath (DEBUG ONLY)
for coord in movement_map:
coord:list
tmp = font1.render("X", 1, ROT)
result = MAINSCREEN.blit(tmp, (coord["x"], coord["y"]))
#Removes collided objects from list
if(pacman.check_collision(result) and len(movement_map) >= 1):
print(len(movement_map))
print(coord)
movement_map.remove(coord)
pass
pacman_pos_label = font1.render(F"x:{pacman.get_position()[0]} y:{pacman.get_position()[1]}", 1, SCHWARZ)
fps_label = font1.render(F"FPS: {int(clock.get_fps())}", 1, SCHWARZ)
if debug_mode:
MAINSCREEN.blit(pacman_pos_label, (CENTER_X,MAINSCREEN_SIZE[1]-40))
MAINSCREEN.blit(fps_label, (CENTER_X - 150,MAINSCREEN_SIZE[1]-40))
MAINSCREEN.blit(hash_label, (10, MAINSCREEN_SIZE[1]-40))
pygame.display.flip()
clock.tick(60)