Python 3.x 我如何创建/运行这个类?python
我正在做一个游戏,你在银河系周围飞行,应该有其他飞船在某处飞行,但我怎么能真正运行这个呢?让我们假设像Xa次数 那么什么时候有办法关闭所有船只Python 3.x 我如何创建/运行这个类?python,python-3.x,pygame,Python 3.x,Pygame,我正在做一个游戏,你在银河系周围飞行,应该有其他飞船在某处飞行,但我怎么能真正运行这个呢?让我们假设像Xa次数 那么什么时候有办法关闭所有船只 class spaceship(): ###thigs that fly arround when GUI == 4 def __init__(self,GUIstatus): self.shipUp = pygame.image.load("icons/shipUP.png") self.shipRight
class spaceship(): ###thigs that fly arround when GUI == 4
def __init__(self,GUIstatus):
self.shipUp = pygame.image.load("icons/shipUP.png")
self.shipRight = pygame.image.load("icons/shipRIGHT.png")
self.shipLeft = pygame.image.load("icons/shipLEFT.png")
self.shipDown = pygame.image.load("icons/shipDOWN.png")
self.shipImage = self.shipLeft
self.shipnpc_x = 0
self.shipnpc_y = 0
self.GUIstatus = GUIstatus
self.shipType = (random.randrange(0,10))
self.randomspeed = (random.randrange(0,5))
self.speed = 10 += self.randomspeed
self.exists = 0
##Logic place for selecting and randomizing ship and things
def Update(self):
if self.exists == 1:
return
if self.exists == 0 and self.GUIstatus == 4:
if dirrection == LEFT:
self.shipnpc_x +=selfspeed
self.shipImage = self.shipLeft
if dirrection == RIGHT:
self.shipnpc_x -=selfspeed
self.shipImage = self.shipRight
if dirrection == UP:
self.shipnpc_y -=self.speed
self.shipImage = self.shipUp
if dirrection == DOWN:
self.npcship_y +=self.speed
self.shipImage = self.shipDown
def Draw(self,screen):
if self.exists == 1:
return
screen.blit(self.shipImage,(self.shipnpc_x,self.shipnpc_y))
别担心,这实际上是初学者面临的一个相当普遍的问题 您要做的是将对象放在某种类型的列表中(在python中,您通常使用
[]
),而不是对单个对象调用方法,如
ship1.Update()
使用循环在该列表上迭代,如
for ship in ships:
ship.Update()
但是因为您使用的是pygame,所以更简单,因为有一个类可以为您完成一些繁重的工作
您应该从使用and类开始,因此让我们更改您的spaceship
类。注意评论
class Spaceship(pygame.sprite.Sprite): ###thigs that fly arround when GUI == 4
def __init__(self, ships, GUIstatus):
# ships is a Group for all ships
pygame.sprite.Sprite.__init__(self, ships)
self.shipUp = ...
# the image has to be in self.image
self.image = self.shipLeft
# no need for shipnpc_x and shipnpc_y, since we store
# our position in self.rect
self.rect = self.image.get_rect()
self.GUIstatus = GUIstatus
self.shipType = ...
# this has to be called update instead of update
# to work with groups
def update(self):
# to change the position, we just alter self.rect
if self.exists == 0 and self.GUIstatus == 4:
if dirrection == LEFT:
self.rect.move_ip(-self.speed, 0)
self.shipImage = self.shipLeft
if dirrection == RIGHT:
self.rect.move_ip(self.speed, 0)
self.shipImage = self.shipRight
if dirrection == UP:
self.rect.move_ip(0, -self.speed)
self.shipImage = self.shipUp
if dirrection == DOWN:
self.rect.move_ip(0, self.speed)
self.shipImage = self.shipDown
# no need for the Draw function, since it's handled
# by the sprite class' draw function
现在有了这些更改,我们只需创建一个:
因此,无论何时创建太空船,只要将组传递给初始值设定项,就可以将其放入ships组。如果您想要30艘船,只需使用一个循环:
for _ in xrange(30):
Spaceship(ships, GUIstatus)
至于更新和绘图,只需致电
ships.update()
ships.draw(screen)
在主循环中,组
类负责调用update
和draw
,在每个Sprite
中调用(基本上,它只是在一个列表中迭代一个for
,就像我上面写的那样)
就这样!如果你想摆脱宇宙飞船,只需调用它的函数(可能在update方法中),或者使用类似于自动完成的方法
最后,您可能希望有多个组,例如,一个组用于在主循环中调用的
update
和draw
,并且,如果您创建一个射击游戏,一个组用于所有敌人,一个组用于子弹,这样您就可以使用碰撞检测等等。您的问题有点不清楚。您可以通过ship1=spaceship()
创建新实例,然后使用ship1.draw()
或ship1.update()
调用update/draw方法。但我猜你不是这个意思吧?此外,通常将类名命名为Spaceship
(CamelCase)和方法/变量update
(小写)。这种约定使其他程序员更容易理解彼此的程序。您需要mainloop
在其中移动每个循环中的所有程序。每艘船都应该有自己的方法来改变它的位置。在你有玩家和两个摩托的地方-当你移动玩家时,摩托也会移动。是的,很抱歉有点不清楚,我怎么能一次创建30艘飞船,而不是每次都说ship1=spaceship()'self.rect.move_ip(self.speed,0)'在(x,y)里面,对吗?那么xrange应该是什么呢?
ships.update()
ships.draw(screen)