Python Pacman?
import math
import pygame
pygame.init()
#Konstante
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 128)
CYAN = (0, 255, 255)
WIDTH = 500
HIGHT = 500
blockSize = 1
border_Color = (220, 20, 60)
radius = 10
xPos = 30
yPos = 30
Player1_Color = (0, 255, 255)
Player1_Speed = 3
schrittPause = 50
openMouthRight = [45, 315]
openMouthUp = [135, 45]
openMouthLeft = [225, 135]
openMouthDown = [315, 225]
isOpening = False
pause = False
mouthSize = openMouthRight
mouthSpeed = 10
screen = pygame.display.set_mode((WIDTH, HIGHT))
pacman = pygame.draw.arc(screen, Player1_Color, [xPos, yPos, 2 * radius, 2 * radius], math.radians(mouthSize[0]),
math.radians(mouthSize[1]), radius)
clock = pygame.time.Clock()
activeKey = None
def print_hi(name):
# Use a breakpoint in the code line below to debug your script.
print(f'Hi, {name}') # Press Ctrl+F8 to toggle the breakpoint.
def spielfeldLesen():
f = open("spielfeld.txt", "r")
xDim = int(f.readline()) # 15
yDim = int(f.readline()) # 10
mSpielfeld = [[0 for i in range(xDim)] for j in range(yDim)]
# nur zum testen
mSpielfeld [2][1] = 99
for row in range(yDim):
for col in range(xDim):
mSpielfeld[row][col] = int(f.read(1))
f.read(1) # das NL fressen
f.close()
return [xDim, yDim, mSpielfeld]
1 Antwort
def waendeAufbauen(screen, blockSize):
[xD, yD, spielFeldWaende] = spielfeldLesen()
# zaehlen, wie viele Punkte man maximal finden kann
maxPuntkte = 0
for rIdx, rVal in enumerate(spielFeldWaende):
for cIdx, cVal in enumerate(rVal):
if spielFeldWaende[rIdx][cIdx] == 1:
pygame.draw.rect(screen, border_Color, [cIdx*blockSize, rIdx*blockSize, blockSize, blockSize], 0)
elif spielFeldWaende[rIdx][cIdx] == 0:
pygame.draw.circle(screen, WHITE, [cIdx*blockSize + 15, rIdx*blockSize + 15], 5, 0)
maxPuntkte += 1
return [spielFeldWaende, maxPuntkte]
#Baue spielfeld auf
def draw_Spielfeld(captionString):
pygame.init()
screen
pygame.display.set_caption(captionString)
# hier kommen dann die ganzen Waende
[spf, maxPunkte] = waendeAufbauen(screen, 30)
return [screen, spf, maxPunkte]
#Tasten eingaben Check
def checkKey():
global activeKey
global mouthSize
print("Spieler hat Taste gedrückt")
#Ausgabe von Welche Taste gedrückt wurde + festlegung der Mund Richtung
if event.key == pygame.K_s or event.key == pygame.K_DOWN:
activeKey = pygame.K_s
mouthSize = openMouthDown
elif event.key == pygame.K_w or event.key == pygame.K_UP:
activeKey = pygame.K_w
mouthSize = openMouthUp
elif event.key == pygame.K_a or event.key == pygame.K_LEFT:
activeKey = pygame.K_a
mouthSize = openMouthLeft
elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
activeKey = pygame.K_d
mouthSize = openMouthRight
elif event.key == pygame.K_p or event.key == pygame.K_ESCAPE:
activeKey = None
print("Pause")
print(f"Aktuelle Position: {xPos},{yPos}")
#Bewgung Spieler 1
def movePlayer():
global xPos
global yPos
if activeKey == pygame.K_s:
yPos += Player1_Speed
elif activeKey == pygame.K_w:
yPos -= Player1_Speed
elif activeKey == pygame.K_a:
xPos -= Player1_Speed
elif activeKey == pygame.K_d:
xPos += Player1_Speed
elif activeKey == pygame.K_p:
gameText()
screen
if __name__ == '__main__':
[screenMain, spielfeld, maxPunkte] = draw_Spielfeld("Unsere SuperDuperPacWoman")
clock
pygame.display.update()
# solange die Variable True ist, soll das Spiel laufen
spielaktiv = True
# Schleife Hauptprogramm
while spielaktiv:
for event in pygame.event.get():
if event.type == pygame.QUIT:
spielaktiv = False
print("Spieler hat Quit-Button angeklickt")
elif event.type == pygame.KEYDOWN:
checkKey()
elif event.type == pygame.MOUSEBUTTONDOWN:
print("Spieler hast Maus angeklickt")
if activeKey != None:
movePlayer()
pygame.quit()