-4

我和我的朋友一起做这个游戏是为了找点乐子,我试图让随机数量的敌人产生并跟随玩家,但会发生以下情况:

我的主要脚本

#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)
>>> 
4

1 回答 1

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.

既然您看到了课程的有用性,那就去学习所有关于它们的知识吧!

于 2013-10-18T02:11:55.940 回答