- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我试图用 pygame 制作一个我们中间的类型游戏。我刚开始,所以我没有太多东西,现在正在制作 map 。但是,我正在努力解决的一件事是碰撞逻辑。 map 现在有一个细长的八边形形状,但我认为无论形状如何,我都会使用类似 pygame 多边形的东西。当我运行我现在拥有的代码时,它会检查我的玩家(pygame 矩形)和墙壁(pygame 多边形)之间的碰撞,它说:TypeError: Argument must be rect style object
我发现这是因为 pygame 多边形返回一个矩形,但在碰撞检查器中没有以这种方式分类。我尝试了一个名为 collision 的库,并归功于碰撞检测付出了巨大的努力,但玩家仍然能够穿过墙壁。旁注:我保存了我使用这个库的代码,如果有人想看到它,也许可以改进我的缺点。
无论如何,把这一切归结起来:
我需要一种方法来检测多边形和矩形之间的碰撞(真的,最好在 pygame 中)
感谢您提供的任何帮助,如果您有问题/要求,请发表评论。
这是我的代码:
import pygame
pygame.init()
W, H=500, 500
screen = pygame.display.set_mode([500, 500])
running = True
bcg=(200, 200, 200)
red=(255, 0 ,0)
purp=(255, 0, 255)
wall=(100, 100, 100)
class player:
def bg(self):
screen.fill(bcg)
x,y=self.x,self.y
self.outer=(
(x,y),
(x+800, y),
(x+1200, y+200),
(x+1200, y+600),
(x+800, y+800),
(x, y+800),
(x-400, y+600),
(x-400, y+200),
(x,y),
(x, y+50),
(x-350, y+225),
(x-350, y+575),
(x, y+750),
(x+800, y+750),
(x+1150, y+575),
(x+1150, y+225),
(x+800, y+50),
(x, y+50)
)
pygame.draw.polygon(screen, wall, self.outer)
def __init__(self, color, size=20, speed=0.25):
self.x=0
self.y=0
self.col=color
self.size=size
self.speed=speed
def draw(self):
s=self.size
self.rect=pygame.Rect(W/2-s/2, H/2-s/2, self.size, self.size)
pygame.draw.rect(screen, self.col, self.rect)
def move(self, x, y):
x*=self.speed
y*=self.speed
if not self.rect.colliderect(self.outer):
self.x+=x
self.y+=y
p=player(red)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
p.bg()
keys=pygame.key.get_pressed()
if keys[pygame.K_a]: p.move(1, 0)
if keys[pygame.K_d]: p.move(-1, 0)
if keys[pygame.K_w]: p.move(0, 1)
if keys[pygame.K_s]: p.move(0, -1)
p.draw()
pygame.display.update()
pygame.quit()
最佳答案
写一个函数 collideLineLine
测试线段是否相交。这个函数的算法在问题pygame, detecting collision of a rotating rectangle的回答中有详细解释。 :
def collideLineLine(l1_p1, l1_p2, l2_p1, l2_p2):
# normalized direction of the lines and start of the lines
P = pygame.math.Vector2(*l1_p1)
line1_vec = pygame.math.Vector2(*l1_p2) - P
R = line1_vec.normalize()
Q = pygame.math.Vector2(*l2_p1)
line2_vec = pygame.math.Vector2(*l2_p2) - Q
S = line2_vec.normalize()
# normal vectors to the lines
RNV = pygame.math.Vector2(R[1], -R[0])
SNV = pygame.math.Vector2(S[1], -S[0])
RdotSVN = R.dot(SNV)
if RdotSVN == 0:
return False
# distance to the intersection point
QP = Q - P
t = QP.dot(SNV) / RdotSVN
u = QP.dot(RNV) / RdotSVN
return t > 0 and u > 0 and t*t < line1_vec.magnitude_squared() and u*u < line2_vec.magnitude_squared()
编写函数
colideRectLine
测试矩形和线段是否相交。要测试线段是否与矩形相交,您必须测试它是否与矩形的 4 条边中的任何一条相交:
def colideRectLine(rect, p1, p2):
return (collideLineLine(p1, p2, rect.topleft, rect.bottomleft) or
collideLineLine(p1, p2, rect.bottomleft, rect.bottomright) or
collideLineLine(p1, p2, rect.bottomright, rect.topright) or
collideLineLine(p1, p2, rect.topright, rect.topleft))
下一个函数
collideRectPolygon
测试多边形和矩形是否相交。这可以通过在循环中针对矩形测试多边形上的每个线段来实现:
def collideRectPolygon(rect, polygon):
for i in range(len(polygon)-1):
if colideRectLine(rect, polygon[i], polygon[i+1]):
return True
return False
最后你可以使用
collideRectPolygon
用于碰撞测试。但是请注意,对于测试,您需要像玩家在移动一样使用多边形:
class player:
def bg(self):
screen.fill(bcg)
self.outer = self.createPolygon(self.x, self.y)
pygame.draw.polygon(screen, wall, self.outer)
def createPolygon(self, x, y):
return [
(x,y), (x+800, y), (x+1200, y+200), (x+1200, y+600),
(x+800, y+800), (x, y+800), (x-400, y+600), (x-400, y+200),
(x,y), (x, y+50), (x-350, y+225), (x-350, y+575),
(x, y+750), (x+800, y+750), (x+1150, y+575), (x+1150, y+225),
(x+800, y+50),(x, y+50)]
# [...]
def move(self, x, y):
x *= self.speed
y *= self.speed
polygon = self.createPolygon(self.x + x, self.y + y)
if not collideRectPolygon(self.rect, polygon):
self.x += x
self.y += y
另见
Collision and Intersection - Rectangle and polygon
import pygame
pygame.init()
W, H=500, 500
screen = pygame.display.set_mode([500, 500])
running = True
bcg=(200, 200, 200)
red=(255, 0 ,0)
purp=(255, 0, 255)
wall=(100, 100, 100)
def collideLineLine(l1_p1, l1_p2, l2_p1, l2_p2):
# normalized direction of the lines and start of the lines
P = pygame.math.Vector2(*l1_p1)
line1_vec = pygame.math.Vector2(*l1_p2) - P
R = line1_vec.normalize()
Q = pygame.math.Vector2(*l2_p1)
line2_vec = pygame.math.Vector2(*l2_p2) - Q
S = line2_vec.normalize()
# normal vectors to the lines
RNV = pygame.math.Vector2(R[1], -R[0])
SNV = pygame.math.Vector2(S[1], -S[0])
RdotSVN = R.dot(SNV)
if RdotSVN == 0:
return False
# distance to the intersection point
QP = Q - P
t = QP.dot(SNV) / RdotSVN
u = QP.dot(RNV) / RdotSVN
return t > 0 and u > 0 and t*t < line1_vec.magnitude_squared() and u*u < line2_vec.magnitude_squared()
def colideRectLine(rect, p1, p2):
return (collideLineLine(p1, p2, rect.topleft, rect.bottomleft) or
collideLineLine(p1, p2, rect.bottomleft, rect.bottomright) or
collideLineLine(p1, p2, rect.bottomright, rect.topright) or
collideLineLine(p1, p2, rect.topright, rect.topleft))
def collideRectPolygon(rect, polygon):
for i in range(len(polygon)-1):
if colideRectLine(rect, polygon[i], polygon[i+1]):
return True
return False
class player:
def bg(self):
screen.fill(bcg)
self.outer = self.createPolygon(self.x, self.y)
pygame.draw.polygon(screen, wall, self.outer)
def createPolygon(self, x, y):
return [
(x,y), (x+800, y), (x+1200, y+200), (x+1200, y+600),
(x+800, y+800), (x, y+800), (x-400, y+600), (x-400, y+200),
(x,y), (x, y+50), (x-350, y+225), (x-350, y+575),
(x, y+750), (x+800, y+750), (x+1150, y+575), (x+1150, y+225),
(x+800, y+50),(x, y+50)]
def __init__(self, color, size=20, speed=0.25):
self.x=0
self.y=0
self.col=color
self.size=size
self.speed=speed
def draw(self):
s=self.size
self.rect=pygame.Rect(W/2-s/2, H/2-s/2, self.size, self.size)
pygame.draw.rect(screen, self.col, self.rect)
def move(self, x, y):
x *= self.speed
y *= self.speed
polygon = self.createPolygon(self.x + x, self.y + y)
if not collideRectPolygon(self.rect, polygon):
self.x += x
self.y += y
p=player(red)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
p.bg()
keys=pygame.key.get_pressed()
if keys[pygame.K_a]: p.move(1, 0)
if keys[pygame.K_d]: p.move(-1, 0)
if keys[pygame.K_w]: p.move(0, 1)
if keys[pygame.K_s]: p.move(0, -1)
p.draw()
pygame.display.update()
pygame.quit()
关于python - 在 Pygame 中检测多边形和矩形之间的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64095396/
我正在尝试将外框内的框(坐标)放入。我已经使用交集联合方法完成了工作,并且我希望其他方法也可以这样做。 另外,能否请您告诉我如何比较这两个内盒? 最佳答案 通过比较边界框和内部框的左上角和右下角的坐标
我希望输出看起来像这样: 如何安排这些循环以获得两个三角形数字模式?我该如何改进我的代码。 JAVA 中的新功能:-) for (int i = 1; icount; num--) {
我需要将 map 边界存储在 MySQL 数据库中。我花了一些时间在地理空间扩展的文档上,但是学习所有相关信息(WKT、WKB 等)很困难,而且就我而言没有必要。我只需要一种方法来存储坐标矩形并稍后将
在 gnuplot 中,我可以通过绘制一个矩形 set object rect from x0,y0 to x1,y1 如何从文件中读取坐标 x0,x1,y0,y1? 最佳答案 一种方法是将设置矩形的
我正在尝试创建一个填充了水平线或垂直线的矩形。 矩形的宽度是动态的,所以我不能使用图像刷。 如果有人知道任何解决方案,请告诉我。 最佳答案 我想出了一个直接的方法来做到这一点;最后,我使用以下视觉画笔
这个 SVG 在所有浏览器中看起来都很模糊,在所有缩放级别。 在 Chrome、Safari 和 Firefox 中,它看起来像这样: 如果放大,您可以看到笔画有两个像素的宽度,即使默认笔画
我正在尝试在ggplot2图上添加多个阴影/矩形。在这个可重现的示例中,我只添加了3,但是使用完整数据可能需要总计一百。 这是我的原始数据的子集-在名为temp的数据框中-dput在问题的底部:
我有一个包含驻留在 Viewport3D 中的 3D 对象的应用程序,我希望用户能够通过在屏幕上拖动一个矩形来选择它们。 我尝试在 Viewport3D 上应用 GeometryHitTestPara
如何才能使 WPF 矩形的顶角变成圆角? 我创建了一个边框并设置了 CornerRadius 属性,并在边框内添加了矩形,但它不起作用,矩形不是圆角的。 最佳答案 您遇到的问题是矩形“溢
我正在尝试使用此 question 中的代码旋转 Leaflet 矩形。 rotatePoints (center, points, yaw) { const res = [] const a
我有以下图像。 this image 我想删除数字周围的橙色框/矩形,并保持原始图像干净,没有任何橙色网格/矩形。 以下是我当前的代码,但没有将其删除。 Mat mask = new Mat(); M
我发现矩形有些不好笑: 比方说,给定的是左、上、右和下坐标的值,所有这些坐标都旨在包含在内。 所以,计算宽度是这样的: width = right - left + 1 到目前为止,一切都很合乎逻辑。
所以,我一直在学习 Java,但我还是个新手,所以请耐心等待。我最近的目标是图形化程序,这次是对键盘控制的测试。由于某种原因,该程序不会显示矩形。通常,paint() 会独立运行,但由于某种原因它不会
我正在阅读 website 中的解决方案 3 (2D)并试图将其翻译成java代码。 java是否正确请评论。我使用的是纬度和经度坐标,而不是 x 和 y 坐标(注意:loc.getLongitude
我似乎无法删除矩形上的边框!请参阅下面的代码,我正在使用 PDFannotation 创建链接。这些链接都有效,但每个矩形都有一个边框。 PdfAnnotation annotation; Recta
如何在保持原始位图面积的同时将位图旋转给定的度数。即,我旋转宽度:100,高度:200 的位图,我的最终结果将是一个更大的图像,但旋转部分的面积仍然为 100*200 最佳答案 图形转换函数非常适合这
我创建了矩形用户控件,我在我的应用程序中使用了这个用户控件。在我的应用程序中,我正在处理图像以进行不同的操作,例如从图像中读取条形码等。这里我有两种处理图像的可能性,一种正在处理整个图像,另一个正在处
好的,我该如何开始呢? 我有一个应用程序可以在屏幕上绘制一些形状(实际上是几千个)。它们有两种类型:矩形和直线。矩形有填充,线条有描边 + 描边厚度。 我从两个文件中读取数据,一个是顶部的数据,一个是
简而言之: 我正在致力于使用 AI 和 GUI 创建纸牌游戏。用户的手显示在游戏界面上,我尚未完成界面,但我打算将牌面图像添加到屏幕上的矩形中。我没有找到 5 种几乎相同的方法,而是找到了一篇类似的文
我遇到了麻烦。我正在尝试使用用户输入的数组列表创建条形图。我可以创建一个条,但只会创建一个条。我需要所有数组输入来创建一个条。 import java.awt.Color; import java.a
我是一名优秀的程序员,十分优秀!