20201320 《Python程序设计》实验四报告

20201320 《Python程序设计》实验四报告  

课程:《Python程序设计》
班级: 2013
姓名: 石志涛
学号:20201320
实验教师:王志强
实验日期:2020年6月13日
必修/选修: 公选课

 

1.实验内容

python综合实践——游戏

2. 实验过程及结果

使用pygame包来进行编程

文件

1)构建程序主体

检测事件,刷新屏幕,重置游戏

2)编写功能文件

对主要运行的操作编写在这个文件里,按需添加、修改

3)设定文件

将主要游戏设定数据统一放在这个文件中,方便游戏调试以及修改数值

4)统计文件

积分、等级系统和死亡判定放入这个文件中

5)对象文件

所有对象文件的统称,包括用户、敌方单位、子弹、屏幕、计分板

源文件

(1)main_program.py

import pygame

from settings import Settings
from game_stats import GameStats
from scoreboard import Scoreboard
from button import Button
from pygame.sprite import Group
from user import User

import game_functions as gf


def run_game():
    pygame.init()
    print("1")
    clock = pygame.time.Clock()
    print("2")
    m_settings = Settings()
    print("3")
    screen = pygame.display.set_mode((m_settings.screen_width, m_settings.screen_height))
    print("4")
    pygame.display.set_caption("打飞机")
    print("5")
    play_button = Button(m_settings, screen, "Play")
    print("6")
    stats = GameStats(m_settings)
    print("7")
    player = User(m_settings, screen)
    print("8")
    bullets = Group()
    print("9")
    enemies = Group()
    print("10")
    sb = Scoreboard(m_settings, screen, stats)
    print("11")
    gf.create_fleet(m_settings, screen, player, enemies)
    print("12")

    while True:
        clock.tick(120)
        gf.check_events(m_settings, screen, stats, sb, play_button, player, enemies, bullets)
        if stats.game_active:
            player.update()
            gf.update_bullets(m_settings, screen, stats, sb, player, enemies, bullets)
            gf.update_enemies(m_settings, screen, stats, sb, player, enemies, bullets)
        gf.update_screen(m_settings, screen, stats, sb, player, enemies, bullets, play_button)


run_game()

在调试的时候加入了print函数来检查未导入的py文件,调用game_functions中的update_screen函数刷新屏幕

(2)game_functions.py

import pygame
import sys
from time import sleep
from bullet import Bullet
from enemy import Enemy


def check_keydown_event(event,m_settings,screen,player,bullets):
    if event.key == pygame.K_RIGHT:
        player.moving_right = True
    elif event.key == pygame.K_LEFT:
        player.moving_left = True
    if event.key == pygame.K_UP:
        player.moving_up = True
    elif event.key == pygame.K_DOWN:
        player.moving_down = True
    if event.key == pygame.K_SPACE:
        fire_bullet(m_settings, screen, player,bullets)


def check_keyup_event(event, player):
    if event.key == pygame.K_RIGHT:
        player.moving_right = False
    elif event.key == pygame.K_LEFT:
        player.moving_left = False
    if event.key == pygame.K_UP:
        player.moving_up = False
    elif event.key == pygame.K_DOWN:
        player.moving_down = False


def check_events(m_settings, screen, stats,sb,play_button, player,enemies, bullets):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        elif event.type == pygame.KEYDOWN:
            check_keydown_event(event,m_settings,screen,player,bullets)

        elif event.type == pygame.KEYUP:
            check_keyup_event(event,player)

        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            check_play_button(m_settings, screen, stats,sb,play_button, player,enemies, bullets,mouse_x,mouse_y)


def check_play_button(m_settings,screen,stats,sb,play_button,player,enemies,bullets,mouse_x,mouse_y):
    button_check = play_button.rect.collidepoint(mouse_x, mouse_y)
    if button_check and not stats.game_active:

        m_settings.initialize_dynamic_settings()

        pygame.mouse.set_visible(False)
        stats.reset_stats()
        stats.game_active = True

        sb.prep_score()
        sb.prep_high_score()
        sb.prep_level()
        sb.prep_players()
        sb.prep_bullet_allowed()

        enemies.empty()
        bullets.empty()

        create_fleet(m_settings,screen,player,enemies)
        player.center_player()


