- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前正在构建一个自上而下的赛车,但我正在尝试对静态方形物体进行准确的碰撞检测。
最好的方法是什么?
这是我的部分代码:
class VehicleSprite(Entity):
MAX_FORWARD_SPEED = 18
MAX_REVERSE_SPEED = 1
def __init__(self, images, position):
Entity.__init__(self)
self.src_images = images
self.images = images
self.rect = self.images.get_rect(center=position)
self.position = pygame.math.Vector2(position)
self.velocity = pygame.math.Vector2(0, 0)
self.speed = self.direction = 0
self.k_left = self.k_right = self.k_down = self.k_up = 0
self.width = 28
self.height = 64
self.numImages = 8
self.cImage = 0
def update(self, time):
self.speed += self.k_up + self.k_down
# To clamp the speed.
self.speed = max(-self.MAX_REVERSE_SPEED,
min(self.speed, self.MAX_FORWARD_SPEED))
# Degrees sprite is facing (direction)
self.direction += (self.k_right + self.k_left)
rad = math.radians(self.direction)
self.velocity.x = -self.speed*math.sin(rad)
self.velocity.y = -self.speed*math.cos(rad)
self.position += self.velocity
if (self.cImage >= self.numImages - 1):
self.cImage = 0
else:
self.cImage += 1
self.images = pygame.transform.rotate(self.src_images, self.direction)
self.rect = self.images.get_rect(center=self.position)
def render(self, screen, camera):
screen.blit(self.images, (self.rect.topleft+camera), (self.cImage*self.width, 0, self.width, self.height))
这是碰撞检测位:
for sprite in all_sprites:
for crate in crates:
crate.update(time)
for crate in crates:
screen.blit(power_img, crate.position+camera)
# Collision with crate
if ((purple_bike.position.x >= crate.x) and purple_bike.position.x < (crate.x + crate.width) or (purple_bike.position.x + purple_bike.width) > crate.x and purple_bike.position.x + purple_bike.width < crate.x + crate.width) and ((purple_bike.position.y > crate.y) and purple_bike.position.y < (crate.y + crate.height) or (purple_bike.position.y + purple_bike.height) > crate.y and (purple_bike.position.y + purple_bike.height) < (crate.y + crate.height)):
print("Hit")
purple_bike.speed = 0
最佳答案
这是一个示例,向您展示如何将 Pymunk 与 pygame 结合使用来获得旋转的碰撞框/碰撞矩形。绿色轮廓是 Pymunk 形状的边缘,蓝色矩形是 pygame 矩形,其唯一目的是存储 Sprite 的 blit 位置。熟悉 Pymunk 需要一些时间,但它是在 pygame 中实现旋转、任意形状的碰撞盒的最佳方式之一。
import sys
import math
import pygame as pg
import pymunk as pm
from pymunk import Vec2d
def flipy(p):
"""Convert chipmunk coordinates to pygame coordinates."""
return Vec2d(p[0], -p[1]+600)
class Player(pg.sprite.Sprite):
def __init__(self, pos, space, mass=0.3):
super().__init__()
self.image = pg.Surface((52, 72), pg.SRCALPHA)
pg.draw.polygon(self.image, pg.Color('steelblue2'),
[(1, 72), (26, 1), (51, 72)])
self.rect = self.image.get_rect(center=pos)
self.orig_image = self.image
# The verts for the Pymunk shape in relation
# to the sprite's center.
vertices = [(0, 36), (26, -36), (-26, -36)]
# Create the physics body and shape of this object.
moment = pm.moment_for_poly(mass, vertices)
self.body = pm.Body(mass, moment)
self.shape = pm.Poly(self.body, vertices, radius=3)
self.shape.friction = .8
self.shape.elasticity = .2
self.body.position = pos
# Add them to the Pymunk space.
self.space = space
self.space.add(self.body, self.shape)
self.accel_forw = False
self.accel_back = False
self.turn_left = False
self.turn_right = False
self.topspeed = 1790
self.angle = 0
def handle_event(self, event):
if event.type == pg.KEYDOWN:
if event.key == pg.K_w:
self.accel_forw = True
if event.key == pg.K_a:
self.turn_left = True
if event.key == pg.K_d:
self.turn_right = True
if event.key == pg.K_s:
self.accel_back = True
if event.type == pg.KEYUP:
if event.key == pg.K_w:
self.accel_forw = False
if event.key == pg.K_a:
self.turn_left = False
if event.key == pg.K_d:
self.turn_right = False
if event.key == pg.K_s:
self.accel_back = False
def update(self, dt):
# Accelerate the pymunk body of this sprite.
if self.accel_forw and self.body.velocity.length < self.topspeed:
self.body.apply_force_at_local_point(Vec2d(0, 624), Vec2d(0, 0))
if self.accel_back and self.body.velocity.length < self.topspeed:
self.body.apply_force_at_local_point(Vec2d(0, -514), Vec2d(0, 0))
if self.turn_left and self.body.velocity.length < self.topspeed:
self.body.angle += .1
self.body.angular_velocity = 0
if self.turn_right and self.body.velocity.length < self.topspeed:
self.body.angle -= .1
self.body.angular_velocity = 0
# Rotate the image of the sprite.
self.angle = self.body.angle
self.rect.center = flipy(self.body.position)
self.image = pg.transform.rotozoom(
self.orig_image, math.degrees(self.body.angle), 1)
self.rect = self.image.get_rect(center=self.rect.center)
class Wall(pg.sprite.Sprite):
def __init__(self, pos, verts, space, mass, *sprite_groups):
super().__init__(*sprite_groups)
# Determine the width and height of the surface.
width = max(v[0] for v in verts)
height = max(v[1] for v in verts)
self.image = pg.Surface((width, height), pg.SRCALPHA)
pg.draw.polygon(self.image, pg.Color('sienna1'), verts)
self.rect = self.image.get_rect(topleft=pos)
moment = pm.moment_for_poly(mass, verts)
self.body = pm.Body(mass, moment, pm.Body.STATIC)
# Need to transform the vertices for the pymunk poly shape,
# so that they fit to the image vertices.
verts2 = [(x, -y) for x, y in verts]
self.shape = pm.Poly(self.body, verts2, radius=2)
self.shape.friction = 1.0
self.shape.elasticity = .52
self.body.position = flipy(pos)
self.space = space
self.space.add(self.shape)
class Game:
def __init__(self):
self.done = False
self.screen = pg.display.set_mode((800, 600))
self.clock = pg.time.Clock()
self.bg_color = pg.Color(60, 60, 60)
self.space = pm.Space()
self.space.gravity = Vec2d(0.0, 0.0)
self.space.damping = .4
self.all_sprites = pg.sprite.Group()
self.player = Player((100, 300), self.space)
self.all_sprites.add(self.player)
# Position-vertices tuples for the walls.
vertices = [
([80, 120], ((0, 0), (100, 0), (70, 100), (0, 100))),
([400, 250], ((20, 40), (100, 0), (80, 80), (10, 100))),
([200, 450], ((20, 40), (300, 0), (300, 120), (10, 100))),
([760, 10], ((0, 0), (30, 0), (30, 420), (0, 400))),
]
for pos, verts in vertices:
Wall(pos, verts, self.space, 1, self.all_sprites)
def run(self):
while not self.done:
self.dt = self.clock.tick(30) / 1000
self.handle_events()
self.run_logic()
self.draw()
def handle_events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
self.player.handle_event(event)
def run_logic(self):
self.space.step(1/60)
self.all_sprites.update(self.dt)
def draw(self):
self.screen.fill(self.bg_color)
self.all_sprites.draw(self.screen)
# Debug draw - Pymunk shapes are green, pygame rects are blue.
for obj in self.all_sprites:
shape = obj.shape
ps = [flipy(pos.rotated(shape.body.angle) + shape.body.position)
for pos in shape.get_vertices()]
ps.append(ps[0])
pg.draw.rect(self.screen, pg.Color('blue'), obj.rect, 2)
pg.draw.lines(self.screen, (90, 200, 50), False, ps, 2)
pg.display.flip()
if __name__ == '__main__':
pg.init()
Game().run()
pg.quit()
sys.exit()
关于python - pygame中的旋转命中框矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45516072/
关于 APC 操作码缓存,什么是“命中与未命中”?我已经安装了 APC 并且它运行良好,但我有“一些”失误,我想知道这是否“不好”。此外,我正在运行 Openx,因此,我很快就会填满“缓存完整计数”。
我试过这个: def test_send_confirm_hit(monkeypatch): hit = False def called(): global hit
是否可以将找到的单词插入到替换中? $(function() { content = 'hallo mein name ist peter und ich komme aus berlin.
我有一个允许用户将文件上传到文件夹的网站。首先,我检查文件是否存在,如果存在,然后检查复选框的值以确定用户是否要覆盖现有文件。如果点击上传并且未选中该框,我会执行一个带有消息和后退按钮的 die()
我有多个不同的进程通过 IPC 进行通信,当使用 gdb 调试单个进程时,每当遇到断点时,我都会尝试向其他进程发送消息。有没有一种方法可以自动在遇到断点时自动调用一个函数/一段代码(NotifyAll
目前,通过管道传输到 jq 的 cat 命令帮助我解析工作目录中的多个 JSON 文件,并根据正则表达式模式匹配文件中所有可用的电子邮件 ID。但是,我很想识别正则表达式模式被命中/匹配的文件名 ca
我们希望将 podname 解析为 IP,以在 akka 集群中配置种子节点。这是通过在 Kubernetes 中使用 headless (headless)服务和有状态集的概念来实现的。但是,如何在
Maven 项目具有以下文件夹结构: src/main/java src/main/resources src/test/java src/test/resources 如果我们导航到 Maven 项
我只使用 c 几个星期,所以很可能会出现我忽略的明显错误。我看过其他线程,但我不明白我正在读的很多内容。该程序假设有一个无限大的牌组。 已知问题: clearBuffer 当前未使用,我正在尝试不同的
我已将我的 AdMob 代码实现到我的 XML 文件中,如下所示: 在我的 Activity 的 onCreate 方法中: // load ads
我的作业是通过示例程序确定给定跟踪文件的缓存读/写/未命中/命中次数。 举例来说,这是示例跟踪输出的前 10 行。 0x37c852: W 0xbfd4b18c 0x37cfe0: W 0xbfd
https://plnkr.co/edit/2h6fV5yTjeUqLP3SvbvO?p=preview 预期 登录后应用程序重定向到 $state container,其中包含 dashboard
ElasticSearch 独立于 from 和 size 参数,基于查询的所有命中构建聚合结果。在大多数情况下,这是我们想要的,但我有一个特殊情况,我需要将聚合限制为前 N 个命中。 limits
我使用 Intel PCM 进行细粒度的 CPU 测量。在我的代码中,我试图测量缓存效率。 基本上,我首先将一个小数组放入 L1 缓存(通过多次遍历),然后启动计时器,再遍历数组一次(希望使用缓存),
我在为 javascript 滑动元素定义点击区域时遇到问题。 参见示例: http://www.warface.co.uk/clients/warface.co.uk/ 请滑过右侧的灰色框以显示按钮
我正在尝试在 foldersystem 中使用 os.walk() 找到几个 'my_file.bat',如果文件名匹配它应该用 subprocess.call() 或 .run() 调用。问题是 o
我有一个端点,我可以在其中请求我使用 Siesta 查询的多条数据(例如 https://example.com/things?ids=1,2,3) .如果我只缓存了一些 things ,我试图弄清楚
这是我的代码: public static function test(){ try{ $apiContext = ApiContext::create(
我使用 PHP 在需要时传递登录表单,代码如下: $htmlForm = ''.''; switch(LOGIN_METHOD) { case 'both': $htmlFor
我正在使用 nginx-lua带有 redis 的模块提供 ember-app 的静态文件. index文件内容存储在redis作为 value由 nginx 正确提供服务当(根)domain/IP被
我是一名优秀的程序员,十分优秀!