Python自行更改Float中的Integer

发布于 2024-09-19 22:07:51 字数 1236 浏览 3 评论 0原文

我正在尝试通过编写贪吃蛇游戏来学习Python(我有2.5.4),但我陷入了困境。 一些整数变成浮点数并不断随机变化,至少从我的角度来看是这样:)

问题是 Snake.spawnPointCam.move() 改变

print Snake.spawnPoint # first time, this returns '[25, 20]' ,which is good.
Cam.Move()
print Snake.spawnPoint # after the method, the value is '[26.0, 21.0]', and that's bad

之后,它们只是随波逐流。

这就是方法:(它与 spawnPoint 无关。

def Move(self):
    self.vel[0]+=(self.target[0]-self.cen[0])*self.k
    self.vel[1]+=(self.target[1]-self.cen[1])*self.k
    if    self.vel[0] > self.friction: self.vel[0]-= self.friction
    elif  self.vel[0] < self.friction: self.vel[0]+= self.friction
    else: self.vel[0] = 0
    if    self.vel[1] > self.friction: self.vel[1]-= self.friction
    elif  self.vel[1] < self.friction: self.vel[1]+= self.friction
    else: self.vel[1]=0
    self.cen[0]+=self.vel[0]
    self.cen[1]+=self.vel[1]

spawnPoint 是一个常量,我在蛇生成时将其附加到蛇的身体上。我喜欢它是一个列表,因为它有由列表组成的蛇的身体,并且渲染方法使用 index() 来执行操作,

代码似乎不适合这里,所以我将其压缩。 有人可以看一下吗?谢谢 http://www.mediafire.com/?zdcx5s93q9hxz4o

I'm trying to learn Python, (i have 2.5.4) by writing a snake game, but I'm stuck.
Some integers change into floats and keep changing randomly, at least from my perspective :)

The problem is that Snake.spawnPoint gets changed by Cam.move()

print Snake.spawnPoint # first time, this returns '[25, 20]' ,which is good.
Cam.Move()
print Snake.spawnPoint # after the method, the value is '[26.0, 21.0]', and that's bad

After that, they just drift around.

And this is the method: (it has nothing to do with the spawnPoint

def Move(self):
    self.vel[0]+=(self.target[0]-self.cen[0])*self.k
    self.vel[1]+=(self.target[1]-self.cen[1])*self.k
    if    self.vel[0] > self.friction: self.vel[0]-= self.friction
    elif  self.vel[0] < self.friction: self.vel[0]+= self.friction
    else: self.vel[0] = 0
    if    self.vel[1] > self.friction: self.vel[1]-= self.friction
    elif  self.vel[1] < self.friction: self.vel[1]+= self.friction
    else: self.vel[1]=0
    self.cen[0]+=self.vel[0]
    self.cen[1]+=self.vel[1]

The spawnPoint is a constant that I append to the snake's body when he spawns. I like it to be a list because it have the snake's body made of lists, and the rendering method uses index() to do stuff.

The code doesn't seem to fit here, so i zipped it.
Can someone take a look at it? Thanks
http://www.mediafire.com/?zdcx5s93q9hxz4o

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别低头,皇冠会掉 2024-09-26 22:07:55

在Python 中(与许多其他语言一样),任何涉及整数和浮点数的简单算术运算(例如+、-、/、*)都会得出浮点数的结果。整数永远不会变成浮点数,它是作为浮点数返回的运算结果。

请注意,仅涉及整数的运算结果以整数形式返回(我在 Python 中使用整数一词来指代 intlong)。

In Python (as in many other languages) any simple arithmetic operation (e.g. +,-,/,*) involving an integer and a float evaluates to an answer that is a float. Integers never change into floats, it is the result of the operation that is returned as a float.

Note that the result of operations involving only integers is returned as an integer (I'm using the word integer to refer to both int and long in Python).

橘虞初梦 2024-09-26 22:07:53

如果将整数组合在一个表达式中,即乘法、加法、减法(不一定是除法),它们就会变成浮点数。最有可能的是,您的某些变量是浮点数,例如 self.friction。

浮点数本身不会变回整数,只能通过 int() 变回整数。如果你观察到其他东西,你观察到的就是错误的。

看来您的 Move 方法间接修改了“spanwPoint”。我不知道这是否是预期的行为,但这可能意味着您有两个引用指向同一个列表。例如

self.spawnPoint = [1, 0]
self.vel = self.spawnPoint # Does not make a copy!
self.vel[0] += 0.1
self.vel[1] += 0.2

将导致 self.spawnPoint (也)为 [1.1, 0.2]

Integers change to floats if you combine them in an expression, i.e. multiplication, addition, subtraction (not necessarily division). Most likely, some of your variables are floats, e.g. self.friction.

floats don't change back to integers by themselves, only through int(). If you observe anything else, you observe it wrong.

It appears your Move method modifies "spanwPoint" indirectly. I don't know if this is expected behavhiour, but it probably means you have two references pointing to the same list. E.g.

self.spawnPoint = [1, 0]
self.vel = self.spawnPoint # Does not make a copy!
self.vel[0] += 0.1
self.vel[1] += 0.2

will result in self.spawnPoint (also) being [1.1, 0.2]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文