2013-10-18 52 views
-4

我在做這個遊戲和我的朋友一些樂趣,而我試圖讓敵人隨機量產卵,並跟隨玩家,但這裏是發生了什麼:pygame的功能錯誤

我的主腳本

#set up 
import pygame, sys, random, time, math 
from pygame.locals import * 
pygame.init() 

#variables start---------------------------------- 
niass = "grass_shit.png" #grass image 
mil = "head.png" #player name 
ali = "head_2.png" #alien image 

x, y = 0, 0 #character position 
movex, movey = 0, 0 #how far the character will move 
#x is left and right, y is up and down 

screen = pygame.display.set_mode((850, 640),0,32) #set screen      
background = pygame.image.load(niass).convert() #load image to screen 

#WE NEED TO MAKE THESE IMAGERS RECTS BEFORE WE CAN MOVE ON 
char = pygame.image.load(mil).convert_alpha() #covert player image 
ali = pygame.image.load(ali).convert_alpha() #covert alien image 

stop = random.randint(1,4) 

#variables end------------------------------------ 

#classes------------------------------------------ 
class Enemys(): 
    def enemy(): 
     z, w = random.randint(10, 480), random.randint(10, 500) 
     movez, movew = 0, 0 

     if z < x: 
      movez =+ 0.20 
     elif z > x: 
      movez =- 0.20 
     if w < y: 
      movew =+ 0.20 
     elif w > y: 
      movew =- 0.20 

     w += movew 
     z += movez 

     screen.blit(ali,(z,w)) 

#classes------------------------------------------ 

while True: 

    for event in pygame.event.get(): 
      if event.type==QUIT: 
       pygame.quit() 
       sys.exit() 
    if event.type==KEYDOWN:  
      if event.key==K_a: 
        movex=-1 
      elif event.key==K_d: 
        movex=+1 
      elif event.key==K_w: 
        movey=-1 
      elif event.key==K_s: 
        movey=+1 
    if event.type==KEYUP: 
      if event.key==K_a: 
        movex=0 
      elif event.key==K_d: 
        movex=0 
      elif event.key==K_w: 
        movey=0 
      elif event.key==K_s: 
        movey=0 

    while stop > 0: 
     stop =- 1 
     Enemys.enemy()    

    x += movex 
    y += movey 

    screen.blit(background,(0,0)) 
    screen.blit(char,(x,y)) 
    pygame.display.update() 

因此,停止爲變量,選取一個隨機數,然後在同時,真正的部分還有另外一個while循環,它一直持續到一站是0以下,且在while循環敵方功能運行,但它不喜歡它。

這裏的錯誤

>>> ================================ RESTART ================================ 
>>> 

Traceback (most recent call last): 
    File "/home/claude/Dropbox/BigKahunaBurger/BigKahunaBurger LOOP.py", line 115, in <module> 
    Enemys.enemy() 
TypeError: unbound method enemy() must be called with Enemys instance as first argument (got nothing instead) 
>>> 
+3

此代碼顯示了對什麼類(以及一般的OO原則)的使用缺乏理解。 – roippi

+0

好obviasly我有缺乏對類的理解這就是爲什麼即時通訊尋求幫助 –

回答

1

有帶班和你如何使用它至少有兩個關鍵問題。你應該聲明所謂的班級實例,如badGuy = Enemy()。在class Enemy()定義,你應該有成員函數有一個叫self參數(或任何你想調用它,只要它是一致的),這裏有一個例子:

class Enemy(): 
    def __init__(self): # self is needed for all methods defined in Enemy 
     # set some values that each unique enemy has, like health. 
     self.health = 100 
     self.damage = 10 
    def attack(self, target): 
     target.health -= self.damage # when inside the class, use self 

現在課堂外,你需要創建一些敵人:

bandit = Enemy() 
robber = Enemy() 
bandit.attack(robber) 
print bandit.health, robber.health # outside the class use the variable name 
# bandit is the object's name, enemy is the object's type. 

現在,你看怎麼樣有用類,去學習你可以瞭解他們!

+0

感謝您的幫助,你幫助heeps –

+0

好吧,所以當我創建z和w位置時,我應該使用自己的呢? –

+0

@VincentVega相反,這並不能幫助你,它只是爲了向你展示你可以輕鬆學習的東西,並且有一點點的學習奉獻精神。現在不要期望自己編寫自己的類,現在就去閱讀一本關於Python的免費書,[這裏是一個很好的開始](http://inventwithpython.com/) – Leonardo