0

I'm working on a small game in Python called "Blank Screen Simulator" (but it won't just be a black screen, I want to have events. Like a time where random images pop up, but for now I only have a time where you click the mouse alot and a time where you press buttons alot) anyway, I need to make these random. Here's my system I tried:

import pygame
from pygame.locals import *
import time
import random

pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
Event1 == False
Event2 == False
screen_rect=screen.get_rect()
player=pygame.Rect(180, 180, 20, 20)
mouseClickNumber = 0
keyPressNumber = 0
a = random.choice(Event1, Event2)
a = True

def Event1:
    if pygame.mouse.get_pressed(button 1):
        mouseClickNumber = mouseClickNumber + 1
    time.sleep(20)
    clickHappyFunTime = False
    print mouseClickNumber
    mouseClickNumber = 0

def Event2:
    if pygame.key.get_pressed:
        keyPressNumber = keyPressNumber + 1
    time.sleep(20)
    buttonPressTime = False
    print keyPressNumber
    keyPressNumber = 0

My system involves having two events as False, then having a random.choice pick between the two variables, Event1 and Event2, but then when I run it, def Event2: is an invalid syntax. Am I just doing something wrong?

1 Answer 1

1

Function definitions must contain a parameter list, even if it's empty. Like this:

def Event1():

However, you've got a lot of other problems here.

  • Event1 == False doesn't assign False to Event1, it just compares Event1 to False—which is going to raise NameError because you don't have anything named Event1 yet, but even if you did, this wouldn't do anything useful, especially since you ignore the result.
  • If you fix that, a = random.choice(Event1, Event2) is going to fail because choice takes a single argument, a sequence to choose from, not a separate argument for each choice; you wanted a = random.choice([Event1, Event2]).
  • If you fix that, it's going to choose between False and False. Maybe you wanted it to choose between the two functions you're going to define later? But you can't choose between them before you define them. Defining them to some different value earlier doesn't help; it's just choosing the earlier value. (Also, reusing the same name for a normal value and a function is very confusing.)
  • If you fix that, it doesn't matter anyway, because a = True is just going to replace the value you chose.
  • You never call the functions directly, or attach them to events so they'll be called indirectly by PyGame, so they aren't doing much good.
  • The functions will raise an UnboundLocalError if you call them, because assigning to mouseClickNumber inside a function makes it a local variable, hiding the global variable of that name. To avoid that, you need to add global mouseClickNumber.
  • time.sleep inside a GUI application freezes the entire GUI, preventing it from updating the screen.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.