def update_screen(m_settings, screen, stats,sb,player,enemies,bullets,play_button):

    screen.fill(m_settings.bg_color)
    sb.show_score()
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    player.blitme()
    enemies.draw(screen)

    if not stats.game_active:
        play_button.draw_button()

    pygame.display.flip()


def update_bullets(m_settings,screen,stats,sb,player,enemies,bullets):
    bullets.update()

    for bullet in bullets.copy():
        if bullet.rect.bottom <= 0:
            bullets.remove(bullet)

    check_bullets_enemy_collisions(m_settings, screen,stats,sb, player, enemies, bullets)


def check_bullets_enemy_collisions(m_settings,screen,stats,sb,player,enemies,bullets):
    collisions = pygame.sprite.groupcollide(bullets, enemies, True, True)

    if collisions:
        for enemies in collisions.values():
            stats.score += m_settings.enemy_points * len(enemies)
            sb.prep_score()
        check_high_score(stats, sb)

    if len(enemies) == 0:
        bullets.empty()
        m_settings.increase_speed()

        stats.level += 1
        sb.prep_level()

        create_fleet(m_settings, screen, player, enemies)


def fire_bullet(m_settings, screen, player,bullets):
    if len(bullets) < m_settings.bullets_allowed:
        new_bullet = Bullet(m_settings, screen, player)
        bullets.add(new_bullet)


def get_number_enemy_x(m_settings, enemy_width):
    available_space_x = m_settings.screen_width - 2 * enemy_width
    number_enemies_x = int(available_space_x / (2 * enemy_width))
    return number_enemies_x


def get_number_rows(m_settings,player_height,enemy_height):
    available_space_y = (m_settings.screen_height - (3 * enemy_height) - player_height)
    number_rows = int(available_space_y / (2 * enemy_height)-1)
    return number_rows


def create_enemy(m_settings,screen,enemies,enemy_number,row_number):
    enemy = Enemy(m_settings, screen)
    enemy_width = enemy.rect.width
    enemy.x = enemy_width + 2 * enemy_width * enemy_number
    enemy.rect.x = enemy.x
    enemy.rect.y = enemy.rect.height + 2 * enemy.rect.height * row_number
    enemies.add(enemy)


def create_fleet(m_settings,screen,player,enemies):
    enemy = Enemy(m_settings, screen)
    number_enemies_x = get_number_enemy_x(m_settings, enemy.rect.width)
    number_rows = get_number_rows(m_settings,player.rect.height,enemy.rect.height)

    for row_number in range(number_rows):
        for enemy_number in range(number_enemies_x):
            create_enemy(m_settings,screen,enemies,enemy_number,row_number)


def check_fleet_edges(m_settings,enemies):
    for enemy in enemies.sprites():
        if enemy.check_edges():
            change_fleet_direction(m_settings,enemies)
            break


def change_fleet_direction(m_settings,enemies):
    for enemy in enemies.sprites():
        enemy.rect.y += m_settings.fleet_drop_speed
    m_settings.fleet_direction *= -1


def player_hit(m_settings,screen,stats,sb,player,enemies,bullets):
    if stats.players_left > 0:
        stats.players_left -= 1

        sb.prep_players()

        enemies.empty()
        bullets.empty()
        create_fleet(m_settings,screen,player,enemies)
        player.center_player()

        sleep(0.5)
    else:
        stats.game_active = False
        pygame.mouse.set_visible(True)


def update_enemies(m_settings,screen,stats,sb,player,enemies,bullets):
    check_fleet_edges(m_settings,enemies)
    enemies.update()

    if pygame.sprite.spritecollideany(player,enemies):
        player_hit(m_settings,screen,stats,sb,player,enemies,bullets)

    check_enemies_bottom(m_settings,screen,stats,sb,player,enemies,bullets)


def check_enemies_bottom(m_settings,screen,stats,sb,player,enemies,bullets):
    screen_rect = screen.get_rect()
    for enemy in enemies.sprites():
        if enemy.rect.bottom >= screen_rect.bottom:
            player_hit(m_settings,screen,stats,sb,player,enemies,bullets)
            break


