Python中文
首页
教程
问答
标签
搜索
登录
注册
Pygame Collide Rect精度问题
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我在为大学考试设计一个克隆的吃豆人。 我使用pygame,在处理鬼魂与墙的碰撞时遇到了一些问题。你知道吗</p> <p>例如,当重影与左侧的墙碰撞时,重影矩形的左侧采用墙矩形右侧的值:</p> <pre><code>rectGhost.left = blockRect.right </code></pre> <p>但是在多次碰撞(在墙的左侧)之后,鬼影越过墙,就好像鬼影矩形通过运动越过墙,并且在墙的左侧检测到碰撞,然后移动到墙的右侧的鬼影矩形的左侧。你知道吗</p> <p><a href="http://i.stack.imgur.com/ex9Aq.png" rel="nofollow">Collision issue example</a></p> <p><strong>这是幽灵的职业:</strong></p> <pre><code>import character, random from globalVar import * class ghost(character.character): def __init__(self, imgSprite): super(ghost, self).__init__(imgSprite) self._direction = random.choice([LEFT, RIGHT, UP, DOWN]) def catch(self, wall): # Keep the direction if self._direction == LEFT: self.moveToLeft() if self._direction == RIGHT: self.moveToRight() if self._direction == UP: self.moveToUp() if self._direction == DOWN: self.moveToDown() # Get ghost position rectGhost = self.getRectSprite() # Collision with wall for blockRect in wall: if rectGhost.colliderect(blockRect): # Check collision with the wall (wall contain a list of Rect object) if self._direction == LEFT: rectGhost.left = blockRect.right self._direction = random.choice([RIGHT, UP, DOWN]) if self._direction == RIGHT: rectGhost.right = blockRect.left self._direction = random.choice([LEFT, UP, DOWN]) if self._direction == UP: rectGhost.top = blockRect.bottom self._direction = random.choice([LEFT, RIGHT, DOWN]) if self._direction == DOWN: rectGhost.bottom = blockRect.top self._direction = random.choice([LEFT, RIGHT, UP]) </code></pre> <p><strong>这是超类字符:</strong></p> <pre><code>from globalVar import * class character: def __init__(self, imgSprite): self._imageSprite = imgSprite self._spriteRect = self._imageSprite.get_rect() self._dimStep = 10 self._direction = 0 def setCharPos(self, charPos): self._spriteRect.centerx = charPos[0] self._spriteRect.centery = charPos[1] def moveToLeft(self): self._spriteRect.x -= self._dimStep def moveToRight(self): self._spriteRect.x += self._dimStep def moveToUp(self): self._spriteRect.y -= self._dimStep def moveToDown(self): self._spriteRect.y += self._dimStep def drawSprite(self): WINDOWSURF.blit(self._imageSprite, self._spriteRect) def getRectSprite(self): return self._spriteRect def setDirection(self, direction): self._direction = direction </code></pre> <p><strong>这是主要的:</strong></p> <pre><code>def main(): thisPacman = pacman.pacman(pacmanCImg) # Ghot obj thisGhost_a = ghost.ghost(ghostAImg) # Ghot obj thisGhost_o = ghost.ghost(ghostOImg) # Ghot obj thisGhost_p = ghost.ghost(ghostPImg) # Ghot obj thisGhost_r = ghost.ghost(ghostRImg) # Ghot obj # Coords of wall wall = thisLevel.getBlocksListPos() # Coords of Ghosts ghostsStartPos = thisLevel.getGhostsPos() # Start pos azure thisGhost_a.setCharPos(ghostsStartPos[0]) # Start pos ghost orange thisGhost_o.setCharPos(ghostsStartPos[1]) # Start pos ghost purple thisGhost_p.setCharPos(ghostsStartPos[2]) # Start pos ghost red thisGhost_r.setCharPos(ghostsStartPos[3]) while running: thisGhost_a.catch(wall) thisGhost_o.catch(wall) thisGhost_p.catch(wall) thisGhost_r.catch(wall) thisGhost_a.drawSprite() thisGhost_o.drawSprite() thisGhost_p.drawSprite() thisGhost_r.drawSprite() pygame.display.update() FPSCLOCK.tick(8) </code></pre> <p>有什么想法吗? 非常感谢您的关注!你知道吗</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>如果不熟悉你的代码,我很难给你一个答案。但是,您可以尝试从catch()方法调试代码:</p> <pre><code># Collision with wall import pdb; pdb.set_trace() </code></pre> <p>这可能会给你更多的关于正在发生的事情的信息。你知道吗</p> <p>我还注意到,catch()方法中的if语句出现在幽灵移动之后。当然,在允许移动之前需要检查碰撞吗?你知道吗</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
为什么我的神经网络模型的准确性不能在这个训练集上得到提高?
10 回答
为什么我的神经网络模型的权重变化不大?
6 回答
为什么我的神经网络的成本不断增加?
10 回答
为什么我的神经网络的输入pickle文件是19GB?
9 回答
为什么我的神经网络给属性错误?“非类型”对象没有属性“形状”
6 回答
为什么我的神经网络训练这么慢?
10 回答
为什么我的神经网络输出错误?
2 回答
为什么我的神经网络预测适用于MNIST手绘图像时是正确的,而适用于我自己的手绘图像时是不正确的?
4 回答
为什么我的神经网络验证精度比我的训练精度高,而且它们都是常数?
7 回答
为什么我的私人用户间聊天会显示在其他用户的聊天档案中?
10 回答
为什么我的积分的绝对误差估计值大于积分(使用scipy.integrate.nqad)?
9 回答
为什么我的积层回归器得分比它的组件差?
9 回答
为什么我的移动方法不起作用?
10 回答
为什么我的稀疏张量不能转换成张量
10 回答
为什么我的稀疏张量不能转换成张量?
7 回答
为什么我的程序“停止”了?
8 回答
为什么我的程序一直试图占用所有可用的CPU
2 回答
为什么我的程序不使用指定的代理
7 回答
为什么我的程序不工作(python帮助中的反向函数)?
6 回答
为什么我的程序不工作时,我使用多处理模块
9 回答