pygame 中敌人不会自行移动
好吧,所以我的敌人只有在我移动角色时才会移动,但有时它也会完全从游戏窗口中产生。还有背景、玩家和敌人……在我按下移动按钮之前不会弹出,我对此感到非常抱歉 lmfao 也对我被告知将它们放在那里以提醒 wtf 的文字和内容感到抱歉
导入pygame
导入随机
pygame.init()
screen = pygame.display.set_mode((800, 600))
background = pygame.image .load("BackgroundImg.jpg")
pygame.display.set_caption("真正的英雄")
icon = pygame.image.load('Icon.png')
pygame.display.set_icon(icon)
playerImg = pygame.image.load('Player.png')
玩家X = 370
玩家Y = 480
playerX_change = 0
enemyImg = pygame.image.load('Enemy.jpg')
enemyX = random.randint(0, 746)
enemyY = random.randint(50, 746)
enemyX_change = 0.2
enemyY_change = 40
def 玩家(x, y):
screen.blit(playerImg, (x, y))
def敌人(x, y):
screen.blit(enemyImg, (x, y))
running = True
运行时:
screen.fill((0, 0, 0))
# Background Image
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.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.2
enemyY += enemyY_change
elif enemyX >= 736:
enemyX_change = -0.2
enemyY += enemyY_change
enemy(enemyX, enemyY)
player(playerX, playerY)
pygame.display.update()
Alright so the enemy I have only moves when I move my character, but it also sometimes spawns completely out of the game's window. Also the background, player and enemy just kinda... Dont pop up until I press a movement button, im so sorry for this lmfao Also sorry about the text and stuff I was told to put them there for reminders on wtf to
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.jpg')
enemyX = random.randint(0, 746)
enemyY = random.randint(50, 746)
enemyX_change = 0.2
enemyY_change = 40
def player(x, y):
screen.blit(playerImg, (x, y))
def enemy(x, y):
screen.blit(enemyImg, (x, y))
running = True
while running:
screen.fill((0, 0, 0))
# Background Image
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.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.2
enemyY += enemyY_change
elif enemyX >= 736:
enemyX_change = -0.2
enemyY += enemyY_change
enemy(enemyX, enemyY)
player(playerX, playerY)
pygame.display.update()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
敌人仅在按下移动键时才会移动的原因是因为
if event.type == pygame.KEYDOWN:
if 语句中有敌人移动代码。您也仅在按下移动键时更新屏幕,因为 pygame.display.update() 也在该 if 语句内。您需要将不应仅在按下某个键时运行的所有代码移出 if 语句。The reason as to why the enemy only moves when you press a movement key is because you have the enemy movement code inside of the
if event.type == pygame.KEYDOWN:
if statement. You are also only updating the screen when you press a movement key because thepygame.display.update()
is also inside that ifstatement. You need to move any code that shouldn't only be run when a key is pressed out of the if statement.