def check_high_score(stats, sb):
    if stats.score > stats.high_score:
        stats.high_score = stats.score
        sb.prep_high_score()

所有的功能操作都放在这个文件中,根据游戏逻辑来调用对应功能

(3)settings.py

class Settings:
    def __init__(self):
        self.screen_width = 1280
        self.screen_height = 720
        self.bg_color = (230, 230, 235)

        self.player_limit = 4

        self.bullet_width = 3
        self.bullet_height = 10
        self.bullet_color = 60,60,60
        self.bullets_allowed = 5

        self.fleet_drop_speed = 10
        self.fleet_direction = 1

        self.speedup_scale = 1.1
        self.score_scale = 1.5

        self.initialize_dynamic_settings()

    def initialize_dynamic_settings(self):

        self.player_speed_factor = 1.5
        self.bullet_speed_factor = 3
        self.enemy_speed_factor = 0.5

        self.fleet_direction = 1

        self.enemy_points = 50

    def increase_speed(self):
        self.player_speed_factor *= self.speedup_scale
        self.bullet_speed_factor *= self.speedup_scale
        self.enemy_speed_factor *= self.speedup_scale

        self.enemy_points = int(self.enemy_points * self.score_scale)

设定游戏各项数据,可按照需求进行修改(将子弹大小扩大,修改窗口大小等)

(4)game_stats.py

class GameStats():

    def __init__(self,m_settings):
        self.m_settings = m_settings
        self.reset_stats()
        self.game_active = False
        self.high_score = 0

    def reset_stats(self):
        self.players_left = self.m_settings.player_limit
        self.score = 0
        self.level = 1

统计游戏分数(最高分),难度等级,游戏是否开始

(5)user.py (用户对象)

import pygame
from pygame.sprite import Sprite


class User(Sprite):

    def __init__(self,m_settings,screen):
        super().__init__()
        self.screen = screen
        self.m_settings = m_settings

        self.image = pygame.image.load('img/player.png')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        self.center = float(self.rect.centerx)
        self.bottomx = float(self.rect.bottom)

        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False

    def update(self):
        if self.moving_right and self.rect.centerx < self.screen_rect.right:
            self.center += self.m_settings.player_speed_factor
        if self.moving_left and self.rect.centerx > 0:
            self.center -= self.m_settings.player_speed_factor
        if self.moving_up and self.rect.top > 50:
            self.bottomx -= self.m_settings.player_speed_factor
        if self.moving_down and self.rect.bottom < self.screen_rect.bottom:
            self.bottomx += self.m_settings.player_speed_factor

        self.rect.centerx = self.center
        self.rect.bottom = self.bottomx

    def blitme(self):
        self.screen.blit(self.image, self.rect)

    def center_player(self):
        self.center = self.screen_rect.centerx
        self.bottomx = self.screen_rect.bottom

(6)enemy.py

import pygame
from pygame.sprite import Sprite


class Enemy(Sprite):

    def __init__(self, m_settings, screen):
        super().__init__()
        self.screen = screen
        self.m_settings = m_settings

        self.image = pygame.image.load('img/em.png')
        self.rect = self.image.get_rect()

        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        self.x = float(self.rect.x)

    def blitme(self):
        self.screen.blit(self.image, self.rect)

    def check_edges(self):
        screen_rect = self.screen.get_rect()
        if self.rect.right >= screen_rect.right:
            return True
        elif self.rect.left <= 0:
            return True

    def update(self):
        self.x += (self.m_settings.enemy_speed_factor * self.m_settings.fleet_direction)
        self.rect.x = self.x

(7)bullet.py

import pygame
from random import randint
from pygame.sprite import Sprite


class Bullet(Sprite):

    def __init__(self,m_settings,screen,player):
        super(Bullet, self).__init__()
        self.screen = screen

        self.rect = pygame.Rect(0,0,m_settings.bullet_width,m_settings.bullet_height)
        self.rect.centerx = player.rect.centerx
        self.rect.top = player.rect.top

        self.y = float(self.rect.y)

