2

There are no errors, but nothing runs when we try to run it.

We are trying to make arrows move and pop up randomly and stop when they make it to the bottom. Kind of like Dance Dance Revolution. We started small and got the arrows to work with just one while loop, but we want them to all be random.

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)

We want random arrows to show up at random times, then disappear when they get to the edge. There are no erroe messages, but there is a window that pops up that just freezes.

  • 5
    multiple whlie loops are the going to be blocking calls and never allow you to update the display (or anything else), you need to stick to the one main game loop – Sayse Oct 20 '19 at 18:12
  • 3
    `MoveArrow.y = 700` is an assignment. The compare operator is `==`. So it has to be `if (MoveArrow.y == 700)`. Furthermore, you've to update the display and to handle the events in each of the `while` loops. – Rabbid76 Oct 20 '19 at 18:16
  • 4
    Your program will never get past the first while loop. – quamrana Oct 20 '19 at 18:28
  • 1
    Remove all the `while True:` but the first one, and see if it works. Also you have a lot of repetitions which could be removed. Replace them with a function. – Valentino Oct 20 '19 at 22:02

0 Answers0