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

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

Sprite不會在pygame中移動

Sprite不會在pygame中移動

皈依舞 2022-06-14 16:57:32
此代碼中的“行星”精靈不會左右移動,不會動畫。錯誤在哪里?實際上,該列表來自 google play 的應用程序“Programming Hero”,其中應用程序的作者一步一步地指導學生完成視頻 - 使用 python 制作游戲的過程......import pygamescreen_size = [360,640]screen =     pygame.display.set_mode(screen_size)background = pygame.image.load('background.png')planet =     pygame.image.load('one.png')spaceship =  pygame.image.load('spaceship.png')bullet =     pygame.image.load('bullet.png')keep_alive = Trueplanet_x = 140           # I've edited thismove_direction = 'right' # out of the loop, too# according to the same logic "I've saved the planet" to go infinitely right# thanx while keep_alive:    pygame.event.get()    keys = pygame.key.get_pressed()    if keys[pygame.K_SPACE] == True:        print('Space key pressed')    screen.blit(background, [0,0])    screen.blit(bullet,[180,500])    screen.blit(spaceship,[160,500])    # I put this out off loop:           planet_x = 140    # wrong, must go out of a loop, too: move_direction = 'right'    if move_direction == 'right':        planet_x = planet_x + 5        if planet_x == 270:            move_direction = 'left'    else:        planet_x = planet_x - 5        if planet_x == 0:            move_direction = 'right'    screen.blit(planet, [planet_x, 50])    pygame.display.update()
查看完整描述

1 回答

?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

行星 ( planet_x) 的位置在每一幀中重新初始化:


while keep_alive:

   # [...]


   planet_x = 140


   if move_direction == 'right':

       planet_x = planet_x + 5


   # [...]


   screen.blit(planet, [planet_x, 50])

將 , 的初始化移到planet_x主應用程序循環之前:


planet_x = 140

while keep_alive:


    # [...]


    if move_direction == 'right':

        planet_x = planet_x + 5


    # [...]


    screen.blit(planet, [planet_x, 50])


查看完整回答
反對 回復 2022-06-14
  • 1 回答
  • 0 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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