Cerebrock Cerebrock - 1 month ago 7
Python Question

Can't figure out what's wrong in Pygame app

I'm writing this time reaction test. It is supposed to display the text, wait for a key stroke and start the trials, appending the resulting measure to the file. But it just displays the text and freezes. Damn. I would be very grateful if you could help me point out the issue

import pygame
import random
import time

pygame.init()

red = (255, 0, 0)
light_sea_green = (32, 178, 172)
white = (255, 255, 255)
black = (0, 0, 0)

height, width = pygame.display.Info().current_w,pygame.display.Info().current_h

try:
screen = pygame.display.set_mode((height, width), pygame.FULLSCREEN)
except pygame.error:
screen = pygame.display.set_mode((780, 1360), pygame.FULLSCREEN)
print("display err")

screen.fill(white)
pygame.display.flip()
pygame.display.set_caption("TRTest")

data = open("data.txt", "r+")
sid = len([l for l in data.readlines()]) + 1

while True:
start = False
fnt = pygame.font.Font(None, 28)
txtrn = fnt.render("TR, press return", 0, (0, 0, 0))
screen.blit(txtrn, (((height/2) - (txtrn.get_rect().width/2)), ((width/2) - (txtrn.get_rect().height)/2)))
pygame.display.flip()

while not start:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
start == True

while start == True:
for x in range(3): #3 Trials
screen.fill((255, 255, 255))
pygame.draw.line(screen, (0, 0, 0), (height/2, width/2 - 10), (height/2, width/2 + 10), 2)
pygame.draw.line(screen, (0, 0, 0), (height/2 - 10, width/2), (height/2 + 10, width/2), 2)
pygame.display.flip()
pygame.time.delay(random.randint(2000, 8000))
pygame.draw.circle(screen, red, (height/2, width/2), 80)
pygame.display.flip()
t0 = time.clock()
pygame.event.clear()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
t1 = time.clock()
tr = (t1 - t0)
data.append(round(tr, 3))
running = False
elif event.type == pygame.QUIT:
pygame.quit()

Answer

You need = instead of == in code

if event.type == pygame.KEYDOWN:
     start == True # <--- need `=` instead of `==`