亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Pygame角色不會向左或向右移動

Pygame角色不會向左或向右移動

一只甜甜圈 2022-07-12 14:47:36
在我下面使用 PyGame 的代碼中,角色不會左右移動,子彈也不會射擊。我不確定是什么原因造成的。它可以在沒有帶有按鈕概念的整個開始菜單的情況下工作,所以我想知道這是否是問題所在?import pygamefrom pygame import mixerimport mathimport randomimport time# Initialize pygame modulepygame.init()# Create the screenscreen = pygame.display.set_mode((800, 600))display_width = 800display_height = 600# Backgroundbackground = pygame.image.load('space1.jpg')# Background Soundmixer.music.load('background.wav')mixer.music.play(-1)# Title and Iconpygame.display.set_caption('Space Invaders')icon = pygame.image.load('ufo.png')pygame.display.set_icon(icon)# PlayerplayerImg = pygame.image.load('space-invaders.png')playerX = 370playerY = 480player_change = 0playerX += player_change# EnemyenemyImg = []enemyX = []enemyY = []enemyX_change = []enemyY_change = []num_of_enemies = 6# ColoursWHITE = (255, 255, 255)BLACK = (0, 0, 0)RED = (150, 0, 0)GREEN = (0, 150, 0)BRIGHT_RED = (255, 0,0)BRIGHT_GREEN = (0, 255, 0)for i in range(num_of_enemies):    enemyImg.append(pygame.image.load('alien.png'))    enemyX.append(random.randint(0, 736))    enemyY.append(random.randint(50, 150))    enemyX_change.append(2)    enemyY_change.append(40)# Bullet# Ready - You cant see the bullet on the screen# Fire - The bullet is current movingbulletImg = pygame.image.load('bullet.png')bulletX = 0bulletY = 480bulletY_change = 10bullet_state = 'ready'# Scorescore_value = 0font = pygame.font.Font('freesansbold.ttf', 32)textX = 10textY = 10# Game Over Textover_font = pygame.font.Font('freesansbold.ttf', 64)# Clockclock = pygame.time.Clock()def show_score(x, y):    score = font.render('score : ' + str(score_value), True, (0, 255, 0))    screen.blit(score, (x, y))def game_over_text():    over_text = over_font.render('GAME OVER', True, (0, 255, 0))    screen.blit(over_text, (200, 250))def player(x, y):    screen.blit(playerImg, (x, y))def enemy(x, y, i):    screen.blit(enemyImg[i], (x, y))
查看完整描述

2 回答

?
阿波羅的戰車

TA貢獻1862條經驗 獲得超6個贊

角色不會移動


player_change在主應用程序循環中,播放器的移動連續設置為 0。此外,玩家的位置不斷初始化:


def game_loop(playerX, playerY, player_change, bulletX, bulletY, bulletX_change, >bulletY_change, bullet_state, bulletImg):

   running = True

   while running:

       playerX = 370

       playerY = 480

       player_change = 0

初始化playerX,playerY和player_change循環之前:


def game_loop(playerX, playerY, player_change, bulletX, bulletY, bulletX_change, bulletY_change, bullet_state, bulletImg):

    running = True

    player_change = 0

    playerX = 370

    playerY = 480

    while running:    

        playerX += player_change

        # [...]

子彈不會射


如果要發射子彈,則必須設置子彈的初始位置bulletX, bulletY = playerX, playerY并設置bullet_state = 'fire':


def game_loop(playerX, playerY, player_change, bulletX, bulletY, bulletX_change, bulletY_change, bullet_state, bulletImg):

    # [...]    


    while running:    

        playerX += player_change


        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                running = False


            # If keystroke is pressed check whether its left or right

            if event.type == pygame.KEYDOWN:

                # [...]               


                if event.key == pygame.K_SPACE:

                    if bullet_state is 'ready':

                        #bullet_sound = mixer.Sound('laser.wav')

                        #bullet_sound.play()

                        # Get the current x coordinate of the spaceship

                        bulletX, bulletY = playerX, playerY

                        bullet_state = 'fire'

注意,global變量bullet_state


def fire_bullet(x, y):

   global bullet_state

   bullet_state = 'fire'

與 中的局部變量不同bullet_state,game_loop因為bullet_state它是一個參數game_loop,因此在不同的范圍內是一個完全不同的變量,其名稱隨便相同:


def game_loop(playerX, playerY, player_change, bulletX, bulletY, 

              bulletX_change, bulletY_change, 

              bullet_state, # <--- that is a new variable in local scope

                            #      and not the "global" bullet_state

              bulletImg):

    # [...]


查看完整回答
反對 回復 2022-07-12
?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

     player_change = 0
        playerX += player_change

嘗試交換這兩行的順序。否則,當 X 位置更新時,更改將始終為零。


查看完整回答
反對 回復 2022-07-12
  • 2 回答
  • 0 關注
  • 224 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號