|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这个是出的问题,求助大佬
Traceback (most recent call last):
File "/home/bbbbb/Desktop/HM/planemain.py", line 92, in <module>
game.start_game()
File "/home/bbbbb/Desktop/HM/planemain.py", line 39, in start_game
self.__update_sprites()
File "/home/bbbbb/Desktop/HM/planemain.py", line 76, in __update_sprites
group.draw(self.screen)
File "/home/bbbbb/.local/lib/python3.6/site-packages/pygame/sprite.py", line 476, in draw
self.spritedict[spr] = surface_blit(spr.image, spr.rect)
pygame.error: display Surface quit
Process finished with exit code 1
这个是我的代码,这是主要一段,还有一段辅助的
import pygame
from planesprites import *
class Planegame(object):
def __init__(self):
print("游戏初始化")
self.screen = pygame.display.set_mode(SCREEN_RECT.size)
self.clock = pygame.time.Clock()
self.__create_sprites()
pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)
pygame.time.set_timer(HERO_FIRE_EVENT,500)
def __create_sprites(self):
bg1 = Background()
bg2 = Background(True)
self.back_group = pygame.sprite.Group(bg1, bg2)
self.enemy_group = pygame.sprite.Group()
self.hero = Hero()
self.hero_group = pygame.sprite.Group(self.hero)
def start_game(self):
print("游戏开始")
while True:
self.clock.tick(FRAME_PER_SEC)
self.__event_handler()
self.__check_collide()
self.__update_sprites()
pygame.display.upadte()
def __event_handler(self):
for event in pygame.event.get():
if event == pygame.quit():
Planegame.__game_over()
elif event == CREATE_ENEMY_EVENT:
print("enemy coming!")
enemy = Enemy()
self.enemy_group.add(enemy)
elif event ==HERO_FIRE_EVENT:
self.hero.fire()
pygame.init()
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_RIGHT]:
# print("我持续向右移动")
self.hero.speed=2
elif key_pressed[pygame.K_LEFT]:
self.hero.speed=-2
else:
self.hero.speed=0
def __check_collide(self):
pygame.sprite.groupcollide(self.hero.bullets,self.enemy_group,True,True)
enemies=pygame.sprite.spritecollide(self.hero,self.enemy_group,True)
if len(enemies)>0:
self.hero.kill()
Planegame.__game_over()
def __update_sprites(self):
for group in [self.back_group, self.enemy_group,
self.hero_group, self.hero.bullets]:
group.update()
group.draw(self.screen)
@staticmethod
def __game_over():
print("游戏结束。。")
pygame.quit()
exit()
if __name__ == '__main__':
# 创建游戏对象
game = Planegame()
# 启动游戏
game.start_game()
第二段
import random
import pygame
SCREEN_RECT=pygame.Rect(0,0,480,700)
# 屏幕大小常量
FRAME_PER_SEC=60
# 刷新贞率常量
CREATE_ENEMY_EVENT=pygame.USEREVENT
HERO_FIRE_EVENT=pygame.USEREVENT+1
# 设置常量
class GameSprite(pygame.sprite.Sprite):
def __init__(self,image_name,speed=1):
super().__init__()
self.image=pygame.image.load(image_name)
self.speed=speed
self.rect=self.image.get_rect()
def update(self):
self.rect.y+=self.speed
class Background(GameSprite):
def __init__(self, is_alt=False):
super().__init__("./images/background.png")
if is_alt:
self.rect.y = -self.rect.height
def update(self):
super().update()
if self.rect.y>=SCREEN_RECT.height:
self.rect.y=-self.rect.height
pass
class Enemy(GameSprite):
def __init__(self):
super().__init__("./images/enemy1.png")
self.speed=range.randint(1,3)
self.rect.bottom=0
x_max=SCREEN_RECT.X - self.rect.width
self.rect.x=random.randint(0,x_max)
def update(self):
if self.rect.y >= SCREEN_RECT.height:
print("delete enemy plane")
self.kill()
# 销毁精灵
class Hero(GameSprite):
def __init__(self):
super().__init__("./images/me1.png",0)
self.rect.centerx=SCREEN_RECT.centerx
self.rect.bottom=SCREEN_RECT.bottom-120
self.bullets=pygame.sprite.Group()
def update(self):
self.rect.x+=self.speed
if self.rect.x<0:
self.rect.x=0
elif self.rect.right>SCREEN_RECT.right:
self.rect.right=SCREEN_RECT
def fire(self):
for i in (0,1,2):
bullet = Bullet()
bullet.rect.y= self.rect.y-(i*20)
bullet.rect.x=self.rect.centerx
self.hero.bullets.add(bullet)
class Bullet(GameSprite):
def __init__(self):
super().__init__("./images/bullet1.png",(-2))
def update(self):
super().update()
if self.rect.bottom<0:
self.kill()
def __del__(self):
print("子弹没有了")
|
|