文章目录
- 参数
- 火箭 rocket.py
- 子弹 zidan.py
- 操作实现 function.py
- 主函数
- 效果图
参数
class Canshu():
def __init__(self):
self.screen_width = 1200
self.screen_height = 800
self.bg_color = (250,250,250)
self.rocket_speed = 2
self.zidan_speed = 1.5
self.zidan_width = 15
self.zidan_height = 3
self.zidan_color = 60,60,60
self.zidans_allowed = 5
火箭 rocket.py
import pygame
class Rocket():
def __init__(self,set,screen):
self.set = set
self.screen = screen
self.image = pygame.image.load('images/huojian.jpg')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
self.rect.centery = self.screen_rect.centery
self.rect.left = self.screen_rect.left
self.center = float(self.rect.centery)
self.moving_up = False
self.moving_down = False
def update(self):
if self.moving_up and self.rect.top > 0:
self.center -= self.set.rocket_speed
if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
self.center += self.set.rocket_speed
self.rect.centery = self.center
def blitme(self):
self.screen.blit(self.image,self.rect)
子弹 zidan.py
import pygame
from pygame.sprite import Sprite
class Zidan(Sprite):
def __init__(self, set, screen, rocket):
super().__init__()
self.screen = screen
self.rect = pygame.Rect(0,0,set.zidan_width,set.zidan_height)
self.rect.centery = rocket.rect.centery
self.rect.right = rocket.rect.right
self.x = float(self.rect.x)
self.color = set.zidan_color
self.speed = set.zidan_speed
def update(self):
self.x += self.speed
self.rect.x = self.x
def draw_zidan(self):
pygame.draw.rect(self.screen,self.color,self.rect)
操作实现 function.py
import sys
import pygame
from zidan import Zidan
def check_keydown_events(event, set, screen, rocket, zidans):
"""响应按键"""
if event.key == pygame.K_UP:
rocket.moving_up = True
elif event.key == pygame.K_DOWN:
rocket.moving_down = True
elif event.key == pygame.K_SPACE:
fire_zidan(set, screen, rocket, zidans)
def check_keyup_events(event, rocket):
'''响应松开'''
if event.key == pygame.K_UP:
rocket.moving_up = False
elif event.key == pygame.K_DOWN:
rocket.moving_down = False
def check_events(set, screen, rocket, zidans):
"""响应鼠标、键盘事件"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown_events(event, set, screen, rocket, zidans)
elif event.type == pygame.KEYUP:
check_keyup_events(event, rocket)
def fire_zidan(set, screen, rocket, zidans):
# 如果子弹未超过限制,创建新子弹,加入子弹编组中.
if len(zidans) < set.zidans_allowed:
new_zidan = Zidan(set, screen, rocket)
zidans.add(new_zidan)
def update_screen(set, screen, rocket, zidans):
"""更新屏幕图像,显示新的图像"""
# 每次循环时重新绘制屏幕
screen.fill(set.bg_color)
#画出每个子弹位置
for zidan in zidans.sprites():
zidan.draw_zidan()
#画出飞船位置
rocket.blitme()
# 让最近绘制的屏幕可见
pygame.display.flip()
def update_zidans(set, zidans):
# 更新子弹位置
zidans.update()
# 删除消失子弹
for zidan in zidans.copy():
if zidan.rect.left > set.screen_width:
zidans.remove(zidan)
主函数
import pygame
from canshu import Canshu
from rocket import Rocket
import function as f
from pygame.sprite import Group
def run_game():
pygame.init()
set = Canshu()
screen = pygame.display.set_mode((set.screen_width, set.screen_height))
pygame.display.set_caption("zrq fire rocket")
bg_color = (230,230,230)
rocket = Rocket(set,screen)
zidans = Group()
while True:
# 监视键盘鼠标事件
f.check_events(set, screen, rocket, zidans)
rocket.update()
f.update_zidans(set,zidans)
f.update_screen(set, screen, rocket, zidans)
run_game()
效果图