当我的角色的碰撞盒与某个NPC的碰撞盒碰撞时,我在向屏幕显示文本框时遇到问题。
这段代码的第一部分是我创建的模块文件的一部分,我创建的资源供游戏使用。这是我创建我的文本框对象以在主文件中使用的地方。
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所建议的那样,我把主要循环中的文本框调用条件放在条件之前,如果它与NPC发生冲突,我会让PC离开。如下所示。遗憾的是,即使有这样的安排,文本框仍然没有出现。
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
答案 0 :(得分:2)
在blot文本框后,你真的打电话给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