简体   繁体   中英

When i try and use the function "wait", it doesn't work

import os, sys, math, pygame, pygame.mixer
from pygame.locals import *

class Unit():
    def __init__(self):
        self.last = pygame.time.get_ticks()
        self.cooldown = 3000
    def wait(self):
        now = pygame.time.get_ticks()
        if now - self.last >= self.cooldown:
            screen.fill(black)

black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
white = (255,255,255)
run_me = True

stage1 = 1
stage2 = 1
stage3 = 1
stage4 = 1
st1 = True
st2 = True
st3 = True
st4 = True

screen_size = screen_width, screen_height = 600, 400
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('ha ha ha ha ha')

clock = pygame.time.Clock()
fps_limit = 60

while run_me:
    clock.tick(fps_limit) 

    if stage1 == 1:
        screen.fill(red)
        wait(self)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run_me = False
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    screen.fill(red)
                    stage2 - 1
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    screen.fill(black)
                    stage1 - 1
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    screen.fill(blue)
                    stage1 - 1
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_DOWN:
                    screen.fill(green)
                    stage1 - 1

        if stage1 == 0:
            run_me = False

    pygame.display.flip()

pygame.quit()

when I run this code it outputs "wait is not defined" how can I fix this and make the code make the screen red then black again. this should have a 3 second gap. I'm not good with functions and I would really like some help.

also if you have enough time can you send me a guide for functions. I will be most greatful.

To call a function inside a class, you have to first of all create an instance of the class. To do this:

unit = Unit()

So now you have an instance of the class stored in unit and you can call any of it's properties (including functions) directly from it. So to call the wait function :

unit = Unit()
unit.wait()

Note that you don't have to include self as a parameter because you already created an instance of Unit() in the unit variable


As a sidenote if you want to pause the program for three seconds after you change the background to red, then change it back to black, you can use the time module. After importing time you can simply type in:

window.fill(red)
pygame.display.flip()
time.sleep(3)
window.fill(black)
pygame.display.flip()

at whatever part in your code where you want it to pause. After the pause is done you can then change the background back to black. But you will have to call pygame.display.flip() each time you change the background for this to take effect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM