我和我的朋友一起做这个游戏是为了找点乐子,我试图让随机数量的敌人产生并跟随玩家,但会发生以下情况:
我的主要脚本:
#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)
>>>