私は友人と一緒にこのゲームを作成しており、ランダムな量の敵を生成してプレイヤーを追跡しようとしていますが、何が起こるかは次のとおりです。
私の主なスクリプト:
#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()
したがって、stop は変数で、乱数を選択し、while True の部分に別の while ループがあり、stop が 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)
>>>