Python从入门到实践14-2射击练习

项目分析

1.创建一个矩形,它在屏幕右边缘以固定的速度上下移动。

2.在屏幕左边缘创建一艘飞船,玩家可上下移动该飞船,并射击前述矩形目标。

3.添加一个用于开始游戏的Play 按钮,在玩家三次未击中目标时结束游戏,并重新显示Play 按钮,让玩家能够通过单击该按钮来重新开始游戏。

1.通用设置

settings.py

class Settings():
    def __init__(self):
        self.screen_width=800
        self.screen_height=600
        self.bg_color=(255,255,255)
        self.enemy_droop_speed=1
        self.enemy_color=(0,255,0)
        self.enemy_direction=1
        self.defeat_times=0

2.运行游戏类

shooting_game.py

import pygame
from pygame.sprite import Group
from game_stats import Game_stats
from settings import Settings
import game_function as gf
from enemy import Enemy
from ship import Ship
from button import Button
def run_game():
    #初始化pygame
    pygame.init()
    #初始化游戏属性
    settings = Settings()
    #窗口surface
    screen=pygame.display.set_mode((settings.screen_width,settings.screen_height))
    pygame.display.set_caption("Shooting_game")
    ship=Ship(settings,screen)
    enemy = Enemy(settings, screen)
    screen.fill(settings.bg_color)
    bullets=Group()
    stats=Game_stats(settings,screen)
    play_button=Button(settings,screen,"Play")
    while True:
        gf.check_event(settings,screen,ship,bullets,play_button,stats)
        if stats.game_active:
            gf.check_defeat(settings,bullets,screen,enemy,stats,ship)
            bullets.update()
            enemy.update_enemy()
        gf.update_screen(settings,screen,enemy,ship,bullets,stats,play_button)
run_game()

3.创建射击目标rect

使用pygame中的Rect方法创建矩形

enemy.py

import pygame
class Enemy():
    def __init__(self,settings,screen):
        self.settings=settings
        self.screen=screen
        self.screen_rect=self.screen.get_rect()
        self.color=self.settings.enemy_color
        self.width,self.height=10,50
        #绘制矩形,设置位置
        self.rect=pygame.Rect(0,0,self.width,self.height)
        self.rect.right=self.screen_rect.right
        self.rect.top=self.screen_rect.top
        self.y=self.rect.y
    #移动敌人
    def update_enemy(self):
        if self.rect.top <0 or self.rect.bottom>self.screen_rect.bottom:
            self.settings.enemy_direction*=-1
        self.y+=self.settings.enemy_droop_speed*self.settings.enemy_direction
        self.rect.y=self.y
    #绘制敌人
    def draw_enemy(self):
        pygame.draw.rect(self.screen,self.color,self.rect)

4.添加ship

添加ship图片,设置初始位置,按键控制ship上下移动

ship.py

import pygame
class Ship():
    def __init__(self,settings,screen):
        '''初始化属性'''
        self.settings=settings
        self.screen=screen
        self.screen_rect=self.screen.get_rect()
        #加载图片
        self.image=pygame.image.load("images/ship.png")
        #上下移动标志实现一直移动
        self.moving_up=False
        self.moving_down=False
        #设置图片位置
        self.rect=self.image.get_rect()
        self.rect.left=self.screen_rect.left
        self.rect.centery=self.screen_rect.centery
    #绘制
    def blitme(self):
        image_t=pygame.transform.rotate(self.image,-90)
        self.screen.blit(image_t,self.rect)
    #更新上下移动飞船
    def update(self):
        if self.moving_up and self.rect.y>0 :
            self.rect.y-=1
        elif self.moving_down and self.rect.bottom<self.screen_rect.bottom:
            self.rect.y+=1
    def center_ship(self):
        self.rect.left = self.screen_rect.left
        self.rect.centery = self.screen_rect.centery

5.重构出game_function

一些游戏功能,减轻主函数负担

game_function.py

