import pygame # pygame2d游戏 import time from pygame.locals import *class HeroPlane(object):def __init__(self,screen_temp):self.x=210self.y=700self.screen=screen_tempself.image= pygame.image.load("./feiji/hero1.png")self.bullet_list=[]def display(self):self.screen.blit(self.image, (self.x, self.y))for bullet in self.bullet_list:bullet.display()bullet.move()def move_left(self):self.x-=5def move_right(self):self.x+=5def fire(self):self.bullet_list.append(Bullet(self.screen,self.x,self.y))class Bullet(object):def __init__(self,screen_temp,x,y):self.x = x+40self.y = y-20self.screen = screen_tempself.image = pygame.image.load("./feiji/bullet.png")def display(self):self.screen.blit(self.image,(self.x,self.y))def move(self):self.y-=20def key_control(herotemp):for event in pygame.event.get(): # 11if event.type == QUIT: # 按下叉号print("exit")exit() # 退出程序,循环结束是breakelif event.type == KEYDOWN: # 按下键盘键if event.key == K_a or event.key == K_LEFT:print("left")herotemp.move_left()elif event.key == K_d or event.key == K_RIGHT:print("right")herotemp.move_right()elif event.key == K_SPACE:print("space")herotemp.fire() def main(): # 01screen = pygame.display.set_mode((480, 852), 0, 32) # 1窗口background = pygame.image.load("./feiji/background.png") # 2背景hero = HeroPlane(screen)while True: # 5死循环,一直重复screen.blit(background, (0, 0)) # 3背景贴到窗口00处 hero.display()pygame.display.update() # 4显示贴的效果 key_control(hero)time.sleep(0.01) # 6电脑配置不高的话,内存占用会很大,所以延时显示可以减少内存消耗if __name__ == '__main__': # 0main()