pygame(class类)调用视图的方法

以下将介绍pygame精灵动画的基础知识,希望对大家有帮助:
1.在此,精灵类必须继承pygame.sprite.Sprite并初始化pygame.sprite.Sprite.__init__(self),
2.调用group=pygame.sprite.Group()的精灵组必须要在类中初始化image和rect属性

import pygame
import sys
# -------------子弹类-----------
pygame.init()
screen=pygame.display.set_mode([800,600])


class Bullet(pygame.sprite.Sprite):

def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.bullet_up=pygame.image.load(r"..\image\bullet_up.png")

self.bullet=self.bullet_down
self.rect=self.bullet.get_rect()

self.image = self.bullet

def changeImage(self,dir_x,dir_y):
self.bullet = self.bullet_down
self.dir_x,self.dir_y=dir_x,dir_y
if self.dir_x==0 and self.dir_y==-1:
self.bullet=self.bullet_up
elif self.dir_x==0 and self.dir_y==1:
self.bullet=self.bullet_down

def move(self):
# 子弹当前位置等于移动后的位置
self.rect=self.rect.move(self.dir_x*self.speed,self.dir_y*self.speed)
# 设置子弹碰撞地图边缘自动毁灭消失
if self.rect.top<3:
self.life=False


实例化类
ss=Bullet()

生成精灵组
group=pygame.sprite.Group()

将实例化对象添加到精灵组
group.add(ss)
while True:
print('我被调用了')
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
screen.fill([0,0,0])
group.update()
group.draw(screen)
pygame.display.update()
 
 
posted @ 2018-09-29 15:31  L某人  阅读(1159)  评论(0编辑  收藏  举报