import sys
from bullet import Bullet
import pygame
#检测事件
def check_event(settings,screen,ship,bullets,play_button,stats):
    #遍历pygame中出现的事件
    for event in pygame.event.get():
        #判断事件类型
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type==pygame.KEYDOWN:
            check_keydown_event(settings,screen,event,ship,bullets)
        elif event.type==pygame.KEYUP:
            check_keyup_event(event,ship)
        elif event.type==pygame.MOUSEBUTTONDOWN:
            mouse_x,mouse_y=pygame.mouse.get_pos()
            check_play_button(ship,play_button,mouse_x,mouse_y,stats,bullets)
def check_play_button(ship,play_button,mouse_x,mouse_y,stats,bullets):
    button_cliked=play_button.rect.collidepoint(mouse_x,mouse_y)
    if button_cliked and not stats.game_active:
        stats.game_active=True
        bullets.empty()
        ship.center_ship()
        stats.reset_stats()
def fire_bullet(settings,screen,ship,bullets):
    new_bullet = Bullet(settings, screen, ship)
    bullets.add(new_bullet)
def check_defeat(settings,bullets,screen,enemy,stats,ship):
    for bullet in bullets.copy():
        if bullet.rect.right>screen.get_rect().right:
            bullets.remove(bullet)
            settings.defeat_times+=1
        elif pygame.sprite.spritecollideany(enemy,bullets):
            settings.defeat_times=0
        elif settings.defeat_times>2:
            bullets.empty()
            stats.game_active=False
            ship.center_ship()
##按键落下
def check_keydown_event(settings,screen,event,ship,bullets):
    if event.key==pygame.K_UP:
        ship.moving_up=True
    elif event.key==pygame.K_DOWN:
        ship.moving_down=True
    elif event.key == pygame.K_SPACE:
        fire_bullet(settings, screen, ship, bullets)
#按键抬起
def check_keyup_event(event,ship):
    if event.key==pygame.K_UP:
        ship.moving_up=False
    elif event.key==pygame.K_DOWN:
        ship.moving_down=False
#更新屏幕
def update_screen(settings,screen,enemy,ship,bullets,stats,play_button):
    screen.fill(settings.bg_color)
    enemy.draw_enemy()
    ship.update()
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    ship.blitme()
    if not stats.game_active:
        play_button.draw_button()
    pygame.display.flip()

6.添加bullets子弹

射击子弹

bullets.py

import pygame
from pygame.sprite import Sprite
class Bullet(Sprite):
    def __init__(self,settings,screen,ship):
        super(Bullet,self).__init__()
        self.settings=settings
        self.screen=screen
        self.color="black"
        self.rect=pygame.Rect(0,0,20,3)
        self.rect.centery=ship.rect.centery
        self.rect.right=ship.rect.right
    def draw_bullet(self):
        pygame.draw.rect(self.screen,self.color,self.rect)
    def update(self):
        self.rect.x+=1

7.统计信息类

新建Game_stats类用来统计游戏信息,以及在重新开始时重置游戏信息

game_stats.py

class Game_stats():
    def __init__(self,settings,screen):
        self.game_active=False
        self.settings=settings
        self.reset_stats()
    def reset_stats(self):
        self.settings.defeat_times=0

8.添加play_button按钮

button.py

import pygame.font
class Button():
    def __init__(self,settings,screen,msg):
        self.settings=settings
        self.screen=screen
        self.width,self.height=200,50
        self.button_color=0,0,255
        self.text_color=255,255,255
        self.font=pygame.font.SysFont(None,48)
        self.rect=pygame.Rect(0,0,self.width,self.height)
        self.screen_rect=self.screen.get_rect()
        self.rect.center=self.screen_rect.center
        self.prep_msg(msg)
    def prep_msg(self,msg):
        self.msg_image=self.font.render(msg,True,self.button_color,self.text_color)
        self.msg_image_rect=self.msg_image.get_rect()
        self.msg_image_rect.center=self.rect.center
    def draw_button(self):
        self.screen.fill(self.button_color,self.rect)
        self.screen.blit(self.msg_image,self.msg_image_rect)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值