2014-04-04 31 views
0

這是vector2模塊:不能得到這個Python程序工作

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.0,100.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 
+0

@adchilds這是不正確的。假設類Vector2處於文件vector2中,需要使用vector2.Vector2來引用它,或者使用from vector2 import Vector2來引用它。 –

+0

問題中所顯示的代碼是在一個文件中顯示的,還是在「#主程序從這裏開始」的評論中是兩分爲二?如果它是全部在一個文件中,你得到的異常沒有任何意義,但如果頂部部分在'vector2.py'中,則會發生異常。 – Blckknght

+0

它實際上是兩個獨立的代碼,但在同一個文件夾內。因此,我只是導入了Vector2以便使用它。 – user2983686

回答

3

我猜測,在問題的代碼實際上是兩個文件,一個與Vector2類(在vector2.py)和其他一些文件中的其餘部分之間的分裂(其中進口vector2)。

您遇到的問題是您沒有正確訪問該類。在您的主模塊中,您需要使用vector2.Vector2訪問其模塊中的類。

或者,如果您希望更方便地訪問課程,則可以改爲將您的導入從import vector2更改爲from vector2 import Vector2。這將該類放入主模塊的名稱空間,因此您可以直接以Vector2的身份訪問它。

至於使用position = Vector2(100.0,100.0),這是對Vector2類的構造函數的調用。它創建該類的一個實例,使用值100x100y進行初始化,然後將其綁定到變量position。然後可以使用類定義的各種方法和運算符來更新實例或獲取新值。例如,後面的行position += (heading * distance_moved)首先將矢量heading乘以標量distance_moved,然後將矢量結果添加到position。你可以用列表或元組中的值來做到這一點,但它會複雜得多(你需要自己添加和增加矢量的組件)。

+0

我的意思是我沒有得到使用Vector2創建一個新的向量的含義,因爲它的一個類,所以它不會讓我感覺從類創建新的向量。如果說position = [100.0,100.0 ]?我希望它現在更清晰。感謝 – user2983686

+0

@ user2983686:我已經更新了我的答案,試圖解釋一下那裏發生的一些事情。如果你仍然不理解它,你可能想看看關於Python面向對象編程的教程,比如[Python文檔的這一頁](https://docs.python.org/2/tutorial/classes。 HTML)。 – Blckknght

+0

謝謝你,我現在完全明白了。 – user2983686