pygame 中敌人不会自行移动

发布于 2025-01-18 23:42:42 字数 1970 浏览 0 评论 0原文

好吧,所以我的敌人只有在我移动角色时才会移动,但有时它也会完全从游戏窗口中产生。还有背景、玩家和敌人……在我按下移动按钮之前不会弹出,我对此感到非常抱歉 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢华的一滴泪 2025-01-25 23:42:42

敌人仅在按下移动键时才会移动的原因是因为 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 the pygame.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文