Files
pacman/main.py
2024-07-13 13:22:48 +02:00

188 lines
5.3 KiB
Python

import pygame
import subprocess
import random
pygame.init()
MAINSCREEN_SIZE = (1440,900)
MAINSCREEN = pygame.display.set_mode(MAINSCREEN_SIZE, pygame.RESIZABLE)
#MAINSCREEN = pygame.display.set_mode(MAINSCREEN_SIZE, pygame.FULLSCREEN)
CENTER_X = MAINSCREEN_SIZE[0]/2
CENTER_Y = MAINSCREEN_SIZE[1]/2
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"))
#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()
def generate_map(*mapfile):
#Generate row
startpos_x = 0
startpos_y = 0
id = 0
for line in map_file:
#print(line)
for byte in line:
#print(byte)
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({"x":startpos_x, "y":startpos_y})
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
#print(line)
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}")
pacman_posx += movement_map[0]["x"]+movement_map_center
pacman_posy += movement_map[0]["y"]+movement_map_center
count = 0
count2 = 0
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):
if (event.key == pygame.K_UP):
movedirection["up"] = True
movedirection["down"] = False
print("hoch")
if (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_DOWN):
movedirection["down"] = True
movedirection["up"] = False
print("runter")
if (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_LEFT):
movedirection["left"] = True
movedirection["right"] = False
print("links")
if (event.type == pygame.KEYDOWN):
if (event.key == pygame.K_RIGHT):
movedirection["right"] = True
movedirection["left"] = False
print("rechts")
#TODO Check for wall-collision!!!!
if(movedirection["up"] == True):
if(pacman_posy >= 0):
pacman_posy -= pacman_movespeed
if(movedirection["down"] == True):
if(pacman_posy <= (MAINSCREEN_SIZE[1]-pacman_size)):
pacman_posy += pacman_movespeed
if(movedirection["right"] == True):
if(pacman_posx <= (MAINSCREEN_SIZE[0]-pacman_size)):
pacman_posx += pacman_movespeed
if(movedirection["left"] == True):
if(pacman_posx >= 0):
pacman_posx -= pacman_movespeed
#print(pacman_animation)
MAINSCREEN.blit(pacman_animation[count], (pacman_posx,pacman_posy))
count += 1
if count == 5: count = 0
for row in map1:
#print(row)
MAINSCREEN.blit(row["object"], (row["x"],row["y"]))
for index, item in enumerate(movedirection):
if(movedirection[item]==True):
print(F"Pacman-Position: x:{pacman_posx} y:{pacman_posy}")
#Adds visual character for vaild movementpath (DEBUG ONLY)
for coord in movement_map:
tmp = font1.render("X", 1, ROT)
MAINSCREEN.blit(tmp, (coord["x"], coord["y"]))
pacman_pos_label = font1.render(F"x:{pacman_posx} y:{pacman_posy}", 1, SCHWARZ)
fps_label = font1.render(F"FPS: {int(clock.get_fps())}", 1, SCHWARZ)
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)
count2 += 1
if(count2 == 60):
count2 = 0