Chloe Newhart
Chloe Newhart

Reputation: 11

Debugging coding falling objects at random in Python 3.8

I need to have an object with the variable name "enemy" to start at the top of the screen and fall towards the bottom of the screen towards the variable "player". I have the code for that already so that's working. But what I need is for it to fall in random different locations rather than just keep falling in the same location.

I know how to have it come from the left-hand side and go to the right at random but having it go vertically is a challenge for me. How can I understand what I am doing wrong?

#imports and init
import pygame
pygame.init()

#set up draw window
width, height = 1024,768
screen = pygame.display.set_mode((width,height))

#player var
playerPos = [20, 495]
playerSpeed = 8
playerLasers = []
playerLaserSpeed = 15
playerScore = 0
healthValue = 500

#enemy specific variables
import random
enemyimg = pygame.image.load("pythonimages/reaper.png")
enemytimer = random.randint(40,100)
enemies= []

#font set up
#health text
font = pygame.font.Font(None,36)
healthText = font.render("Health:" +str(healthValue), True, (21,244,238))
healthTextRect = healthText.get_rect()
healthTextRect.topright = [900,20]

#score text
font = pygame.font.Font(None,36)
scoreText = font.render("Score:" +str(playerScore), True, (21,244,238))
scoreTextRect = scoreText.get_rect()
scoreTextRect.topright = [115,10]

#load images
bg = pygame.image.load("pythonimages/forest.png")
moon = pygame.image.load("pythonimages/moon.png")
player = pygame.image.load("pythonimages/fairy.png")
playerLaser = pygame.image.load("pythonimages/flower.png")

#run until user quits
running = True
while running:
    #clear the screen
    screen.fill(0)

    #draw elements
    screen.blit(bg, (0,0))
    screen.blit(moon, (800, 20))

    #subtract from enemytimer
    enemytimer -= 1

    #move and update player laser
    for allLasers in playerLasers:
        index1 = 0
        #move up screen
        allLasers[1] -= playerLaserSpeed

        #check to see if laser is off screen
        if allLasers[1] > height:
            playerLasers.pop(index1)
        index1 +=1

    #check to see if we need new enemy
        if enemytimer <= 0:
            enemies.append([0, random.randint (0,0)])
            enemytimer = random.randint(40,60)

    #move enemies
    index2=0
    for enemy in enemies:
        enemy[1] += 5
        if enemy[1] < -enemyimg.get_size()[1]:
            enemies.pop(index2)
        index2 +=1

    #draw enemies
        for enemy in enemies:
            screen.blit(enemyimg, enemy)
        
    #draw laser
    for allLasers in playerLasers:
        screen.blit(playerLaser, (allLasers[0], allLasers[1]))

    #draw player
    screen.blit(player,playerPos)

    #draw Health Value
    screen.blit(healthText,healthTextRect)

    #draw Score Value
    screen.blit(scoreText,scoreTextRect)

    #flip display
    pygame.display.flip()

    #exit game
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    #fire laser
        if event.type == pygame.MOUSEBUTTONDOWN:
            playerLasers.append([playerPos[0], playerPos[1]])

    #move player
    keys = pygame.key.get_pressed()
    #1 up/down 0 left/right
    if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
        playerPos[0]  +=playerSpeed
    if keys[pygame.K_a] or keys[pygame.K_LEFT]:
        playerPos[0]  -=playerSpeed

    #keep player on screen
    if playerPos[0] <0:
        playerPos[0] =0
    if playerPos[0] >(width - player.get_size()[0]):
        playerPos[0] = (width - player.get_size()[0])
#done
pygame.quit()

Upvotes: 1

Views: 491

Answers (1)

Joshua Foo
Joshua Foo

Reputation: 103

enemies.append([0, random.randint (0,0)])

Always appends [0,0]. That's why it's always appearing at the same space.

I tried running your code with some pictures, and I kind of get what you are trying to do. You are appending an array of length 2, with the first element being the x-coordinate and the second being the y-coordinate. So, it will depend firstly on the size of your image as well as where you want it to sprawn. As all sprawn at the top, you should not be having a random integer for your Y-coordinate, but rather your x-coordinate.

Also, do keep in mind that the randint function takes in 2 values, the upper bound and lower bound. You may want to look at the following article as to how to properly utilise the randint function.

Hope this helps!

Article for random.randint Function: https://pynative.com/python-random-randrange/

Upvotes: 4

Related Questions