can';我不能让这个python程序工作
这是can';我不能让这个python程序工作,python,pygame,Python,Pygame,这是vector2模块: class Vector2(object): "this calculates the vector between two points" def __init__(self , x = 0.0, y = 0.0): self.x = x self.y = y def __str__(self): return "( %s,%s )"%(self.x, self.y) @classme
vector2
模块:
class Vector2(object):
"this calculates the vector between two points"
def __init__(self , x = 0.0, y = 0.0):
self.x = x
self.y = y
def __str__(self):
return "( %s,%s )"%(self.x, self.y)
@classmethod
def from_points(cls,p1,p2):
return cls(p2[0]-p1[0],p2[1]-p1[1])
#the magnetude of a vector is the distance between two points
def get_magnetude(self):
return math.sqrt(self.x**2+self.y**2)
def normalize(self):
magnetude = self.get_magnetude()
self.x /= magnetude
self.y /= magnetude
#rhs stands for right hand side
def __add__(self,rhs):
return Vector2(self.x + rhs.x,self.y+rhs.y)
def __sub__(self,rhs):
return Vector2(self.x-rhs.x,self.y-rhs.y)
def __neg__(self):
return Vector2(-self.x, -self.y)
def __mul__(self,scalar):
return Vector2(self.x*scalar,self.y*scalar)
def __div__(self,scalar):
return Vector2(self.x /scalar, self.y/scalar)
这是主程序,它导入vector2
:
background_image_file = 'download.jpeg'
sprite_image_file = 'images.jpeg'
import math
import pygame
from pygame.locals import*
from sys import exit
import vector2
pygame.init()
screen = pygame.display.set_mode((640,480), 0 ,32)
background = pygame.image.load(background_image_file).convert()
sprite = pygame.image.load(sprite_image_file).convert_alpha()
clock = pygame.time.Clock()
position = Vector2(100.0, 100.0)#the starting point coordonates
heading = Vector2()#a vector without its magnetude(unit vector)
speed = 250.0
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
exit()
if event.type == MOUSEBUTTONDOWN:
destination_x = event.pos[0]-sprite.get_width()/2
destination_y =event.pos[1]-sprite.get_height()/2
destination = (destination_x, destination_y)
heading = Vector2.get_magnetude(position,destination)#
heading.normalize()#a vector without magnetude,just direction
screen.blit(background, (0,0))
screen.blit(sprite, position)
time_passed = clock.tick()
time_passed_seconds = time_passed / 1000.0
distance_moved = time_passed_seconds * speed
position += (heading * distance_moved)
pygame.display.update()
我正在自学Python和pygame(从Python和pygame开始游戏开发-从新手到专业人员(2007)),但我无法让程序正常工作。还有人能给我解释一下作者为什么使用position=Vector2(100.0100.0)
和position=Vector2()
来创建新的向量吗
它一直在说:
traceback (most recent call last):
File "/home/moussa/Documents/python /vector_movement.py", line 21, in <module>
position = Vector2(100.0, 100.0)#the starting point coordonates
NameError: name 'Vector2' is not defined
回溯(最近一次呼叫最后一次):
文件“/home/moussa/Documents/python/vector_movement.py”,第21行,在
位置=矢量2(100.0100.0)#起点坐标
NameError:未定义名称“Vector2”
我猜问题中的代码实际上分为两个文件,一个是Vector2
类(在Vector2.py
中),另一个是其他文件(导入Vector2
)
您遇到的问题是没有正确访问类。在主模块中,需要使用vector2.vector2
访问其模块中的类
或者,如果您希望更方便地访问该类,可以将导入从import vector2
更改为from vector2 import vector2
。这会将类放入主模块的名称空间中,因此您可以作为Vector2
直接访问它
至于使用
position=Vector2(100.0100.0)
,这是对Vector2
类构造函数的调用。它创建类的一个实例,使用x
的值100
和y
的值100
对其进行初始化,然后将其绑定到变量位置
。然后可以使用类定义的各种方法和运算符来更新实例或获取新值。例如,后面的行position+=(heading*distance\u moved)
首先将向量heading
乘以标量distance\u moved
,然后将向量结果添加到位置。您可以使用列表或元组中的值来实现这一点,但这要复杂得多(您需要自己添加和乘以向量的分量)。@adchilds这是不正确的。假设类Vector2
在文件Vector2
中,需要的是通过Vector2.Vector2
引用它,或者使用from Vector2 import Vector2
。问题中显示的代码是全部在一个文件中,还是在“主程序从这里开始”注释所在的位置分为两部分?如果所有文件都在一个文件中,那么您得到的异常就没有任何意义,但是如果顶部部分在vector2.py
中,那么异常就有意义。它实际上是两个独立的代码,但在同一个文件夹中。所以我只是导入了vector2以便使用它。我的意思是,我不理解使用vector2创建新向量的意义,因为它是一个类,因此,从一个类中创建新的向量对我来说没有意义。如果说位置=[100.0100.0],这不是相同的吗?我希望现在它更清楚了。thanks@user2983686当前位置我更新了我的答案,试图解释一下那里发生了什么。如果您仍然不理解它,您可能想查看Python面向对象编程的教程,例如。