我在做這個遊戲和我的朋友一些樂趣,而我試圖讓敵人隨機量產卵,並跟隨玩家,但這裏是發生了什麼: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)
>>>
此代碼顯示了對什麼類(以及一般的OO原則)的使用缺乏理解。 – roippi
好obviasly我有缺乏對類的理解這就是爲什麼即時通訊尋求幫助 –