python - 角色与 NPC 碰撞时显示文本框的问题

标签 python pygame collision-detection

当我的角色的碰撞框与某个 NPC 的碰撞框发生碰撞时,我在屏幕上显示文本框时遇到问题。

第一部分代码是我创建的模块文件的一部分,我在其中创建了供游戏使用的 Assets 。这是我创建要在主文件中使用的文本框对象的地方。

import pygame, sys
from pygame.locals import *

class stuff:
    def __init__(self, Surface):
        self.surface = Surface
        self.readyfornextbox = False
        self.textboxlist = []

    def textbox(self, textinput, textcolor,location): #extra is basically asking whether or not the programmer wants t oclose
        self.readyfornextbox = True #Checks if player is going to be clicking for next text box to appear on screen
        self.font = pygame.font.Font(None, 24)

        text = self.font.render(textinput, 1, (255, 0, 0))
        self.textboxlist.append([text,location])


    def ready(self, ready):
        if self.textboxlist:
            self.textboxlist.remove(self.textboxlist[0])

        if(not self.textboxlist):
            self.readyfornextbox=False

    def draw_textbox(self):
        if(self.readyfornextbox==True):
            pygame.draw.rect(self.surface, (0, 255,0), self.textboxlist[0][1])
            self.surface.blit(self.textboxlist[0][0], ((self.textboxlist[0][1].x + 25), (self.textboxlist[0][1].y + 25)))

下一段代码来自游戏本身的主文件。当玩家的碰撞盒与 NPC 的碰撞盒发生碰撞的条件满足时,我会调用此方法将文本框绘制到屏幕上。我在主游戏循环之外调用它。

if PC_ColBox.colliderect(OM_ColBox):
    blah.textbox("Hello there, young man", (255, 0, 0), bottom)

由于某种原因,文本框没有显示在屏幕上。我之前曾使用相同的条件语句毫无问题地在玩家和 NPC 之间创建边界,以阻止玩家与 NPC 重叠。所以我不认为条件是问题所在。此外,当文本框在没有条件语句的情况下单独调用时,它显示得很好。

提前感谢您的帮助。

编辑 根据两个用户的要求,我将在下面粘贴整个主文件的代码。非常感谢你们尝试提供帮助:)

import pygame, sys
from pygame.locals import *
import os, time, random
import essentials
import pyganim


#bleh = random.randint(2, 30)
#print(bleh)

bottom = pygame.Rect(120, 450, 760, 100)

middle = pygame.Rect(120, 250, 760, 100)

top = pygame.Rect(120, 50, 760, 100)

FOV = Rect(701, 1570, 1000, 600)

pygame.init()
Axis_Change_x = 0
Axis_Change_y = 0
Change = 0
x_Axis_Move = False
y_Axis_Move = False
CamMoving = False
CamSpeed = 0

UP = False
RIGHT = False
DOWN = False
LEFT = False

BG = pygame.display.set_mode((1000, 600))
BG.fill((0, 0, 0))
blah=essentials.stuff(BG)
BG1 = pygame.image.load("firstbackground.png")
##blah.textbox("This...", (255, 0, 0), top)
##blah.textbox("is a...", (255, 0, 0), middle)
##blah.textbox("test...", (255, 0, 0), bottom)

#blah.textbox("CHECK!", (255, 0, 0))


#Evil Old Man
OM_Stand_Front = pygame.image.load("oldman.png")
OM_ColBox = Rect(520, 220, 25, 61)

#Player Character
PC_WalkF1 = pygame.image.load(os.path.join("PC", "WalkF1.png"))
PC_WalkF2 = pygame.image.load(os.path.join("PC", "WalkF2.png"))
PC_WalkB1 = pygame.image.load(os.path.join("PC", "WalkB1.png"))
PC_WalkB2 = pygame.image.load(os.path.join("PC", "WalkB2.png"))
PC_WalkL1 = pygame.image.load(os.path.join("PC", "WalkL1.png"))
PC_WalkL2 = pygame.image.load(os.path.join("PC", "WalkL2.png"))
PC_WalkR1 = pygame.image.load(os.path.join("PC", "WalkR1.png"))
PC_WalkR2 = pygame.image.load(os.path.join("PC", "WalkR2.png"))
PC_ColBox = Rect(500, 400, 67, 79)

#Cyclops
C_WalkF1 = pygame.image.load(os.path.join("Monster", "monsterWalkF1.png"))
C_WalkF2 = pygame.image.load(os.path.join("Monster", "monsterWalkF2.png"))
C_WalkB1 = pygame.image.load(os.path.join("Monster", "monsterWalkB1.png"))
C_WalkB2 = pygame.image.load(os.path.join("Monster", "monsterWalkB2.png"))
C_WalkL1 = pygame.image.load(os.path.join("Monster", "monsterWalkL1.png"))
C_WalkL2 = pygame.image.load(os.path.join("Monster", "monsterWalkL2.png"))
C_WalkR1 = pygame.image.load(os.path.join("Monster", "monsterWalkR1.png"))
C_WalkR2 = pygame.image.load(os.path.join("Monster", "monsterWalkR2.png"))

if PC_ColBox.colliderect(OM_ColBox):
    blah.textbox("Hello there, young man", (255, 0, 0), bottom)


