栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

简单的拖动物理,左右移动时的动作有所不同

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

简单的拖动物理,左右移动时的动作有所不同

造成此问题的原因是,

pygame.Rect
存储整数坐标:

Rect对象的坐标都是整数。[…]

的部分组件

dx
,并
dy
当你丢失:

self.Rect.x += dxself.Rect.y += dy

您必须以浮点精度进行计算。在类中添加一个

x
and
y
属性。递增属性
move
并同步
Rect
属性:

class Player:    def __init__(self, color):        self.Rect = pygame.Rect([50, 50], [30, 50])        self.x = self.Rect.x        self.y = slef.Rect.y        # [...]    def move(self, dx, dy, platforms):        # Test for collisions with platforms        # handle movement on the X axis        self.x += dx        self.Rect.x = round(self.x)        for platform in platforms: if self.Rect.colliderect(platform.Rect):     if dx > 0:         self.Rect.right = platform.Rect.left     if dx < 0:         self.Rect.left = platform.Rect.right     self.x = self.Rect.x     # Reset velocity when collision with wall     self.vx = 0        # handle movement on the Y axis        self.Rect.y += dy        self.Rect.y = round(self.y)        for platform in platforms: if self.Rect.colliderect(platform.Rect):     if dy > 0:         self.Rect.bottom = platform.Rect.top     if dy < 0:         self.Rect.top = platform.Rect.bottom     self.y = self.Rect.y     # Reset velocity when collision with floor or roof     self.vy = 0        # return correctly collided rect to draw()        return self.Rect


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/648111.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号