多个while循环

2024-10-04 01:32:56 发布

您现在位置:Python中文网/ 问答频道 /正文

没有错误,但是当我们尝试运行它时,什么也不会运行。你知道吗

我们试图使箭头移动和弹出随机和停止时,他们使它的底部。有点像舞蹈革命。我们从小处着手,让箭头只与一个while循环一起工作,但我们希望它们都是随机的。你知道吗

import pygame, sys
from pygame.locals import *
from random import randint
from time import sleep

pygame.init()

speed = 30 # frames per second
WIDTH = 1100
HEIGHT = 700
GameState = "play" #this can be play, title, or end
class MoveArrow(object):


    arrowRight = pygame.image.load("red right.png")
    arrowLeft = pygame.image.load("blue left.png")
    arrowUp = pygame.image.load("green up.png")
    arrowDown = pygame.image.load("yellow down.png")
    grayUp =pygame.image.load("gray up.png")
    grayDown =pygame.image.load("gray down.png")
    grayLeft =pygame.image.load("gray left.png")
    grayRight =pygame.image.load("gray right.png")


    y = 0
    x1 = 50
    x2 = 350
    x3 = 650
    x4 = 950

    arrow = [arrowRight, arrowLeft, arrowUp, arrowDown]
    x = [x1, x2, x3, x4]

speedControl = pygame.time.Clock()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
background = pygame.image.load("try.jpg")
background = pygame.transform.scale(background,(WIDTH,HEIGHT)) ## Make it the same size as the screen
pygame.display.set_caption("Animation")
white = (255, 255, 255)
black = ( 0, 0, 0)

while True: # game loop
    screen.blit(background,(0,0)) ## Blit the background onto the screen first

while True:
    screen.blit(MoveArrow.grayRight, (MoveArrow.x1, 575))
    screen.blit(MoveArrow.grayLeft, (MoveArrow.x2, 575))
    screen.blit(MoveArrow.grayUp, (MoveArrow.x3, 575))
    screen.blit(MoveArrow.grayDown, (MoveArrow.x4, 575))

while True:
    chosenArrow = MoveArrow.arrowRight
    screen.blit(chosenArrow, (x1, MoveArrow.y))
    if (MoveArrow.y = 700):
        sleepTime = randint(0, 10)
        sleep(sleepTime)
    else:
        MoveArrow.y += 5

while True:
    chosenArrow = MoveArrow.arrowLeft
    screen.blit(chosenArrow, (x2, MoveArrow.y))
    if (MoveArrow.y = 700):
        sleepTime = randint(0, 10)
        sleep(sleepTime)
    else:
        MoveArrow.y += 5


while True:
    chosenArrow = MoveArrow.arrowUp
    screen.blit(chosenArrow, (x3, MoveArrow.y))
    if (MoveArrow.y = 700):
        sleepTime = randint(0, 10)
        sleep(sleepTime)
    else:
        MoveArrow.y += 5

while True:
    chosenArrow = MoveArrow.arrowDown
    screen.blit(chosenArrow, (x4, MoveArrow.y))
    if (MoveArrow.y = 700):
        sleepTime = randint(0, 10)
        sleep(sleepTime)
    else:
        MoveArrow.y += 5

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    pygame.display.flip()
    pygame.display.update()
    speedControl.tick(speed)

我们希望随机箭头在随机时间出现,然后在到达边缘时消失。没有错误信息,但是有一个弹出的窗口冻结了。你知道吗


Tags: imagetrueifpngloadsleepscreenpygame