2 回答

TA貢獻(xiàn)1862條經(jīng)驗 獲得超6個贊
角色不會移動
player_change在主應(yīng)用程序循環(huán)中,播放器的移動連續(xù)設(shè)置為 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循環(huán)之前:
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
# [...]
子彈不會射
如果要發(fā)射子彈,則必須設(shè)置子彈的初始位置bulletX, bulletY = playerX, playerY并設(shè)置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它是一個參數(shù)game_loop,因此在不同的范圍內(nèi)是一個完全不同的變量,其名稱隨便相同:
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):
# [...]

TA貢獻(xiàn)1829條經(jīng)驗 獲得超7個贊
player_change = 0 playerX += player_change
嘗試交換這兩行的順序。否則,當(dāng) X 位置更新時,更改將始終為零。
添加回答
舉報