滑动的精灵

Sliding sprites(滑动的精灵)
本文介绍了滑动的精灵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的精灵像他在冰上一样滑行。因此,如果他在地面上,他可以正常行走,但当他接触到冰时,他会滑动,直到有东西阻止他。 有人知道怎么才能做到这一点吗? 谢谢

推荐答案

操作摩擦系数,如"Sprite Movement Towards a Target"示例(修改如下):

class Sprite(pygame.sprite.Sprite):
    ICE = 0.01
    LAND = 1.

    def __init__(self):
        # ...
        self.normal_friction = .95 # friction while accelerating
        self.slowing_friction = .8 # friction while slowing down

    def update(self):
        # ...
        if self.dir: # if there is a direction to move

            if self.in_ice_region():
                surface_coefficient = Sprite.ICE
            else:
                surface_coefficient = Sprite.LAND

            if self.distance_check(self.dist): # if we need to slow down
                self.speedX += (self.dir[0] * (self.speed / 2)) # reduced speed
                self.speedY += (self.dir[1] * (self.speed / 2))
                self.speedX *= surface_coefficient * self.slowing_friction # increased friction
                self.speedY *= surface_coefficient * self.slowing_friction

            else: # if we need to go normal speed
                self.speedX += (self.dir[0] * self.speed) # calculate speed from direction to move and speed constant
                self.speedY += (self.dir[1] * self.speed)
                self.speedX *= surface_coefficient * self.normal_friction # apply friction
                self.speedY *= surface_coefficient * self.normal_friction

            self.trueX += self.speedX # store true x decimal values
            self.trueY += self.speedY
            self.rect.center = (round(self.trueX),round(self.trueY)) # apply values to sprite.center

这篇关于滑动的精灵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)