My player doesn't move and my timer doesn't count down like it should. I'll post my whole code, so you can recreate everything I have. The timer stuff is inside the main_loop_state_config right after the for loop.
import pygame, os
from pygame.locals import *
import random
###############################################################
# Color-Codes
###############################################################
ORANGE = (255, 140, 0)
RED = (255, 0, 0)
DARKBLUE = (0, 0, 139)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
GREEN = (0, 255, 0)
DARKGREEN = (0, 98, 7)
LIMEGREEN = (50, 205, 50)
DARKGREY = (70, 70, 70)
BLUE = (0, 0, 255)
LIGHTBLUE = (173, 216, 230)
WOODY = (139,69,19)
LIST_OF_ALL_COLOR_NAMES = [ ORANGE, RED, DARKBLUE, WHITE, BLACK, YELLOW, GREEN, DARKGREEN, LIMEGREEN,
DARKGREY, BLUE, LIGHTBLUE ]
###############################################################
# Konstanten
###############################################################
SCREEN_WIDTH = 1200
SCREEN_HEIGHT = 800
PANEL_SIZE = 160
SQUARE_SIZE = 80
#SIDEPANEL = pygame.Rect(pxl(13), pxl(0), pxl(2), pxl(10))
background = pygame.Rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
###############################################################
# Measurement -> Maßeinheit und Bildschirmmaße
###############################################################
def pxl (number_of_squares):
"number of squares -> number of pixels"
return SQUARE_SIZE * number_of_squares
###############################################################
# Variablen
###############################################################
level = 0
a = 1085
b = 80
gameDrawn = False
counter = 15
timerRect = pygame.Rect(pxl(13), pxl(0), pxl(1), pxl(1))
counterRect = pygame.Rect(pxl(14), pxl(0), pxl(3), pxl(1))
###############################################################
# Classes -> Klassen
###############################################################
class Wall(object):
def __init__(self, pos):
self.rect = pygame.Rect(pos[0], pos[1], pxl(1), pxl(1))
class End(object):
def __init__(self, pos):
self.rect = pygame.Rect(pos[0], pos[1], pxl(1), pxl(1))
class Player(object):
def __init__(self, pos):
self.rect = pygame.Rect(pos[0], pos[1], pxl(1), pxl(1))
#def move(dx, dy):
# Bewegung für x und y. Kollision wird überprüft
#if dx != 0:
#wallCollisionDetection((dx, 0))
#if dy != 0:
#wallCollisionDetection(0, dy)
def move(self, dx, dy):
walls = drawLevel(level)
# Den Spieler bewegen
self.rect.x += dx
self.rect.y += dy
for wall in walls:
if self.rect.colliderect(wall.rect):
if dx > 0:
self.rect.right = wall.rect.left
if dx < 0:
self.rect.left = wall.rect.right
if dy > 0:
self.rect.bottom = wall.rect.top
if dy < 0:
self.rect.top = wall.rect.bottom
#def endCollisionDetection(self, dx, dy):
###############################################################
# Listen
###############################################################
move_list = []
max_move_list = [17]
levels =[[ "WWWWWWWWWWWWW",
"W EW",
"W WWW",
"W WWWW W",
"W W W",
"WWWWW WWWW W",
"W W W W",
"W W W W",
"WP W",
"WWWWWWWWWWWWW",
]]
curr_moves = max_move_list[level]
###############################################################
# Methoden
###############################################################
def movePlayer(move_list):
for i in move_list:
if i == 1:
player.move(pxl(0), pxl(-1))
elif i == 2:
player.move(pxl(0), pxl(1))
elif i == 3:
player.move(pxl(1), pxl(0))
elif i == 4:
player.move(pxl(-1), pxl(0))
def drawGrid():
for x in range(0, SCREEN_WIDTH - PANEL_SIZE, SQUARE_SIZE):
for y in range(0, SCREEN_HEIGHT, SQUARE_SIZE):
rect = pygame.Rect(x, y, SQUARE_SIZE, SQUARE_SIZE)
pygame.draw.rect(screen, WHITE, rect, 1)
def drawRect():
pygame.draw.rect(screen, WHITE, (pygame.Rect(pxl(13), pxl(0), pxl(2), pxl(10))))
def drawStartMessage():
startText11 = "Das Spiel funktioniert so:"
startText22 = "Sobald es startet, haben sie 15 Sekunden Zeit um ihre Züge einzugeben."
startText33 = "Das Ziel ist es den Spieler (blaues Rechteck) ins Ziel (rotes Rechteck) zu bekommen."
startText44 = "Dazu haben sie neben den 15 Sekunden eine gewisse Anzahl an Zügen."
startText55 = "Drücken sie eine Taste zum Starten"
startText1 = font.render(startText11, True, BLACK)
startText2 = font.render(startText22, True, BLACK)
startText3 = font.render(startText33, True, BLACK)
startText4 = font.render(startText44, True, BLACK)
startText5 = font.render(startText55, True, BLACK)
startText1_rect = startText1.get_rect(center=(SCREEN_WIDTH/2, 160))
startText2_rect = startText2.get_rect(center=(SCREEN_WIDTH/2, 240))
startText3_rect = startText3.get_rect(center=(SCREEN_WIDTH/2, 320))
startText4_rect = startText4.get_rect(center=(SCREEN_WIDTH/2, 400))
startText5_rect = startText5.get_rect(center=(SCREEN_WIDTH/2, 640))
screen.blit(startText1, startText1_rect)
screen.blit(startText2, startText2_rect)
screen.blit(startText3, startText3_rect)
screen.blit(startText4, startText4_rect)
screen.blit(startText5, startText5_rect)
def drawLevel(level):
global gameDrawn
x = y = 0
walls = []
ends = []
players = []
if gameDrawn == False:
screen.fill(WOODY)
drawGrid()
drawRect()
for row in levels[level]:
for col in row:
if col == "W":
wall = Wall((x, y))
walls.append(wall)
if col == "E":
end = End((x, y))
ends.append(end)
if col == "P":
player = Player((x,y))
players.append(player)
x += 80
y += 80
x = 0
for wall in walls:
pygame.draw.rect(screen, BLACK, wall.rect)
for end in ends:
pygame.draw.rect(screen, RED, end.rect)
for player in players:
pygame.draw.rect(screen, BLUE, player.rect)
gameDrawn = True
#elif gameDrawn == True:
#for event in pygame.event.get():
#if event.type == pygame.USEREVENT:
#drawTimer(counter)
#counter -= 1
return players, walls, ends
#def drawPlayerPath():
#def getPlayerPath():
# players, _, _ = drawLevel(level)
# player_x, player_y = players[0]
# for i in move_list:
# if i == 1:
# player_y += -1
# elif i == 2:
# player_y += 1
# elif i == 3:
# player_x += -1
# elif i == 4:
# player_x += -1
# players.append((player_x, player_y))
def calcCounter():
global curr_moves
curr_moves += 1
return curr_moves
def drawCounter(curr_moves):
global screen
text_currMoves = font.render(str(curr_moves) + "/" + str(max_move_list[level]), True, BLACK)
pygame.draw.rect(screen, WHITE, counterRect)
screen.blit(text_currMoves, (1130,40))
if curr_moves == 0:
state = "running"
init_state = True
def writeMoves():
a = 1085
b = 80
for i in range(len(move_list)):
if move_list[i] == 1:
screen.blit(text_up, (a, b))
elif move_list[i] == 2:
screen.blit(text_down, (a, b))
elif move_list[i] == 3:
screen.blit(text_right, (a, b))
elif move_list[i] == 4:
screen.blit(text_left, (a, b))
b += 40
def wallCollisionDetection(player, wall, dx, dy):
walls = drawLevel(level)
players = drawLevel(level)
for wall in walls:
if player.rect.colliderect(wall.rect):
if dx > 0:
player.rect.right = wall.rect.left
if dx < 0:
player.rect.left = wall.rect.right
if dy > 0:
player.rect.bottom = wall.rect.top
if dy < 0:
player.rect.top = wall.rect.bottom
pygame.init()
clock = pygame.time.Clock()
pygame.time.set_timer(pygame.USEREVENT, 1000)
font = pygame.font.SysFont('Consolas', 30)
###############################################################
# Game-States
###############################################################
def init_state_start():
pass
def main_loop_state_start():
global state, init_state
for event in pygame.event.get():
if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
pygame.quit()
print("Spiel wird beendet!")
# User-Input wird erfasst
elif event.type == KEYDOWN:
state = "config"
init_state = True
# draw screen -> Startbildschirm wird "gemalt"
screen.fill(LIGHTBLUE)
drawStartMessage()
def init_state_config():
pass
def main_loop_state_config():
global level, end_rect, curr_moves, max_moves, i, j, counter
pygame.time.set_timer(pygame.USEREVENT, 1500)
drawLevel(level)
players, walls, ends = drawLevel(level)
for event in pygame.event.get():
if event.type == pygame.USEREVENT:
counter -= 1
counterT = font.render(str(counter), True, BLACK)
screen.blit(counterT, (1070, 40))
if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
pygame.quit()
print("Spiel wird beendet!")
elif event.type == KEYDOWN:
if event.key == K_UP:
move_list.append(1)
curr_moves -= 1
elif event.key == K_DOWN:
move_list.append(2)
curr_moves -= 1
elif event.key == K_RIGHT:
move_list.append(3)
curr_moves -= 1
elif event.key == K_LEFT:
move_list.append(4)
curr_moves -= 1
if counter == 0:
state = "running"
init_state = True
drawCounter(curr_moves)
writeMoves()
pygame.display.flip()
def init_state_running():
pass
def main_loop_state_running():
global level
players, _, _ = drawLevel(level)
for event in pygame.event.get():
if event.type == QUIT or event.type == KEYDOWN and event.key == K_ESCAPE:
running = False
pygame.quit()
print("Spiel wird beendet!")
if event.type == KEYDOWN and event.key == K_SPACE:
print(move_list)
for player in players:
movePlayer(move_list)
###############################################################
# Measurement -> Maßeinheit und Bildschirmmaße
###############################################################
def pxl (number_of_squares):
"number of squares -> number of pixels"
return SQUARE_SIZE * number_of_squares
###############################################################
# Font and Texts
###############################################################
font = pygame.font.SysFont("Consolas", 20)
moveFont = pygame.font.SysFont(None, 30) # Schrift für Züge
startFont = pygame.font.SysFont(None, 40) # Schrift für den Startbildschirm
timerFont = pygame.font.SysFont(None, 30) # Schrift für den Timer
text_up = font.render("OBEN", True, BLACK)
text_down = font.render("UNTEN", True, BLACK)
text_right = font.render("RECHTS", True, BLACK)
text_left = font.render("LINKS", True, BLACK)
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) # Fensterbreite und länge in pixeln, nur der Zeichenbereich
###############################################################
# Generelles Setup
###############################################################
# Schnelligkeit des Spiels festlegen, damit es überall gleich schnell läuft
state = "start"
init_state = True
###############################################################
# Screen-Settings
###############################################################
pygame.display.set_caption("Pygame")
###############################################################
# Main-Game-Loop
###############################################################
running = True
while running:
if state == "start":
if init_state:
init_state_start()
init_state = False
else:
main_loop_state_start()
elif state == "config":
if init_state:
init_state_config()
init_state = False
else:
main_loop_state_config()
if counter == 0:
state = "running"
init_state = True
if curr_moves == 0:
state = "running"
init_state = True
elif state == "running":
if init_state:
init_state_running()
init_state = False
else:
main_loop_state_running()
# update window
clock.tick(60) # set refresh rate
pygame.display.flip()
###############################################################
It would be really amazing if you could help me and explain the error to me, since I have to do that stuff for university. Thank you really much :D