- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我的角色的碰撞框与某个 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/
我已经开始开发一个小型多人赛车游戏,显然我们正在使用半条命、雷神之锤和虚幻引擎使用的所有玩家预测、航位推算和延迟补偿技术——但是我们计划在游戏中也有几十辆人工智能汽车. 最初我们决定简单地向所有客户端
我是编码新手,最近一直在尝试制作 2D 平台游戏,但遇到了一个我似乎无法解决的问题,我的 NPC 不会在 map 上跟随玩家,也不会它对碰撞使用react,欢迎任何帮助。 我还一直在考虑创建一种算法,
当我的角色的碰撞框与某个 NPC 的碰撞框发生碰撞时,我在屏幕上显示文本框时遇到问题。 第一部分代码是我创建的模块文件的一部分,我在其中创建了供游戏使用的 Assets 。这是我创建要在主文件中使用的
出于某些研究目的,我构建了一款小型坦克游戏,其中有 1 辆由玩家控制的坦克和一辆或多辆 NPC 坦克。 现在我希望这些 NPC 坦克能够穿越他们不了解的领域。如果障碍物在一定范围内,它们可以检测到。如
我现在正在上 Java 类(class),我们应该制作一个基于文本的游戏。我们游戏中的选项之一是允许玩家在 3 种船型之间进行选择;小型、中型和大型。根据他们的选择,他们将能够选择乘船旅行的人数。小型
我在士兵动画师中有一个 Grounded 过渡,默认情况下这是从这个过渡开始的。然后我在 3 秒后执行此操作,它会在从 Grounded 到 Rifle_Aiming_Idle 的两个转换之间缓慢变化
使用当前代码,NPC 将检测到并转向播放所需动画的玩家。但是,NPC 只会快速面向玩家,而不会在玩家在 NPC 范围内绕过时继续旋转。 我想对此进行修改,以便当玩家处于对撞机范围内时,NPC 始终如一
好的,这将是一些我需要帮助的代码。 前言 下面的代码现在描述了 FSM 的实现,因为我使用的是 unity,这允许我根据我的状态/ Action 和决策创建 Assets 。 州 [CreateAss
这个问题与this one (Using C# 5 async to wait for something that executes over a number of game frames)有关.
我想让游戏中的角色执行部分随机但也受偏好影响的 Action 。例如,如果一个角色感到生气,他们大喊大叫的可能性比讲笑话的可能性更大。所以我在思考如何确定角色会采取什么样的行动。以下是我的想法。 解决
我的问题的核心: 我正在尝试使用网格包的convertUnit、convertX 和convertY 函数将npc 单位转换为本地单位。 (npc=归一化父坐标,可能称为 ndc 单位,归一化设备坐标
我正在用java为我工作的mod公司制作一个住房插件(Bukkit 1.6.4)。我需要使用NPCS。我尝试使用 Cizizens API,但当我尝试创建它时总是出现错误: java.lang.Nul
我想在每个面板的底部画一个框,每个面板可能有不同的 y 范围。该框应始终位于 x 轴上的 3 到 7 之间,并且从面板底部的边框到 y 轴高度的 5% 左右。 所以我想指定例如的坐标annotatio
如何在 ggplot 中获取 geom_point 的 x、y 坐标,其中引用系是整个绘制的图像? 我可以使用以下命令创建一个带有一些 geom_points 的 ggplot: library(gg
抱歉,标题令人困惑,我不太确定如何措辞。 在最近发现 HTML5 的 Canvas 元素的强大功能后,我刚刚开始从事 2D 游戏开发。我正在进行我的第一个基本项目来学习诀窍。这款游戏将允许玩家加入游戏
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 6 年前。 当我加入我的服务器时,“测试”会
我已经研究了大约一个小时,研究如何让 NPC “排斥”,意思是在 Unity3d 中逃离玩家。我到处搜索这个,我发现了很多关于如何让 NPC 跑向玩家但从不排斥的东西。我是游戏开发的新手,所以我不知道
我正在使用 C++ 开发一个 2D 游戏引擎,并考虑过将 NPC 脚本(例如对话和菜单选择项)移动到 LUA 中。我很难弄清楚如何等待用户/客户端的响应。我的场景如下- 用户点击 NPC。 NPC 启
我正在使用 C++ 开发一个游戏引擎,使用 Lua 处理 NPC 行为。我在设计过程中遇到了一些问题。 对于需要多于一个框架来执行的所有事情,我想使用一个进程链表(它们是 C++ 类)。所以这个: g
我尝试更正我的代码中的最后一个错误。我已经将 VS6.0 迁移到 VS2008。 我的最后一个错误在我的 SimpleNPC.cpp 文件中: .\SimpleNPC.cpp(216) : error
我是一名优秀的程序员,十分优秀!