|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import random
import time
import pygame # 导入pygame模块
from pygame.constants import *
# 我方飞机类属性
class HeroPlane(pygame.sprite.Sprite):
def __init__(self, screen):
pygame.sprite.Sprite.__init__(self)
self.player = pygame.image.load("./feiji/hero.gif") # 导入我方飞机
self.rect = self.image.get_rect()
self.rect.topleft = [480 / 2, -100 / 2, 600]
self.speed = 10 # 飞机速度
self.screen = screen
self.bullste = pygame.sprite.Group()
def key_control(self):
key_pressed = pygame.key.get_pressed()
if key_pressed[K_w] or key_pressed[K_UP]:
self.rect.top -= self.speed
if key_pressed[K_s] or key_pressed[K_DOWN]:
self.rect.bottom += self.speed
if key_pressed[K_a] or key_pressed[K_LEFT]:
self.rect.left -= self.speed
if key_pressed[K_d] or key_pressed[K_RIGHT]:
self.rect.right += self.speed
if key_pressed[K_SPACE]:
bullet = Bullet(self.screen, self.rect.left, self.rect.top)
self.bullste.add(bullet)
def update(self):
self.key_control()
self.display()
def display(self):
self.screen.blit(self.player, self.rect) # 我方飞机坐标
self.bullste.update()
self.bullste.draw(self.screen)
# 敌方飞机类属性
class EnemyPlane(pygame.sprite.Sprite):
def __init__(self, screen):
pygame.sprite.Sprite.__init__(self)
self.player = pygame.image.load("./feiji/enemy0.png.gif") # 导入我方飞机
self.rect = self.image.get_rect()
self.rect.topleft = [0, 0]
self.speed = 10 # 飞机速度
self.screen = screen
self.bullste = pygame.sprite.Group()
self.dirextion = 'right'
def display(self):
self.screen.blit(self.player, self.rect) # 我方飞机坐标
for _ in self.bullste:
self.bullste.update()
self.bullste.draw(self.screen)
def update(self):
self.auto_move()
self.auto_fire()
self.display()
def auto_move(self):
if self.dirextion == 'right':
self.rect.right += self.speed
elif self.dirextion == 'left':
self.rect.right -= self.speed
if self.rect.right > 480 - 51:
self.dirextion = 'left'
elif self.rect.right < 0:
self.dirextion = 'right'
def auto_fire(self):
random_num = random.randint(1, 20)
if random_num == 8:
bullet = EnemyBullet(self.screen, self.rect.left, self.rect.top)
self.bullste.add(bullet)
# 子弹类属性
class Bullet(pygame.sprite.Sprite):
def __init__(self, screen, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("./feiji/bullet.png")
self.rect = self.image.get_rect()
self.rect.topleft = [x + 100 / 2 - 22 / 2, y - 22]
self.screen = screen
self.speed = 20
def update(self):
self.rect.top -= self.speed
if self.rect.top < -22:
self.kill()
# 敌方子弹类属性
class EnemyBullet(pygame.sprite.Sprite):
def __init__(self, screen, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("./feiji/bullet1.png.png")
self.rect = self.image.get_rect()
self.rect.topleft = [x + 50 / 2 - 8 / 2, y - 39]
self.screen = screen
self.speed = 20
def update(self):
self.rect.top += self.speed
if self.rect.top > 852:
self.kill()
def main():
"""完成整个程序的控制"""
screen = pygame.display.set_mode((480, 852), 0, 32) # 创建窗口
backgeound = pygame.image.load("./feiji/background.png") # 导入图片,做背景图
player = HeroPlane(screen)
enemyplane = EnemyPlane(screen)
while True:
screen.blit(backgeound, (0, 0)) # 背景图的坐标
for event in pygame.event.get(): # 获取事件
if event.type == pygame.QUIT: # 判断事件类型
pygame.quit() # 执行pygame程序退出
exit() # pygame程序退出
player.key_control()
player.display()
enemyplane.display()
enemyplane.auto_move()
enemyplane.auto_fire()
pygame.display.update() # 显示窗口内容
time.sleep(0.01) # 操作键盘的反应时间
if __name__ == '__main__':
main()
|
|