please excuse the messiness of my code as im still a beginner, but i'm here to ask about a problem im having. I'm trying to create a timer for donkey kong to throw barrels at specific time intervals but im not sure exactly how to work it out. I have the logic figured out for one barrel, but trying to apply my thinking for generating multiple isnt working (it worked when i did it for some simple shapes in a test). If i had to hazard a guess, i assume it might have to do with it trying to index my animation frame on a new barrel, because i dont think i have it set up correctly, but im not sure why my first barrel isnt blitting in the place i have it set. Here's my code for reference:
import pygame
import random
import math
from pygame import *
import pygame.freetype
def resetPosition():
global marioX
marioX = 50
global marioY
marioY = 725
global moveMarioRight
moveMarioRight = False
global moveMarioLeft
moveMarioLeft = False
global marioJump
marioJump = False
global climbPause
climbPause = False
global onLadder
onLadder = False
global isClimbingUp
isClimbingUp = False
global isClimbingDown
isClimbingDown = False
global gravity
gravity = 0
global walkCount
walkCount = 0
global climbCount
climbCount = 0
global barrelRollCount
barrelRollCount = 0
global dkCount
dkCount = 0
global barrelGravity
barrelGravity = 0
global barrelPos
barrelPos = [231, 244]
global barrelMoveRight
barrelMoveRight = True
global barrelMoveLeft
barrelMoveLeft = False
global barrelDescend
barrelDescend = False
global wasMovingLeft
wasMovingLeft = True
global barrelOffLadder
barrelOffLadder = True
global barrelMoveAnimate
barrelMoveAnimate = True
global barrelDescendAnimate
barrelDescendAnimate = False
def main():
""" Set up the game and run the main game loop """
pygame.init() # Prepare the pygame module for use
pygame.freetype.init()
surfaceWidth = 715
surfaceLength = 813# Desired physical surface size, in pixels.
clock = pygame.time.Clock() #Force frame rate to be slower
# Create surface of (width, height), and its window.
mainSurface = pygame.display.set_mode((surfaceWidth, surfaceLength))
font = pygame.freetype.Font("8bitOperatorPlus8-Bold.ttf", 72)
buttonFont = pygame.freetype.Font("8bitOperatorPlus8-Bold.ttf", 48)
programState = 'initialize'
if programState == 'initialize':
platforms = pygame.image.load("level.png")
platformPos = [0, 0]
global marioX
marioX = 50
global marioY
marioY = 725
marioRight = [pygame.image.load("mario-right.png"), pygame.image.load("run-right.png"), pygame.image.load("jump-right.png")]
marioLeft = [pygame.image.load("mario-left.png"), pygame.image.load("run-left.png"), pygame.image.load("jump-left.png")]
marioClimb = [pygame.image.load("marioClimb1.png"), pygame.image.load("marioClimb2.png")]
dkSprite = [pygame.image.load("dkForward.png"), pygame.image.load("dkLeft.png"), pygame.image.load("dkRight.png")]
barrelStack = pygame.image.load("barrel-stack.png")
barrels = [pygame.image.load("barrel1.png"), pygame.image.load("barrel2.png"), pygame.image.load("barrel3.png"), pygame.image.load("barrel4.png"), pygame.image.load("barrel-down.png")]
deadMario = pygame.image.load("dead.png")
deadMario = pygame.transform.scale2x(deadMario)
hammer = pygame.image.load("marioHammer.png")
hammer = pygame.transform.scale2x(hammer)
marioHammer = pygame.image.load("marioHammer1.png")
marioHammer = pygame.transform.scale2x(marioHammer)
#luigiRight = [pygame.image.load("luigi-right.png").convert(), pygame.image.load("luigi-runright.png").convert()]
#luigiRight.set_colorkey((255, 255, 255))
dkPos = [95, 183]
global moveMarioRight
moveMarioRight = False
global moveMarioLeft
moveMarioLeft = False
runRightAnimate = False
runLeftAnimate = False
standingLeft = False
standingRight = True
jumpingLeft = False
jumpingRight = False
global marioJump
marioJump = False
climbAnimate = False
global barrelMoveAnimate
barrelMoveAnimate = True
global barrelDescendAnimate
barrelDescendAnimate = False
global climbPause
climbPause = False
global onLadder
onLadder = False
global isClimbingUp
isClimbingUp = False
global isClimbingDown
isClimbingDown = False
global gravity
gravity = 0
global walkCount
walkCount = 0
global climbCount
climbCount = 0
global barrelRollCount
barrelRollCount = 0
global dkCount
dkCount = 0
groundLevel = 800
barrelGroundLevel = 250
global barrelGravity
barrelGravity = 0
ladderPosList = []
blockerPosList = []
detectorPosList = []
numLadders = 6
numLadderBlockers = 6
numBarrelDetectors = 5
ladderHitboxColour = (100, 100, 100)
ladderBlockerColour = (255, 0, 0)
barrelDetectorColour = (0, 0, 255)
global barrelPos
barrelPos = [231, 244]
global barrelMoveRight
barrelMoveRight = True
global barrelMoveLeft
barrelMoveLeft = False
global barrelDescend
barrelDescend = False
global wasMovingLeft
wasMovingLeft = True
global barrelOffLadder
barrelOffLadder = True
buttonColour = (161, 161, 161)
button2Colour = (161, 161, 161)
barrelCount = 60
hammerPos = [560, 608]
hammerGrabbed = False
barrelPosList = []
barrelCount = 0
futureBarrelCount = 60
for i in range((numLadders)):
ladderPosList.append([577, 676])
ladderPosList.append([122, 582])
ladderPosList.append([354, 466])
ladderPosList.append([116, 383])
ladderPosList.append([565, 282])
ladderPosList.append([403, 197])
for i in range((numLadderBlockers)):
blockerPosList.append([577, 751.5])
blockerPosList.append([122, 654.5])
blockerPosList.append([354, 563.5])
blockerPosList.append([116, 455.5])
blockerPosList.append([565, 359.5])
blockerPosList.append([403, 282.5])
for i in range((numBarrelDetectors)):
detectorPosList.append([577, 676])
detectorPosList.append([122, 577])
detectorPosList.append([354, 466])
detectorPosList.append([116, 383])
detectorPosList.append([565, 282])
programState = 'game'
while True:
mouseX, mouseY = pygame.mouse.get_pos()
print(mouseX, mouseY)
ev = pygame.event.poll() # Look for any event
if ev.type == pygame.QUIT: # Window close button clicked?
break# ... leave game loop
if programState == 'game':
marioHitBox = marioRight[0].get_rect(topleft=(marioX, marioY))
barrelHitBox = barrels[0].get_rect(topleft=(barrelPos[0], barrelPos[1]))
hammerHitBox = hammer.get_rect(topleft=(120, 700))
if ev.type == pygame.KEYDOWN:
if isClimbingUp == False and isClimbingDown == False:
if ev.key == pygame.K_d:
moveMarioRight = True
runRightAnimate = True
standingRight = False
if ev.key == pygame.K_a:
moveMarioLeft = True
runLeftAnimate = True
standingLeft = False
if ev.key == pygame.K_w:
if onLadder == False:
pass
elif onLadder == True:
jumpingLeft = False
jumpingRight = False
isClimbingUp = True
climbAnimate = True
standingRight = False
standingLeft = False
climbPause = False
if ev.key == pygame.K_s:
if onLadder == False:
pass
elif onLadder == True:
jumpingLeft = False
jumpingRight = False
isClimbingDown = True
climbAnimate = True
standingRight = False
standingLeft = False
climbPause = False
elif ev.type == pygame.KEYUP:
if isClimbingUp == False and isClimbingDown == False:
if ev.key == pygame.K_d:
moveMarioRight = False
runRightAnimate = False
standingRight = True
standingLeft = False
if ev.key == pygame.K_a:
moveMarioLeft = False
runLeftAnimate = False
standingLeft = True
standingRight = False
if ev.key == pygame.K_w:
if onLadder == False:
if (not marioJump):
gravity -= 7.5
marioJump = True
if standingLeft or runLeftAnimate == True:
jumpingLeft = True
standingLeft = False
runRightAnimate = False
jumpingRight = False
elif standingRight or runRightAnimate == True:
jumpingRight = True
standingRight = False
jumpingLeft = False
runLeftAnimate = False
if onLadder == True:
isClimbingUp = False
climbAnimate = False
climbPause = True
if ev.key == pygame.K_s:
if onLadder == False:
pass
if onLadder == True:
isClimbingDown = False
climbAnimate = False
climbPause = True
# Update your game objects and data structures here...
if barrelCount >= futureBarrelCount:
barrelPosList.append(barrelPos)
futureBarrelCount = barrelCount + 60
for i in range(len(ladderPosList)):
if marioHitBox.colliderect((ladderPosList[i], (70, 90))):
#A collision happens!
onLadder = True
break
else:
onLadder = False
if onLadder == True:
if isClimbingDown:
for i in range(len(blockerPosList)):
if marioHitBox.colliderect((blockerPosList[i], (70, 22.5))):
#A collision happens!
isClimbingDown = False
break
else:
isClimbingDown = True
for i in range(len(detectorPosList)):
if barrelHitBox.colliderect((detectorPosList[i], (70, 22.5))):
barrelOffLadder = False
if barrelMoveRight == True:
wasMovingLeft = False
barrelMoveRight = False
barrelDescendAnimate = True
barrelDescend = True
elif barrelMoveLeft == True:
wasMovingLeft = True
barrelMoveLeft = False
barrelDescendAnimate = True
barrelDescend = True
for i in range(len(blockerPosList)):
if barrelHitBox.colliderect((blockerPosList[i], (70, 22.5))):
barrelOffLadder = True
barrelDescend = False
barrelDescendAnimate = False
barrelMoveAnimate = True
break
if marioHitBox.colliderect(barrelHitBox):
programState = 'game over'
if marioHitBox.colliderect(hammerHitBox):
hammerGrabbed = True
if onLadder == False:
marioY += gravity
detectionCoord = [int(marioX), int(marioY)+34]
colorBelow = mainSurface.get_at(detectionCoord)
if colorBelow == (255, 6, 65, 255):
groundLevel = detectionCoord[1]-1
else:
groundLevel = surfaceLength
if ((marioY + 34) > groundLevel):
marioY = groundLevel - 34
gravity = 0
marioJump = False
else:
gravity += 0.5
barrelPos[1] += barrelGravity
barrelDetectionCoord = [int(barrelPos[0]), int(barrelPos[1])+23]
barrelColorBelow = mainSurface.get_at(barrelDetectionCoord)
if barrelColorBelow == (255, 6, 65, 255):
barrelGroundLevel = barrelDetectionCoord[1]-1
else:
barrelGroundLevel = surfaceLength
if ((barrelPos[1] + 23) > barrelGroundLevel):
barrelPos[1] = barrelGroundLevel - 23
barrelGravity = 0
else:
barrelGravity += 0.5
if walkCount >= 2:
walkCount = 0
if climbCount >= 2:
climbCount = 0
if barrelRollCount >= 4:
barrelRollCount = 0
if moveMarioRight:
marioX += 3 #update the x
if moveMarioLeft:
marioX -= 3 #update the x
if isClimbingUp:
marioY -= 2
if isClimbingDown:
marioY += 2
if barrelOffLadder and wasMovingLeft == True:
barrelMoveRight = True
barrelMoveLeft = False
if barrelMoveRight:
barrelPos[0] += 5
elif barrelOffLadder and wasMovingLeft == False:
barrelMoveRight = False
barrelMoveLeft = True
if barrelMoveLeft:
barrelPos[0] -= 5
if barrelDescend:
barrelPos[1] += 4
if marioX <= 0:
marioX = 0
elif marioX >= 671:
marioX = 671
# We draw everything from scratch on each frame.
# So first fill everything with the background color
mainSurface.fill((8, 8, 8))
for i in range(len(ladderPosList)):
pygame.draw.rect(mainSurface, ladderHitboxColour, (ladderPosList[i], (70, 90)))
for i in range(len(blockerPosList)):
pygame.draw.rect(mainSurface, ladderBlockerColour, (blockerPosList[i], (70, 22.5)))
for i in range(len(detectorPosList)):
pygame.draw.rect(mainSurface, barrelDetectorColour, (detectorPosList[i], (70, 22.5)))
mainSurface.blit(platforms, platformPos)
mainSurface.blit(barrelStack, (36, 196))
if hammerGrabbed == False:
mainSurface.blit(hammer, (120, 700))
dkCount += 1
if dkCount < 30:
mainSurface.blit(dkSprite[0], dkPos)
elif dkCount >= 30 and dkCount < 60:
mainSurface.blit(dkSprite[1], dkPos)
elif dkCount >= 60 and dkCount < 90:
mainSurface.blit(dkSprite[2], dkPos)
elif dkCount == 90:
dkCount = 0
else:
mainSurface.blit(dkSprite[0], dkPos)
for i in range(len(barrelPosList)):
if barrelMoveAnimate:
mainSurface.blit(barrels[barrelRollCount], barrelPosList[i])
barrelRollCount += 1
if barrelDescendAnimate:
mainSurface.blit(barrels[4], barrelPosList[i])
#mainSurface.blit(marioHammer, (60, 700))
pygame.draw.rect(mainSurface, (6,6,6), (0, 0, surfaceWidth, 70))
pygame.draw.line(mainSurface, (255, 6, 65), (0, 769), (364, 769), 10)
pygame.draw.line(mainSurface, (255, 6, 65), (364, 769), (698, 747),10)
pygame.draw.line(mainSurface, (255, 6, 65), (32, 649), (650, 685), 10)
pygame.draw.line(mainSurface, (255, 6, 65), (79, 587), (697, 551), 10)
pygame.draw.line(mainSurface, (255, 6, 65), (32, 453), (649, 489), 10)
pygame.draw.line(mainSurface, (255, 6, 65), (79, 391), (697, 356), 10)
pygame.draw.line(mainSurface, (255, 6, 65), (461, 283), (650, 292), 10)
pygame.draw.line(mainSurface, (255, 6, 65), (32, 279), (460, 279), 10)
#mainSurface.blit(luigi, (100, 100))
if marioJump == True:
runRightAnimate = False
runLeftAnimate = False
if runRightAnimate:
mainSurface.blit(marioRight[walkCount], (marioX, marioY))
walkCount += 1
elif runLeftAnimate:
mainSurface.blit(marioLeft[walkCount], (marioX, marioY))
walkCount += 1
elif standingRight:
mainSurface.blit(marioRight[0], (marioX, marioY))
elif standingLeft:
mainSurface.blit(marioLeft[0], (marioX, marioY))
elif jumpingLeft:
mainSurface.blit(marioLeft[2], (marioX, marioY))
if marioJump == False:
standingLeft = True
elif jumpingRight:
mainSurface.blit(marioRight[2], (marioX, marioY))
if marioJump == False:
standingRight = True
elif climbAnimate:
mainSurface.blit(marioClimb[climbCount], (marioX, marioY))
climbCount += 1
elif climbPause:
mainSurface.blit(marioClimb[1], (marioX, marioY))
if programState == 'game over':
if mouseX >= 265 and mouseX <= 465 and mouseY >= 460 and mouseY <= 540:
buttonColour = (84, 84, 84)
if ev.type == pygame.MOUSEBUTTONUP:
resetPosition()
programState = "game"
else:
buttonColour = (161, 161, 161)
if mouseX >= 265 and mouseX <= 465 and mouseY >= 590 and mouseY <= 670:
button2Colour = (84, 84, 84)
if ev.type == pygame.MOUSEBUTTONUP:
pass
else:
button2Colour = (161, 161, 161)
mainSurface.fill((8,8,8))
font.render_to(mainSurface, (135, 190), "GAME OVER!", (88, 245, 242))
pygame.draw.rect(mainSurface, buttonColour, [265, 460, 200, 80])
pygame.draw.rect(mainSurface, button2Colour, [265, 590, 200, 80])
buttonFont.render_to(mainSurface, (300, 480), "PLAY", (8, 8, 8))
buttonFont.render_to(mainSurface, (300, 610), "MENU", (8, 8, 8))
mainSurface.blit(pygame.transform.rotate(deadMario, 90), (330, 300))
# Now the surface is ready, tell pygame to display it!
barrelCount += 1
pygame.display.flip()
clock.tick(30) #Force frame rate to be slower
pygame.quit() # Once we leave the loop, close the window.
main()