Hello i need to programm a Game with pygame for school. I nearly finish the Game but when i want to Press the "C" button in the Gameover loop then nothing happends and the game will not restart. unti i spam the "C" Button. I dont think that there is a problem with the detection of pressing the button because i made a print command thatalways prints something when i press the "C" Button and it always print something out. I hope you can help me with the Problem :)
from turtle import pos
import pygame
import time
from pygame.locals import *
import random
pygame.init()
dis_width = 600
dis_height = 400
pos_x = 303
pos_y = 370
x_change = 0
y_change = 0
enemie_pos_x = random.randint(180, 420)
enemie_pos_y = 30
clock = pygame.time.Clock()
screen=pygame.display.set_mode((dis_width, dis_height))
font_style = pygame.font.SysFont("bahnschrift", 25)
time_font = pygame.font.SysFont("bahnschrift", 25)
running=True
obj = False
obj2 = False
enemie = False
game_over = False
crash = True
start = time.time() + 0.1
tolleranz = 40
speed = 5
timer = 5
score = 0
displayscore = 0
highscore = 0
ROT = (255, 0, 0)
BLACK = (0, 0, 0)
pygame.display.update()
def stopwatch(time):
value = time_font.render("Time: " + str(time), True, ROT)
screen.blit(value, [0, 0])
value2 = time_font.render("Score: " + str(score), True, ROT)
screen.blit(value2, [0, 30])
value6 = time_font.render("HighScore: " + str(highscore), True, ROT)
screen.blit(value6, [0, 60])
def enemies():
counter = 1
global enemie_pos_x, enemie_pos_y, speed, aTimeRound, timer, displayscore, highscore
enemie_pos_y += speed
if aTimeRound > timer:
speed += 5
timer += 5
if enemie_pos_y > 430:
enemie_pos_y = 0
enemie_pos_x = random.randint(180, 420)
global score
score += 1
if highscore <= score:
if highscore != score:
highscore += 1
displayscore = score
def gameoverF():
global score, start, speed, timer, game_over, displayscore, highscore
screen.fill(BLACK)
value3 = time_font.render("GAMEOVER", True, ROT)
screen.blit(value3, [230, 100])
value6 = time_font.render("Score:" + str(displayscore), True, ROT)
screen.blit(value6,[230, 150])
value4 = time_font.render("Highscore: " + str(highscore), True, ROT)
screen.blit(value4,[225, 200])
value5 = time_font.render("Press 'C' to play again OR Press 'Q' to Quit", True, ROT)
screen.blit(value5,[70,250])
score = 0
start = time.time()
speed = 5
timer = 5
pygame.display.update()
while running:
while game_over == True:
for event in pygame.event.get(): # Bei jeden GameLoop die Liste an Events abfragen
if event.type == pygame.QUIT:
running = False
pygame.quit()
quit()
if event.type == pygame.KEYDOWN: # Prüfen, ob der Eventtyp ein Tastendruck ist
if event.key == ord("c"): # Prüfen, ob die Cursor-Links-Taste gedrückt wurde...
game_over = False
speed = 5
timer = 5
score = 0
print("GEDRÜCKT!!!!!!!")
if event.key == ord("q"):
pygame.quit()
gameoverF()
#Messung der Zeit
aTime = time.time()-start
aTimeRound = round(time.time()-start,1)
#print(aTime)
#Malen der Steuer Kugel und der Linien
pos_x += x_change
screen.fill(BLACK)
pygame.draw.line(screen, ROT, (150,0),(150,400), 10)
pygame.draw.line(screen, ROT, (450,0),(450,400), 10)
pygame.draw.circle(screen, [255,255,255],(pos_x,pos_y), 25)
stopwatch(aTimeRound)
enemies()
pygame.draw.circle(screen, [255,255,255],(enemie_pos_x,enemie_pos_y), 25)
#print( enemie_pos_x - pos_x)
check_x = pos_x - enemie_pos_x
if check_x < 0:
check_x = check_x * -1
print(check_x)
if check_x < tolleranz and pos_y - enemie_pos_y < tolleranz:
game_over = True
clock.tick(30)
if pos_x <= 192:
pos_x = 192
if pos_x >= 410:
pos_x = 410
for event in pygame.event.get(): # Bei jeden GameLoop die Liste an Events abfragen
if event.type == pygame.QUIT:
running = False
pygame.quit()
quit()
if event.type == pygame.KEYDOWN: # Prüfen, ob der Eventtyp ein Tastendruck ist
if event.key == pygame.K_LEFT or event.key == ord("a"): # Prüfen, ob die Cursor-Links-Taste gedrückt wurde...
x_change = -10
y_change = 0 # Aktion bei Cursor-Links
if event.key == pygame.K_RIGHT or event.key == ord("d"):
x_change = 10
y_change = 0
pygame.display.update()
gameoverF()