Pygame角色不会向左或向右移动

在我下面使用 PyGame 的代码中,角色不会左右移动,子弹也不会射击。我不确定是什么原因造成的。它可以在没有带有按钮概念的整个开始菜单的情况下工作,所以我想知道这是否是问题所在?


import pygame

from pygame import mixer

import math

import random

import time


# Initialize pygame module

pygame.init()


# Create the screen

screen = pygame.display.set_mode((800, 600))

display_width = 800

display_height = 600


# Background

background = pygame.image.load('space1.jpg')


# Background Sound

mixer.music.load('background.wav')

mixer.music.play(-1)


# Title and Icon

pygame.display.set_caption('Space Invaders')

icon = pygame.image.load('ufo.png')

pygame.display.set_icon(icon)


# Player

playerImg = pygame.image.load('space-invaders.png')

playerX = 370

playerY = 480

player_change = 0

playerX += player_change


# Enemy

enemyImg = []

enemyX = []

enemyY = []

enemyX_change = []

enemyY_change = []

num_of_enemies = 6


# Colours

WHITE = (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 moving

bulletImg = pygame.image.load('bullet.png')

bulletX = 0

bulletY = 480


bulletY_change = 10

bullet_state = 'ready'


# Score

score_value = 0

font = pygame.font.Font('freesansbold.ttf', 32)


textX = 10

textY = 10


# Game Over Text

over_font = pygame.font.Font('freesansbold.ttf', 64)


# Clock

clock = 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))


一只甜甜圈
浏览 198回答 2
2回答

阿波罗的战车

角色不会移动player_change在主应用程序循环中,播放器的移动连续设置为 0。此外,玩家的位置不断初始化:def game_loop(playerX, playerY, player_change, bulletX, bulletY, bulletX_change, >bulletY_change, bullet_state, bulletImg):&nbsp; &nbsp;running = True&nbsp; &nbsp;while running:&nbsp; &nbsp; &nbsp; &nbsp;playerX = 370&nbsp; &nbsp; &nbsp; &nbsp;playerY = 480&nbsp; &nbsp; &nbsp; &nbsp;player_change = 0初始化playerX,playerY和player_change循环之前:def game_loop(playerX, playerY, player_change, bulletX, bulletY, bulletX_change, bulletY_change, bullet_state, bulletImg):&nbsp; &nbsp; running = True&nbsp; &nbsp; player_change = 0&nbsp; &nbsp; playerX = 370&nbsp; &nbsp; playerY = 480&nbsp; &nbsp; while running:&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; playerX += player_change&nbsp; &nbsp; &nbsp; &nbsp; # [...]子弹不会射如果要发射子弹,则必须设置子弹的初始位置bulletX, bulletY = playerX, playerY并设置bullet_state = 'fire':def game_loop(playerX, playerY, player_change, bulletX, bulletY, bulletX_change, bulletY_change, bullet_state, bulletImg):&nbsp; &nbsp; # [...]&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; while running:&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; playerX += player_change&nbsp; &nbsp; &nbsp; &nbsp; for event in pygame.event.get():&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; running = False&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # If keystroke is pressed check whether its left or right&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.type == pygame.KEYDOWN:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # [...]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if event.key == pygame.K_SPACE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if bullet_state is 'ready':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #bullet_sound = mixer.Sound('laser.wav')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #bullet_sound.play()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Get the current x coordinate of the spaceship&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bulletX, bulletY = playerX, playerY&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bullet_state = 'fire'注意,global变量bullet_statedef fire_bullet(x, y):&nbsp; &nbsp;global bullet_state&nbsp; &nbsp;bullet_state = 'fire'与 中的局部变量不同bullet_state,game_loop因为bullet_state它是一个参数game_loop,因此在不同的范围内是一个完全不同的变量,其名称随便相同:def game_loop(playerX, playerY, player_change, bulletX, bulletY,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bulletX_change, bulletY_change,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bullet_state, # <--- that is a new variable in local scope&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #&nbsp; &nbsp; &nbsp; and not the "global" bullet_state&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bulletImg):&nbsp; &nbsp; # [...]

千巷猫影

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;player_change&nbsp;=&nbsp;0 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playerX&nbsp;+=&nbsp;player_change尝试交换这两行的顺序。否则,当 X 位置更新时,更改将始终为零。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python