【前情提要】
1、pygame的安装:https://blog.csdn.net/Xiaomo_haa/article/details/83962300
2、Python小游戏——王校长的S8冠军奖杯【引子】:https://blog.csdn.net/Xiaomo_haa/article/details/83929275
3、Python小游戏——王校长的S8冠军奖杯【上】:https://blog.csdn.net/Xiaomo_haa/article/details/83963110
【CODE】
hotdog.py
在游戏中导入hotdog图像,并且设置hotdog属性以及创建绘制和更新函数。
import pygame
from pygame.sprite import Sprite
class Hotdog(Sprite):
""""表示单个hotdog的类"""
def __init__(self,ai_settings,screen,wxz):
"""初始化hotdog并设置其初始位置"""
super(Hotdog, self).__init__()
self.screen = screen
#加载hotdog图像,并设置其rect属性
self.image = pygame.image.load('image/hotdog.png')
self.rect = self.image.get_rect()
#让每个hotdog在WXZ所处位置
self.rect.centerx = wxz.rect.centerx
self.rect.top = wxz.rect.top
#存储hotdog的准确位置
self.y = float(self.rect.y)
self.speed_factor = ai_settings.hotdog_speed_factor
#开火标志
self.hotdog_fire = False
def blitme(self):
"""在指定位置绘制hotdog"""
self.screen.blit(self.image,self.rect)
def update(self):
"""向上移动hotdog"""
#更新表示hotdog位置的小数值
self.y -= self.speed_factor
#更新表示hotdog的rect的位置
self.rect.y = self.y
button.py
创建一个开始按钮,并且只有用鼠标点击play按钮之后游戏才能开始。
import pygame.font
class Button():
def __init__(self,ai_settings,screen,msg):
"""初始化按钮的属性"""
self.screen = screen
self.screen_rect = screen.get_rect()
#设置按钮的尺寸和其他属性
self.width,self.height = 200,50
self.button_color = (0,255,0)
self.text_color = (255,255,255)
self.font = pygame.font.SysFont(None,48)
#创建按钮的rect对象,并使其居中
self.rect = pygame.Rect(0,0,self.width,self.height)
self.rect.center = self.screen_rect.center
#按钮的标签只需创建一次
self.prep_msg(msg)
def prep_msg(self,msg):
"""将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)
game_stata.py
一个统计游戏分数的类与初始化游戏期间变化量。
class GameStats():
"""跟踪游戏的统计信息"""
def __init__(self,ai_settings):
"""初始化统计信息"""
self.ai_settings = ai_settings
self.reset_stats()
#游戏刚启动时处于非活动状态
self.game_active = False
#在任何情况下都不应重置最高分
self.high_score = 0
def reset_stats(self):
"""初始化在游戏运行期间可能变化的统计信息"""
self.wxzs_left = self.ai_settings.wxz_limit
self.score = 0
self.level = 1
scoreboard.py
在屏幕顶部创建计分板,显示游戏得分、最高分、剩余生命等。
import pygame.font
from pygame.sprite import Group
from wxz import Wxz
class Scoreboard():
"""显示得分信息的类"""
def __init__(self,ai_settings,screen,stats):
"""初始化显示得分涉及的属性"""
self.screen = screen
self.screen_rect = screen.get_rect()
self.ai_settihngs = ai_settings
self.stats = stats
#显示得分信息时使用的字体设置
self.text_color = (30,30,30)
self.font = pygame.font.SysFont(None,48)
#准备初始得分图像
self.prep_score()
self.prep_high_score()
self.prep_level()
self.prep_wxzs()
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.ai_settihngs.bg_color)
#将得分放在屏幕右上角
self.score_rect = self.score_image.get_rect()
self.score_rect.right = self.screen_rect.right - 20
self.score_rect.top = 20
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.ai_settihngs.bg_color)
#将最高分放在屏幕顶部中央
self.high_score_ract = self.high_score_image.get_rect()
self.high_score_ract.centerx = self.screen_rect.centerx
self.high_score_ract.top = self.score_rect.top
def show_score(self):
"""在屏幕上显示奖杯和得分"""
self.screen.blit(self.score_image,self.score_rect)
self.screen.blit(self.high_score_image,self.high_score_ract)
self.screen.blit(self.level_image,self.level_rect)
#绘制奖杯
self.wxzs.draw(self.screen)
def prep_level(self):
"""将等级转换为渲染的图像"""
self.level_image = self.font.render(str(self.stats.level),True,self.text_color,self.ai_settihngs.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 + 10
def prep_wxzs(self):
"""显示还余下多少个奖杯"""
self.wxzs = Group()
for wxz_number in range(self.stats.wxzs_left):
wxz = Wxz(self.ai_settihngs,self.screen)
wxz.rect.x = 10 + wxz_number * wxz.rect.width
wxz.rect.y = 10
self.wxzs.add(wxz)
game_functions.py
游戏中所有涉及的处理函数。
import sys
import pygame
from hotdog import Hotdog
from cup import Cup
from time import sleep
def check_keydowm_events(event,ai_settings,screen,wxz,hotdogs):
"""响应按键"""
if event.key == pygame.K_RIGHT:
wxz.moving_right = True
elif event.key == pygame.K_LEFT:
wxz.moving_left = True
elif event.key == pygame.K_a:
fire_hotdog(ai_settings, screen, wxz, hotdogs)
elif event.key == pygame.K_ESCAPE:
sys.exit()
def check_keyup_events(event,wxz):
"""响应松开"""
if event.key == pygame.K_RIGHT:
wxz.moving_right = False
elif event.key == pygame.K_LEFT:
wxz.moving_left = False
def check_events(ai_settings,screen,stats,sb,play_button,wxz,cups,hotdogs):
"""响应按键和鼠标事件"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydowm_events(event,ai_settings,screen,wxz,hotdogs)
elif event.type == pygame.KEYUP:
check_keyup_events(event,wxz)
elif event.type == pygame.MOUSEBUTTONDOWN:
mouse_x,mouse_y = pygame.mouse.get_pos()
check_play_button(ai_settings,screen,stats,sb,play_button,wxz,cups,hotdogs,mouse_x,mouse_y)
def check_play_button(ai_settings,screen,stats,sb,play_button,wxz,cups,hotdogs,mouse_x,mouse_y):
"""在玩家单击Play按钮时开始新游戏"""
button_clicked = play_button.rect.collidepoint(mouse_x,mouse_y)
if button_clicked and not stats.game_active:
#重置游戏速度
ai_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_wxzs()
#清空奖杯列表和hotdog列表
cups.empty()
hotdogs.empty()
#创建一群新的奖杯,并让WXZ居中
create_fleet(ai_settings,screen,wxz,cups)
wxz.center_wxz()
def update_screen(ai_settings,screen,stats,sb,wxz,cups,hotdogs,play_button):
"""更新屏幕上的图像,并切换到新屏幕"""
#每次循环时都重绘屏幕
screen.fill(ai_settings.bg_color)
#在WXZ和外星人后面重绘所有hotdog
for hotdog in hotdogs.sprites():
hotdog.blitme()
wxz.blitme()
cups.draw(screen)
#显示得分
sb.show_score()
#如果游戏处于非活动状态,就绘制Play按钮
if not stats.game_active:
play_button.draw_button()
#让最近绘制的屏幕可见
pygame.display.flip()
def update_hotdogs(ai_settings,screen,stats,sb,wxz,cups,hotdogs):
"""更新hotdog的位置,并删除已消失的hotdog"""
#更新hotdog的位置
hotdogs.update()
# 删除已消失的hotdog
for hotdog in hotdogs.copy():
if hotdog.rect.bottom <= 0:
hotdogs.remove(hotdog)
check_hotdog_cup_collision(ai_settings,screen,stats,sb,wxz,cups,hotdogs)
def check_hotdog_cup_collision(ai_settings,screen,stats,sb,wxz,cups,hotdogs):
"""相应子hotdog和奖杯的碰撞"""
#删除发生碰撞的hotdog和奖杯
collisions = pygame.sprite.groupcollide(hotdogs,cups,True,True)
if collisions:
for cups in collisions.values():
stats.score += ai_settings.cup_points * len(cups)
sb.prep_score()
check_high_score(stats,sb)
if len(cups) == 0:
#整群奖杯被消灭,提高一个等级
#删除现有的hotdog并新建一群奖杯,并加快游戏节奏
hotdogs.empty()
ai_settings.increase_speed()
#提高等级
stats.level += 1
sb.prep_level()
create_fleet(ai_settings,screen,wxz,cups)
def check_high_score(stats,sb):
"""检查是否诞生了最高分"""
if stats.score > stats.high_score:
stats.high_score = stats.score
sb.prep_high_score()
def fire_hotdog(ai_settings,screen,wxz,hotdogs):
"""如果还没达到极限,就发射一颗hotdog"""
if len(hotdogs) < ai_settings.hotdogs_allowed:
new_hotdog = Hotdog(ai_settings, screen, wxz)
hotdogs.add(new_hotdog)
def get_number_cups_x(ai_settings,cup_width):
"""计算每行可容纳多少个奖杯"""
available_space_x = ai_settings.screen_width - 2 * cup_width
number_cups_x = int(available_space_x / (2 * cup_width))
return number_cups_x
def get_number_rows(ai_settings,wxz_height,cup_height):
"""计算屏幕可容纳多少行奖杯"""
available_space_y = (ai_settings.screen_height - (3 * cup_height) - wxz_height)
number_rows = int(available_space_y / (2 * cup_height))
return number_rows
def create_cup(ai_settings,screen,cups,cup_number,row_number):
"""创建一个奖杯并将其放在当前行"""
cup = Cup(ai_settings, screen)
cup_width = cup.rect.width
cup.x = cup_width + 2 * cup_width * cup_number
cup.rect.x = cup.x
cup.rect.y = cup.rect.height + 2 * cup.rect.height * row_number + 66
cups.add(cup)
def create_fleet(ai_settings,screen,wxz,cups):
"""创建奖杯群"""
#创建一个奖杯,并计算一行可以容纳多少奖杯
#奖杯间距为奖杯宽度
cup = Cup(ai_settings,screen)
number_cups_x = get_number_cups_x(ai_settings,cup.rect.width)
number_rows = get_number_rows(ai_settings,wxz.rect.height,cup.rect.height) - 2
#创建第一行奖杯
for row_number in range(number_rows):
for cup_number in range(number_cups_x):
create_cup(ai_settings,screen,cups,cup_number,row_number)
def check_fleet_edges(ai_settings,cups):
"""有奖杯到达边缘时采取相应的措施"""
for cup in cups.sprites():
if cup.check_edges():
change_fleet_direction(ai_settings,cups)
break
def change_fleet_direction(ai_settings,cups):
"""将整群奖杯下移,并改变它们方向"""
for cup in cups.sprites():
cup.rect.y += ai_settings.fleet_drop_speed
ai_settings.fleet_direction *= -1
def wxz_hit(ai_settings,screen,stats,sb,wxz,cups,hotdogs):
"""响应被奖杯撞到的WXZ"""
if stats.wxzs_left > 0:
#将wxz_left -= 1
stats.wxzs_left -= 1
#更新记分牌
sb.prep_wxzs()
#清空奖杯列表和hotdog列表
cups.empty()
hotdogs.empty()
#创建一群新的奖杯,并将WXZ放到屏幕底部中央
create_fleet(ai_settings,screen,wxz,cups)
wxz.center_wxz
#暂停
sleep(0.5)
else:
stats.game_active = False
pygame.mouse.set_visible(True)
def check_cups_bottom(ai_settings,screen,stats,sb,wxz,cups,hotdogs):
"""检查是否有奖杯到达屏幕底端"""
screen_rect = screen.get_rect()
for cup in cups.sprites():
if cup.rect.bottom >= screen_rect.bottom:
#像WXZ撞到一样进行处理
wxz_hit(ai_settings,screen,stats,sb,wxz,cups,hotdogs)
break
def update_cups(ai_settings,screen,stats,sb,wxz,cups,hotdogs):
"""检查是否有奖杯位于屏幕边缘,并更新整群奖杯位置"""
check_fleet_edges(ai_settings,cups)
cups.update()
#检测奖杯的WXZ之间的碰撞
if pygame.sprite.spritecollideany(wxz,cups):
wxz_hit(ai_settings,screen,stats,sb,wxz,cups,hotdogs)
#检查是否有奖杯到达屏幕底端
check_cups_bottom(ai_settings,screen,stats,sb,wxz,cups,hotdogs)
【游戏界面】
至此,游戏全部完成,但是很多功能不够完善,比如自动发射hotdog等等,比如像雷电一样可以自由移动等等......
等待后续更新吧。
附上游戏运行图:
游戏全部代码和资源链接:https://download.csdn.net/download/xiaomo_haa/10780712