类名不可调用两次

时间:2017-06-09 13:41:22

标签: python-3.x class pygame

我正试图为游戏产生多个敌人。出于某种原因,当我尝试两次调用该类时,它会给我一个错误:Type error: spawn object is not callable。我不知道该怎么做。有人可以帮帮我吗? 这是类的代码,它被称为:

class spawn(object):
    def __init__(self,primaryx,primaryy):
        x1=random.randint(100,400)
        y1=random.randint(100,400)
        self.x1=x1
        self.y1=y1
        self.primaryx=primaryx
        self.primaryy=primaryy
    def AIPrototype(self):#The important parts to this error star here
        global x,y
        pygame.draw.rect(screen,THECOLORS['blue'],(self.primaryx,self.primaryy,50,50))
        self.x1=self.primaryx
        self.y1=self.primaryy
        if self.x1<x:
            xspeed1=1
            slopex1=x-self.x1
        if self.x1>x:
            xspeed1=-1
            slopex1=self.x1-x
        if self.y1<y:
            yspeed1=1
            slopey1=y-self.y1
        if self.y1>y:
            yspeed1=-1
            slopey1=self.y1-y       
    #
        hypo1=((slopex1**2)+(slopey1**2))**0.5
        speedmark1=hypo1/3
        speedy1=slopey1/speedmark1
        speedx1=slopex1/speedmark1
        movex1=speedx1
        movey1=speedy1
        if self.x1<=640 and self.x1>=0:
            if self.x1>x:
                self.x1+=xspeed1*movex1
                if self.x1<x:
                    xspeed1=0
        if self.y1<=480 and self.x1>=0:
            if self.y1>y:
                self.y1+=yspeed1*movey1
                if self.y1<y:
                    yspeed1=0
        if self.x1<=640 and self.x1>=0:
            if self.x1<x:
                self.x1+=xspeed1*movex1
                if self.x1>x:
                    xspeed1=0
        if self.y1<=480 and self.x1>=0:
            if self.y1<y:
                self.y1+=yspeed1*movey1
                if self.y1>y:
                    yspeed1=0
    #
        if self.x1>640:
            self.x1=640
        if self.x1<0:
            self.x1=0
        if self.y1>480:
            self.y1=480
        if self.y1<0:
            self.y1=0
        self.primaryx=self.x1
        self.primaryy=self.y1

在这里调用类:

spawn=spawn(600,200)
spawn2=spawn(100,200)
clock = pygame.time.Clock() 
keepGoing = True        

try:
    while keepGoing:
        clock.tick(60) 
        screen.fill(THECOLORS['red'])
        spawn.AIPrototype()
        spawn2.AIPrototype()

如果你想运行代码,这就是完整的事情:

#Auntic Ahamad
#06/06/2017
#Final Project Version 1
#Ms. Strelkovska

import random

""" 
 yaA basic template for starting to program in Pygame
"""

#Import & initialize the pygame module
import pygame

#pygame.locals contains constants like MOUSEMOTION and MOUSEBUTTONUP and QUIT for events. #It's easier to type MOUSEBUTTONUP instead of pygame.locals.MOUSEBUTTONUP
from pygame.locals import *  
# better use pygame.MOUSEMOTION


#This will allow us to name the colours to use rather than give a name  eg (255,0,0)
from pygame.color import THECOLORS
#c=(255,0,0) instead of THECOLORS['red']????

# initial library itself
pygame.init()  
def move():
    global x,y,yspeed,xspeed,rise,run,movex,movey
    spot=pygame.mouse.get_pos()
    spoot=str(spot)
    spoot1=myfont.render((spoot),True,(THECOLORS['blue']))
    screen.blit(spoot1,(spot))
    spoot=spoot.replace('(','')
    spoot=spoot.replace(',','')
    spoot=spoot.replace(')','')
    space=spoot.index(' ')
    rise=spoot[space+1:]
    run=spoot[:space]
    xspeed=0
    yspeed=0
#Just like python, we will use os and time????
import os, time
myfont = pygame.font.SysFont("comicsansms", 20)
#this code is necessary for python to work on tdsb computers????
import platform
if platform.system() == "Windows":
    os.environ['SDL_VIDEODRIVER'] = 'windib'

#Set-up the main screen display window and caption  in the 
size = (640,480)  
screen = pygame.display.set_mode(size) 

#Puts a caption in the bar at the top of the window
pygame.display.set_caption("Yeah, Hello There!") 

# Fills the memory screen surface with colour
screen.fill(THECOLORS['red'])  

#Update and refresh the display to end this frame
pygame.display.flip() #<-- refresh the display
shoot='no'
movex=1
movey=1
click=1
inputx=1
inputy=1
posx=[]
posy=[]
slopex=1
slopey=1
posx1=0
posy1=0
bullet=-1
x=320
y=240
xspeed=0
yspeed=0
soldierup=pygame.image.load("player_direction1.png").convert_alpha()
soldierright=pygame.image.load("player_direction2.png").convert_alpha()
soldierleft=pygame.image.load("player_direction3.png").convert_alpha()
soldierdown=pygame.image.load("player_direction4.png").convert_alpha()
direction=soldierup
def find(x2,y2):
    global x,y,yspeed,xspeed,rise,run,movex,movey,slopex,slopey,direction
    if x2<int(run):
        xspeed=1
        slopex=int(run)-x2
        if abs(slopex)>abs(slopey):
            direction=soldierright
    if x2>int(run):
        xspeed=-1
        slopex=x2-int(run)
        if abs(slopex)>abs(slopey):
            direction=soldierleft
    if y2<int(rise):
        yspeed=1
        slopey=int(rise)-y2
        if abs(slopey)>abs(slopex):
            direction=soldierdown
    if y2>int(rise):
        yspeed=-1
        slopey=y2-int(rise)
        if abs(slopey)>abs(slopex):
            direction=soldierup
    hypo=((slopex**2)+(slopey**2))**0.5
    speedmark=hypo/5
    speedy=slopey/speedmark
    speedx=slopex/speedmark
    movex=speedx
    movey=speedy
    print(abs(int(run)),abs(int(rise)))
def char():
    global x,y,xspeed,yspeed,movex,movey,posx,posy,click,posx1,posy1,bullet,direction
    screen.blit(direction,(x,y))
    find(x,y)
    if click=='left':
        if x<=640 and x>=0:
            if x>int(run):
                x+=xspeed*movex
                if x<int(run):
                    xspeed=0
        if y<=480 and x>=0:
            if y>int(rise):
                y+=yspeed*movey
                if y<int(rise):
                    yspeed=0
        if x<=640 and x>=0:
            if x<int(run):
                x+=xspeed*movex
                if x>int(run):
                    xspeed=0
        if y<=480 and x>=0:
            if y<int(rise):
                y+=yspeed*movey
                if y>int(rise):
                    yspeed=0
    print(x,y)
    if click=='right':
        bullet+=1
        pygame.draw.rect(screen,THECOLORS['blue'],(posx[bullet],posy[bullet],10,10))
        find(posx[bullet],posy[bullet])
        if posx[bullet]<=640 and posx[bullet]>=0:
            if posx[bullet]>int(run):
                posx[bullet]+=xspeed*movex
                if posx[bullet]<int(run):
                    xspeed=0
        if posy[bullet]<=480 and posx[bullet]>=0:
            if posy[bullet]>int(rise):
                posy[bullet]+=yspeed*movey
                if posy[bullet]<int(rise):
                    yspeed=0
        if posx[bullet]<=640 and posx[bullet]>=0:
            if posx[bullet]<int(run):
                posx[bullet]+=xspeed*movex
                if posx[bullet]>int(run):
                    xspeed=0
        if posy[bullet]<=480 and posx[bullet]>=0:
            if posy[bullet]<int(rise):
                posy[bullet]+=yspeed*movey
                if posy[bullet]>int(rise):
                    yspeed=0
    if x>640:
        x=640
    if x<0:
        x=0
    if y>480:
        y=480
    if y<0:
        y=0

class spawn(object):
    def __init__(self,primaryx,primaryy):
        x1=random.randint(100,400)
        y1=random.randint(100,400)
        self.x1=x1
        self.y1=y1
        self.primaryx=primaryx
        self.primaryy=primaryy
    def AIPrototype(self):#The important parts to this error star here
        global x,y
        pygame.draw.rect(screen,THECOLORS['blue'],(self.primaryx,self.primaryy,50,50))
        self.x1=self.primaryx
        self.y1=self.primaryy
        if self.x1<x:
            xspeed1=1
            slopex1=x-self.x1
        if self.x1>x:
            xspeed1=-1
            slopex1=self.x1-x
        if self.y1<y:
            yspeed1=1
            slopey1=y-self.y1
        if self.y1>y:
            yspeed1=-1
            slopey1=self.y1-y       
    #
        hypo1=((slopex1**2)+(slopey1**2))**0.5
        speedmark1=hypo1/3
        speedy1=slopey1/speedmark1
        speedx1=slopex1/speedmark1
        movex1=speedx1
        movey1=speedy1
        if self.x1<=640 and self.x1>=0:
            if self.x1>x:
                self.x1+=xspeed1*movex1
                if self.x1<x:
                    xspeed1=0
        if self.y1<=480 and self.x1>=0:
            if self.y1>y:
                self.y1+=yspeed1*movey1
                if self.y1<y:
                    yspeed1=0
        if self.x1<=640 and self.x1>=0:
            if self.x1<x:
                self.x1+=xspeed1*movex1
                if self.x1>x:
                    xspeed1=0
        if self.y1<=480 and self.x1>=0:
            if self.y1<y:
                self.y1+=yspeed1*movey1
                if self.y1>y:
                    yspeed1=0
    #
        if self.x1>640:
            self.x1=640
        if self.x1<0:
            self.x1=0
        if self.y1>480:
            self.y1=480
        if self.y1<0:
            self.y1=0
        self.primaryx=self.x1
        self.primaryy=self.y1
run=0
rise=0
wait=0
wait1=0
l=0
x1=0
y1=0
spawn=spawn(600,200)
spawn2=spawn(100,200)
clock = pygame.time.Clock() 
keepGoing = True        

try:
    while keepGoing:
        clock.tick(60) 
        screen.fill(THECOLORS['red'])
        char()#start
        posx.append(x)
        posy.append(y)
        spawn.AIPrototype()
        spawn2.AIPrototype()
        pygame.display.flip()
        for ev in pygame.event.get(): 
            if ev.type == pygame.QUIT: 
                keepGoing = False
            if ev.type==MOUSEBUTTONDOWN:
                move()
                if pygame.mouse.get_pressed()[0]==1:
                    click='left'
                if pygame.mouse.get_pressed()[2]==1:
                    click='right'                
            if ev.type==KEYDOWN:
                if ev.key==K_l:
                    shoot='yes'
            if ev.type==KEYUP:
                shoot='no'
finally:
    pygame.quit()  # Keep this IDLE friendly
#fuction to write a fuction
#>1 spawn not working
#enemy jumps

我知道这些代码大部分效率低下,我很抱歉。我是python和类的新手。

1 个答案:

答案 0 :(得分:0)

您的类名为 spawn,然后您创建一个名为 spawn 的类的实例(在:spawn=spawn(600,200)) 中,它掩盖了类...使用大写的类名称是惯例 - 例如:{ {1}}...所以,简而言之 - 更改名称