在Python3.8中调试对下落物体的随机编码

在Python3.8中调试对下落物体的随机编码,python,python-3.x,pygame,Python,Python 3.x,Pygame,我需要有一个变量名为敌人的对象,从屏幕顶部开始,朝着屏幕底部的变量播放器下落。我已经有了这个的代码,所以它可以工作了。但我需要的是让它随机落在不同的地方,而不是一直落在同一个地方 我知道如何让它从左手边来,然后随机向右移动,但是让它垂直移动对我来说是一个挑战。我怎么能理解我做错了什么 #imports and init import pygame pygame.init() #set up draw window width, height = 1024,768 screen = pygame

我需要有一个变量名为敌人的对象,从屏幕顶部开始,朝着屏幕底部的变量播放器下落。我已经有了这个的代码,所以它可以工作了。但我需要的是让它随机落在不同的地方,而不是一直落在同一个地方

我知道如何让它从左手边来,然后随机向右移动,但是让它垂直移动对我来说是一个挑战。我怎么能理解我做错了什么

#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()
始终附加[0,0]。这就是为什么它总是出现在同一个空间

我试着用一些图片运行你的代码,我有点明白你想做什么。您将附加一个长度为2的数组,第一个元素是x坐标,第二个元素是y坐标。因此,这首先取决于你的图像的大小以及你想让它生长到哪里。由于所有的变量都在顶部,所以Y坐标不应该是一个随机整数,而应该是x坐标

另外,请记住randint函数有两个值,上限和下限。关于如何正确使用randint函数,您可能想看下面的文章

希望这有帮助

random.randint函数的文章:

始终附加[0,0]。这就是为什么它总是出现在同一个空间

我试着用一些图片运行你的代码,我有点明白你想做什么。您将附加一个长度为2的数组,第一个元素是x坐标,第二个元素是y坐标。因此,这首先取决于你的图像的大小以及你想让它生长到哪里。由于所有的变量都在顶部,所以Y坐标不应该是一个随机整数,而应该是x坐标

另外,请记住randint函数有两个值,上限和下限。关于如何正确使用randint函数,您可能想看下面的文章

希望这有帮助


random.randint函数文章:

我希望它每次都出现在屏幕顶部。我的意思是,我试图让它每次都随机出现在顶部,但从顶部开始出现在不同/随机的位置。例如,假设一个敌人变量出现在屏幕左边的顶部,那么另一个敌人出现在中间,然后右边,等等。我有什么感觉吗?嘿,比利佛拜金狗,我已经更新了我的答案。祝你的项目一切顺利!非常感谢。我明白我现在需要做什么!你是最棒的!c:我希望它每次都能从屏幕的顶部出现。我的意思是,我试图让它每次都随机出现在顶部,但从顶部开始出现在不同/随机的位置。例如,假设一个敌人变量出现在屏幕左边的顶部,那么另一个敌人出现在中间,然后右边,等等。我有什么感觉吗?嘿,比利佛拜金狗,我已经更新了我的答案。祝你的项目一切顺利!非常感谢。我明白我现在需要做什么!你是最棒的!c:
enemies.append([0, random.randint (0,0)])