#        self.color = m_settings.bullet_color
        self.color = randint(0,255),randint(0,255),randint(0,255)
        self.speed_factor = m_settings.bullet_speed_factor

    def update(self):
        self.y -= self.speed_factor
        self.rect.y = self.y

    def draw_bullet(self):
        pygame.draw.rect(self.screen,self.color,self.rect)

(8)button.py

import pygame.font


class Button:
    def __init__(self,m_settings,screen,msg):
        print("@")
        self.screen = screen
        self.screen_rect = screen.get_rect()
        print("@")
        self.width, self.height = 200, 50
        self.button_color = (100,200,250)
        self.text_color = (255,255,255)
        print("@")
        self.font = pygame.font.SysFont("arial",36)
        print("@")
        self.rect = pygame.Rect(0,0,self.width,self.height)
        self.rect.center = self.screen_rect.center
        print("!")
        self.prep_msg(msg)


    def prep_msg(self,msg):
        self.msg_image = self.font.render(msg,True,self.text_color,self.button_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)

屏幕的背景和文字

(9)screenboard.py

import pygame.font
from pygame.sprite import Group

from user import User


class Scoreboard:
    def __init__(self,m_settings,screen,stats):
        self.screen = screen
        self.screen_rect = screen.get_rect()
        self.m_settings = m_settings
        self.stats = stats

        self.text_color = (30,30,30)
        self.font = pygame.font.SysFont("arial",36)

        self.prep_score()
        self.prep_high_score()
        self.prep_level()
        self.prep_players()
        self.prep_bullet_allowed()

    def show_score(self):
        self.screen.blit(self.score_image,self.score_rect)
        self.screen.blit(self.high_score_image,self.high_score_rect)
        self.screen.blit(self.level_image, self.level_rect)
        self.screen.blit(self.bullet_max_image,self.bullet_max_rect)
        self.players.draw(self.screen)

    def prep_score(self):
        rounded_score = int(round(self.stats.score, -1))
        score_str = "{:,}".format(rounded_score)
        self.score_image = self.font.render(score_str, True, self.text_color, self.m_settings.bg_color)

        self.score_rect = self.score_image.get_rect()
        self.score_rect.right = self.screen_rect.right - 20
        self.score_rect.top = 10

    def prep_high_score(self):
        high_score = int(round(self.stats.high_score,-1))
        high_score_str = "{:,}".format(high_score)
        self.high_score_image = self.font.render(high_score_str, True, self.text_color, self.m_settings.bg_color)

        self.high_score_rect = self.high_score_image.get_rect()
        self.high_score_rect.centerx = self.screen_rect.centerx
        self.high_score_rect.top = self.score_rect.top

    def prep_level(self):
        self.level_image = self.font.render('level: '+str(self.stats.level), True, self.text_color, self.m_settings.bg_color)

        self.level_rect = self.level_image.get_rect()
        self.level_rect.right = self.score_rect.right
        self.level_rect.top = self.score_rect.bottom + 5

    def prep_bullet_allowed(self):
        bullet_max = self.m_settings.bullets_allowed
        bullet_max_str = "Max Bullet: "+str(bullet_max)
        self.bullet_max_image = self.font.render(bullet_max_str, True, self.text_color, self.m_settings.bg_color)

        self.bullet_max_rect = self.bullet_max_image.get_rect()
        self.bullet_max_rect.right = self.score_rect.right
        self.bullet_max_rect.top = self.level_rect.bottom + 5

    def prep_players(self):
        self.players = Group()
        for player_number in range(self.stats.players_left):
            player = User(self.m_settings,self.screen)
            player.rect.x = 10 + player_number * player.rect.width
            player.rect.y = 10
            self.players.add(player)

3. 实验过程中遇到的问题和解决过程

  • 问题1:代码杂乱
  • 问题1解决方案:重构函数,将相似的方法放在一起

其他(感悟、思考等)

python的模块种类繁多,给我带来了许多可以拓展的空间。而且编写游戏的过程中,虽然不用考虑繁杂的堆栈存储,但是在游戏运行的逻辑上还存在

很多困惑,网上的某些参考往往也不是很靠谱

 

posted @ 2021-06-21 12:40  20201320石志涛  阅读(44)  评论(0编辑  收藏  举报