按下另一个按钮或使用鼠标时角色无法移动,子弹也不会发射
因此,我已经开始为敌人和玩家工作,但是当我按下左右的按钮(例如要射击的空间)时,玩家无法移动,子弹本身实际上不会出来或完全出现。它在pygame和ima中说实话,我不知道有什么问题
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((800, 600))
background = pygame.image.load("BackgroundImg.jpg")
pygame.display.set_caption("Real Hero")
icon = pygame.image.load('Icon.png')
pygame.display.set_icon(icon)
playerImg = pygame.image.load('Player.png')
playerX = 370
playerY = 480
playerX_change = 0
enemyImg = pygame.image.load('Enemy.png')
enemyX = random.randint(0, 800)
enemyY = random.randint(50, 150)
enemyX_change = 0.1
enemyY_change = 40
bulletImg = pygame.image.load('Bullet.png')
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 10
bullet_state = "ready"
def player(x, y):
screen.blit(playerImg, (x, y))
def enemy(x, y):
screen.blit(enemyImg, (x, y))
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bulletImg, (x + 16, y + 10))
running = True
while running:
screen.fill((0, 0, 0))
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.3
if event.key == pygame.K_RIGHT:
playerX_change = 0.3
if event.key == pygame.K_SPACE:
fire_bullet(playerX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
enemyX += enemyX_change
if enemyX <= 0:
enemyX_change = 0.1
enemyY += enemyY_change
elif enemyX >= 736:
enemyX_change = -0.1
enemyY += enemyY_change
if bullet_state is "fire":
fire_bullet(playerX, bulletY)
bulletY -= bulletY_change
enemy(enemyX, enemyY)
player(playerX, playerY)
pygame.display.update()
So I've gotten movement to work for enemy and the player, but when I press a button outside of left or right, like space which will be to shoot, the player cant move and the bullet itself wont actually come out or appear at all. Its in pygame and ima be honest I have no clue whats wrong so im sorry it looks so long and weird
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((800, 600))
background = pygame.image.load("BackgroundImg.jpg")
pygame.display.set_caption("Real Hero")
icon = pygame.image.load('Icon.png')
pygame.display.set_icon(icon)
playerImg = pygame.image.load('Player.png')
playerX = 370
playerY = 480
playerX_change = 0
enemyImg = pygame.image.load('Enemy.png')
enemyX = random.randint(0, 800)
enemyY = random.randint(50, 150)
enemyX_change = 0.1
enemyY_change = 40
bulletImg = pygame.image.load('Bullet.png')
bulletX = 0
bulletY = 480
bulletX_change = 0
bulletY_change = 10
bullet_state = "ready"
def player(x, y):
screen.blit(playerImg, (x, y))
def enemy(x, y):
screen.blit(enemyImg, (x, y))
def fire_bullet(x, y):
global bullet_state
bullet_state = "fire"
screen.blit(bulletImg, (x + 16, y + 10))
running = True
while running:
screen.fill((0, 0, 0))
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -0.3
if event.key == pygame.K_RIGHT:
playerX_change = 0.3
if event.key == pygame.K_SPACE:
fire_bullet(playerX, bulletY)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
enemyX += enemyX_change
if enemyX <= 0:
enemyX_change = 0.1
enemyY += enemyY_change
elif enemyX >= 736:
enemyX_change = -0.1
enemyY += enemyY_change
if bullet_state is "fire":
fire_bullet(playerX, bulletY)
bulletY -= bulletY_change
enemy(enemyX, enemyY)
player(playerX, playerY)
pygame.display.update()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是凹痕:
It is a matter of Indentation: