Python 3.x 类名不能调用两次
我试图在一场游戏中产生多个敌人。由于某些原因,当我尝试调用该类两次时,它会给我一个错误:Python 3.x 类名不能调用两次,python-3.x,class,pygame,Python 3.x,Class,Pygame,我试图在一场游戏中产生多个敌人。由于某些原因,当我尝试调用该类两次时,它会给我一个错误:类型错误:spawn对象不可调用。我不知道该怎么办。有人能帮我吗? 下面是调用该类的代码: class spawn(object): def __init__(self,primaryx,primaryy): x1=random.randint(100,400) y1=random.randint(100,400) self.x1=x1
类型错误:spawn对象不可调用
。我不知道该怎么办。有人能帮我吗?
下面是调用该类的代码:
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
如果您想运行代码,下面是完整的内容:
#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
#阿哈玛德阿姨
#06/06/2017
#最终项目版本1
#斯特列科夫斯卡女士
随机输入
"""
YA是在Pygame中开始编程的基本模板
"""
#导入并初始化pygame模块
导入pygame
#pygame.locals包含MOUSEMOTION、MOUSEBUTTONUP和QUIT for events等常量#键入MOUSEBUTTONUP比键入pygame.locals.MOUSEBUTTONUP更容易
从pygame.locals导入*
#最好使用pygame.MOUSEMOTION
#这将允许我们命名要使用的颜色,而不是给出名称,例如(255,0,0)
从pygame.color导入颜色
#c=(255,0,0)而不是颜色['red']????
#初始库本身
pygame.init()
def move():
全局x,y,y速度,x速度,上升,运行,移动x,移动
spot=pygame.mouse.get_pos()
spoot=str(点)
spoot1=myfont.render((spoot),True,(颜色['blue']))
屏幕光点(spoot1,(spot))
spoot=spoot.replace('(','')
spoot=spoot.replace(',','')
spoot=spoot.replace('),“”)
空格=spoot.index(“”)
上升=跳跃[空格+1:]
run=spoot[:空格]
xspeed=0
Y速度=0
#就像python一样,我们将使用操作系统和时间????
导入操作系统,时间
myfont=pygame.font.SysFont(“comicsansms”,20)
#这段代码是python在tdsb计算机上工作所必需的????
导入平台
如果platform.system()=“Windows”:
os.environ['SDL\u VIDEODRIVER']='windib'
#在中设置主屏幕显示窗口和标题
大小=(640480)
screen=pygame.display.set_模式(大小)
#在窗口顶部的栏中放置标题
pygame.display.set_标题(“是的,您好!”)
#用颜色填充内存屏幕表面
屏幕填充(颜色['red'])
#更新并刷新显示以结束此帧
pygame.display.flip()#int(运行):
xspeed=-1
slopex=x2整数(运行)
如果abs(slopex)>abs(slopey):
方向=士兵
如果y2abs(slopex):
方向=士兵镇
如果y2>int(上升):
Y速度=-1
坡度=y2整数(上升)
如果abs(slopey)>abs(slopex):
方向=士兵
hypo=((slopex**2)+(slopey**2))**0.5
speedmark=hypo/5
斯皮蒂=斯皮蒂/斯皮德马克
speedx=slopex/speedmark
movex=speedx
快速的
打印(abs(内部(运行)),abs(内部(上升)))
def char():
全局x,y,x速度,y速度,移动x,移动y,posx,posy,点击,posx1,posy1,项目符号,方向
屏幕光点(方向,(x,y))
查找(x,y)
如果单击==“左”:
如果x=0:
如果x>int(运行):
x+=xspeed*movex
如果xint(上升):
y+=y速度*移动
如果yint(运行):
posx[bullet]+=xspeed*movex
如果posx[bullet]int(上升):
posy[子弹]+=yspeed*movey
如果posy[bullet]640:
x=640
如果是x480:
y=480
如果yx:
self.x1+=xspeed1*movex1
如果self.x1y:
self.y1+=yspeed1*movey1
如果self.y1640:
self.x1=640
如果self.x1480:
自校正y1=480
如果self.y11生成不工作
#敌人跳跃
我知道这段代码大部分是低效的,对此我很抱歉。我对python和类相当陌生。您的类被称为
spawn
,然后您创建一个名为spawn的类实例(在:spawn=spawn(600200))
中,它屏蔽了该类。。。对类使用大写的名称是惯例-例如:Spawn
。。。因此,简而言之-更改名称您的类名为spawn
,然后创建一个名为spawn
(in:spawn=spawn(600200)
)的类实例,该实例屏蔽了该类。。。对类使用大写的名称是惯例-例如:Spawn
。。。所以,简而言之,改变名字谢谢它起作用了:P我要把这个问题标记为解决了吗?
#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