I've been coding a simple platform game, where the user controls a square and has to get to the other side of the window, without colliding with the other squares. I want the other 4 squares to bounce up and down so I wrote this code:
import pygame
import os
import sys
os.environ['SDL_VIDEO_CENTERED'] = "1"
pygame.init()
#Variables:
width = 600
height = width / 16 * 9
running = True
#Colors
PINK = (255, 79, 161)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
clock = pygame.time.Clock()
#MainRectProperties
mainRectX = 0
mainRectY = height / 2 - 20
mainRectSpeed = 250
#RectOneProperties:
rectOneX = 150
rectOneY = 0
#RectTwoPropeties:
rectTwoX = 250
rectTwoY = height - 20
#RectThreeProperties:
rectThreeX = 350
rectThreeY = 0
#RectFourProperties:
rectFourX = 450
rectFourY = height - 20
#Window:
window = pygame.display.set_mode((width, height))
windowText = pygame.display.set_caption("Pixel Animation")
#Rectangles:
mainRect = pygame.draw.rect(window, PINK, (mainRectX, mainRectY, 20, 20), 0)
obstacleRect1 = pygame.draw.rect(window, BLUE, (rectOneX, rectOneY, 20, 20), 0)
obstacleRect2 = pygame.draw.rect(window, BLUE, (rectTwoX, rectTwoY, 20, 20,), 0)
obstacleRect3 = pygame.draw.rect(window, BLUE, (rectThreeX, rectThreeY, 20, 20), 0)
obstacleRect4 = pygame.draw.rect(window, BLUE, (rectFourX, rectFourY, 20, 20), 0)
pygame.display.flip()
#UpdateMainRectFunction
def updateMainRect(x, y):
window.fill(BLACK)
mainRect = pygame.draw.rect(window, PINK, (mainRectX, mainRectY, 20, 20), 0)
pygame.display.flip()
clock.tick(250)
#GameLoop
while running:
goingDown = True
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if rectOneY < height - 21 and goingDown:
print "b"
pygame.draw.rect(window, BLACK, (rectOneX, rectOneY, 20, 20), 0)
rectOneY += 1
obstacleRect1 = pygame.draw.rect(window, BLUE, (rectOneX, rectOneY, 20, 20), 0)
pygame.display.flip()
clock.tick(100)
if rectOneY == height - 21:
goingDown = False
else:
goingDown = True
if not goingDown and rectOneY != 0:
print "a"
pygame.draw.rect(window, BLACK, (rectOneX, rectOneY, 20, 20), 0)
rectOneY -= 1
obstacleRect1 = pygame.draw.rect(window, BLUE, (rectOneX, rectOneY, 20, 20), 0)
pygame.display.flip()
clock.tick(100)
goingDown = False
print rectOneY < height - 21 and goingDown
keys = pygame.key.get_pressed()
#MovingRectCommands
if keys[pygame.K_UP]:
mainRectY -= 1
updateMainRect(mainRectX, mainRectY)
if keys[pygame.K_DOWN]:
mainRectY += 1
updateMainRect(mainRectX, mainRectY)
if keys[pygame.K_LEFT]:
mainRectX -= 1
updateMainRect(mainRectX, mainRectY)
if keys[pygame.K_RIGHT]:
mainRectX += 1
updateMainRect(mainRectX, mainRectY)
Basically the rectangle starts at the top of the screen and then sucessfuly hits the bottom of it. It prints a lot of "b's" to the console, as expected. Then, the rectangle moves up one pixel, the program prints one "a" to the console, but then it goes down again, even though the "if not going down and rectOneY != 0" expression is equal to True and the "if rectOneY < height - 21 and goingDown" expression is equal to False.
I've been trying to fix this for an hour at least, and I simply can't understand what's wrong and I could use some help.
I'd like you to point out what's the problem with my code(only the specific one I'm asking, not the millions of examples of bad code before it xD).
Thanks in advance.