鱼C论坛

 找回密码
 立即注册
查看: 3841|回复: 1

关于Python的gameobject.vector2我有些问题想请教各位。

[复制链接]
发表于 2017-9-14 22:20:12 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Seternal 于 2017-9-14 22:22 编辑

问题是这样的。总的来说我在学习pygame这个第三方库,目前用的Python是3.6版本,电脑系统为win10。这是背景。然后说一下问题发现的过程。首先,我看的教程是这个用Python和pygame写游戏http://eyehere.net/2011/python-pygame-novice-professional-16/。这位大佬用的版本和我的有些不同,所以在这个过程中有些语法和第三方库的不兼容,有的我迷迷糊糊地改没想到还成功了,但是有的只是伪成功,问题实际上没有得到解决。然后,我切入正题==。我遇到的就是这个我以为我改成功了,实际上是自欺欺人的情况。当前我想先确认一下问题是否出在以下代码上:
  1. import math
  2. class Vector2(tuple):

  3.     def __new__(typ, x=1.0, y=1.0):
  4.         n = tuple.__new__(typ, (int(x), int(y)))
  5.         n.x = x
  6.         n.y = y
  7.         return n

  8.     def __mul__(self, other):
  9.         return self.__new__(type(self), self.x*other, self.y*other)

  10.     def __divmod__(self, other):
  11.         return self.__new__(type(self), self.x/other, self.y/other)

  12.     def __add__(self, other):
  13.         return self.__new__(type(self), self.x+other.x, self.y+other.y)

  14.     def __sub__(self, other):
  15.         return self.__new__(type(self), self.x-other.x, self.y-other.y)

  16.     def __str__(self):
  17.         return "(%s, %s)"%(self.x, self.y)
  18.     @staticmethod
  19.     def from_points(P1, P2):
  20.         return Vector2( P2[0] - P1[0], P2[1] - P1[1] )
  21.     def get_magnitude(self):
  22.         return math.sqrt( self.x**2 + self.y**2 )
  23.     def normalize(self):
  24.         magnitude = self.get_magnitude()
  25.         if magnitude==0:
  26.             return None
  27.         else:
  28.             self.x /= magnitude
  29.             self.y /= magnitude
复制代码

这个是我在gameobject的vector2中找的代码。我觉得当我的程序运行时,会出现的问题是由normalize函数引发的。因为几乎每一次调用这个函数时都会出现magnitude==0的情况。在今晚之前,为了它不报错,我将原代码改为:
  1.     def normalize(self):
  2.         magnitude = self.get_magnitude()
  3.         if magnitude==0:
  4.             print("magnitude=0!")
  5.         else:
  6.             self.x /= magnitude
  7.             self.y /= magnitude
复制代码

事实上问题依然存在,但是它这样能够运行。直到我遇到了这段代码:
  1. class GameEntity(object):
  2.     def __init__(self,world,name,image):
  3.         self.world=world
  4.         self.name=name
  5.         self.image=image
  6.         self.location=Vector2(0,0)
  7.         self.destination=Vector2(0,0)
  8.         self.speed=0.
  9.         self.brain=StateMachine()
  10.         self.id=0
  11.     def render(self,surface):
  12.         x,y=self.location
  13.         w,h=self.image.get_size()
  14.         surface.blit(self.image,(x-w/2,y-h/2))
  15.     def process(self,time_passed):
  16.         self.brain.think()
  17.         if self.speed>0 and self.location!=self.destination:
  18.             vec_to_destination=self.destination-self.location
  19.             distance_to_destination=vec_to_destination.get_magnitude()
  20.            [b] heading[/b]=vec_to_destination.[b]normalize()[/b]
  21.             travel_distance=min(distance_to_destination,time_passed*self.speed)
  22.             self.location+=travel_distance*[b]heading[/b]
复制代码

这里是一个游戏实体类的代码,加粗的地方就是与normalize函数相关的变量。运行后它表示heading是一个'NoneType':
File "D:/pycharm/filetem/game14.0.py", line 31, in process
    self.location+=travel_distance*heading
TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'
也就是说当调用normalize时返回的是None,也就是说magnitude=0!
到这里我想先确认一件事:就是我的vector2代码是否有误,比如和我的版本不兼容,或者根本不能这么写之类的。由于教程给出的代码对我来说比较大,我只能一个个排除错误,感觉一次性贴一大段代码不太合适,见谅。
然后如果有大佬知道错在何处,请告诉我如何治本。。。毕竟治标的办法我只能骗骗自己TAT。。问题来了躲不掉啊。。
最后,关于这个问题有需要补充的地方敬请提问,我能说明白的定然悉数告知!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-9-14 22:23:04 | 显示全部楼层
好奇怪。。链接怎么放不上==。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-23 21:46

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表