while True: #The main game loop
    CurrentTime = pygame.time.get_ticks()

    #blah.textbox("This is a test 2232323", (255, 0, 0))


    for event in pygame.event.get():
        if event.type == KEYUP:
            if event.key == K_d:
                Change = 0
                CamSpeed = 0
                if not event.key == K_a and not event.key == K_w:
                    UP = True
                    LEFT = True
                if not event.key == K_a and not event.key == K_s:
                    DOWN = True
                    LEFT = True

                if not event.key == K_a:
                    LEFT = True
                    DOWN = False
                    UP = False
                else:
                    UP = False
                    RIGHT = False
                    DOWN = False
                    LEFT = False

            if event.key == K_a:
                Change = 0
                CamSpeed = 0
                if not event.key == K_d and not event.key == K_w:
                    UP = True
                    RIGHT = True
                if not event.key == K_d and not event.key == K_s:
                    DOWN = True
                    RIGHT = True

                if not event.key == K_d:
                    RIGHT = True
                    UP = False
                    DOWN = False
                    LEFT = False
                else:
                    UP = False
                    RIGHT = False
                    DOWN = False
                    LEFT = False

            if event.key == K_w:
                Change = 9
                CamSpeed = 0
                if not event.key == K_s:
                    DOWN = True
                    Change = 9
                else:
                    DOWN = False

            if event.key == K_s:
                Change = 0
                CamSpeed = 0
                if not event.key == K_w:
                    UP = True
                else:
                    UP = False
                    CamSpeed = 0






        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()


            if (event.key == K_d and not event.key == K_a and not event.key == K_w and not event.key == K_s):
                #BG.blit(AL, WizardHitBox, AL_right_stand)
                Change = 9
                CamSpeed = 9
                UP = False
                RIGHT = True
                DOWN = False
                LEFT = False


            elif (event.key == K_a and not event.key == K_w and not event.key == K_s and not event.key == K_d):
                Change = 9
                CamSpeed = 9
                UP = False
                RIGHT = False
                DOWN = False
                LEFT = True
                Axis_Change_x = 9

            elif (event.key == K_w and not event.key == K_a and not event.key == K_s and not event.key == K_d):
                Change = 9
                CamSpeed = 9
                UP = True
                RIGHT = False
                DOWN = False
                LEFT = False

            elif (event.key == K_s and not event.key == K_a and not event.key == K_w and not event.key == K_d):
                Change = 9
                CamSpeed = 9
                UP = False
                RIGHT = False
                DOWN = True
                LEFT = False
            if event.key == K_SPACE:
                blah.ready(not blah.readyfornextbox)

        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    if RIGHT == True:
        PC_ColBox.x += Change
        if PC_ColBox.colliderect(OM_ColBox):
            PC_ColBox.x -= Change



    if LEFT == True:
        PC_ColBox.x -= Change
        if PC_ColBox.colliderect(OM_ColBox):
            PC_ColBox.x += Change

    if UP == True:
        PC_ColBox.y -= Change
        if PC_ColBox.colliderect(OM_ColBox):
            PC_ColBox.y += Change

    if DOWN == True:
        PC_ColBox.y += Change
        if PC_ColBox.colliderect(OM_ColBox):
            PC_ColBox.y -= Change

    #BG.fill((0, 0, 0))
    BG.blit(BG1, Rect(0,0,1000,800), FOV)
    BG.blit(OM_Stand_Front, (500, 200))
    BG.blit(PC_WalkF1, (PC_ColBox.x, PC_ColBox.y))
    blah.draw_textbox()

    pygame.display.update()
    pygame.time.wait(10)

编辑 2 根据 Junuxx 的建议,我将在主循环内调用文本框的条件放在 PC 与 NPC 碰撞时移开之前。如下所示。不幸的是,即使这样安排,文本框仍然没有显示。

if PC_ColBox.colliderect(OM_ColBox):
    blah.textbox("Hello there, young man", (255, 0, 0), bottom)

if RIGHT == True:
    PC_ColBox.x += Change
    if PC_ColBox.colliderect(OM_ColBox):
        PC_ColBox.x -= Change

if LEFT == True:
    PC_ColBox.x -= Change
    if PC_ColBox.colliderect(OM_ColBox):
        PC_ColBox.x += Change

最佳答案

你真的打电话pygame.display.update()吗?位图文本框之后?这是需要在屏幕上显示任何更改的!

编辑: 在查看更多代码后,看来您应该将碰撞检测移到主循环内。现在,您只需在进入主循环之前开始检查一次碰撞。

当然,考虑到他们的起始位置,两个角色在开始移动之前不可能发生碰撞。

如果将其移到 if RIGHT == True 条件之前,应该可以工作。

if PC_ColBox.colliderect(OM_ColBox):
    blah.textbox("Hello there, young man", (255, 0, 0), bottom)


if RIGHT == True:
    PC_ColBox.x += Change
    if PC_ColBox.colliderect(OM_ColBox):
        PC_ColBox.x -= Change

关于python - 角色与 NPC 碰撞时显示文本框的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24094667/

相关文章:

java - 如何在Java中制作一个实心矩形?

c# - 插值对象碰撞卡住

php - 如何检查哈希冲突

python - 需要在 VirtualEnv Session 中重置环境变量

python - 如何存储从请求收到的cookie?

python - 使用 while 循环的巴比伦算法函数

python - 在cx_Freeze中编译后无法打开图像

python - 我想打乱这些变量,然后将它们组合到另一个函数中以将图像打印到屏幕上

python - 为什么套接字行为依赖于打印语句?

python - 失去健康之间的延迟