用Python编写小游戏(含代码)——吃大便——实训代码

经常听到有朋友说,学习编程是一件非常枯燥无味的事情。其实,大家有没有认真想过,可能是我们的学习方法不对?

比方说,你有没有想过,可以通过打游戏来学编程?

1、游戏展示

模式1 

限时模式

模式2

无尽模式

2、代码讲解

 如何根据用户点击来做出界面选择?

#开始界面的检测
n1 = True
n2 = True
clock = pygame.time.Clock()#决定游戏画面的帧率
#游戏开始前的选择界面
def mian(n1):
    global choice
    while n1:
        clock.tick(30)
        buttons = pygame.mouse.get_pressed()#得到鼠标点击
        x1,y1 = pygame.mouse.get_pos()#鼠标的坐标
        if x1 >= enter_x and x1 <= s1.get_width() + enter_x and y1 >= enter_y and y1 <= s1.get_height() + enter_y:#制定一个区域来判断点击
            start_ck.blit(s1, (enter_x, enter_y))
            if buttons[0]:
                n1 = False
                choice = 1#判断用户点击

        if x1 >= exit_x and x1 <= s2.get_width() + exit_x and y1 >= exit_y and y1 <= s2.get_height() + exit_y:
            start_ck.blit(s2, (exit_x, exit_y))
            if buttons[0]:
                pygame.quit()
                exit()


        if x1 >= choice_x and x1 <= s5.get_width() + choice_x and y1 >= choice_y and y1 <= s5.get_height() + choice_y:
            start_ck.blit(s5, (choice_x, choice_y))
            if buttons[0]:
                n1 = False
                choice = 2
        else:
            start_ck.blit(s1,(enter_x, enter_y))
            start_ck.blit(s2,(exit_x, exit_y))
            start_ck.blit(s5, (choice_x, choice_y))

        window.blit(start_ck,(0,0))
        pygame.display.update()
        #监听事件
        for event in pygame.event.get():

            # 判断事件类型是否是退出事件
            if event.type == pygame.QUIT:
                print("游戏退出...")

                # quit 卸载所有的模块
                pygame.quit()

                # exit() 直接终止当前正在执行的程序
                exit()

    window.blit(start_ck2,(0,0))
    pygame.display.update()
    return n1

如何判断角色与炸弹接触? 

这里我们将与人物接触的物品的坐标移动到画面以外就可以了

#人物与炸弹接触
if choice == 1:
    player_rect = pygame.Rect(player_x, player_y, player_image.get_width(),player_image.get_height())  # (左上角x,左上角y,矩形宽,矩形宽)
    bomb_rect = pygame.Rect(bomb_x, bomb_y, bomb_image.get_width(), bomb_image.get_height())
    if player_rect.colliderect(bomb_rect):  # 检测碰撞返回是否碰撞:True or Flase
        score -= 5 # 碰撞则表示扣分
        s += bomb()
        bomb_y = -100  # y坐标固定在所见见面外
        bomb_x = random.randint(0, window_width - bomb_image.get_width())  # 从所见范围内生成一个随机的x坐标

    player_rect = pygame.Rect(player_x, player_y, player_image.get_width(),
                              player_image.get_height())  # (左上角x,左上角y,矩形宽,矩形宽)
    bomb1_rect = pygame.Rect(bomb1_x, bomb1_y, bomb1_image.get_width(), bomb1_image.get_height())
    if player_rect.colliderect(bomb1_rect):  # 检测碰撞返回是否碰撞:True or Flase
        s += bomb1()
        bomb1_x = random.randint(0, window_width - bomb1_image.get_width())  # 从所见范围内生成一个随机的x坐标
        bomb1_y = random.uniform(-100,-500) # y坐标固定在所见见面外
        score -= 5  # 碰撞则表示扣分

if choice == 2:
    player_rect = pygame.Rect(player_x, player_y, player_image.get_width(),player_image.get_height())  # (左上角x,左上角y,矩形宽,矩形宽)
    bomb_rect = pygame.Rect(bomb_x, bomb_y, bomb_image.get_width(), bomb_image.get_height())
    if player_rect.colliderect(bomb_rect):  # 检测碰撞返回是否碰撞:True or Flase
        s += bomb()
        bomb_x = random.randint(0, window_width - bomb_image.get_width())  # 从所见范围内生成一个随机的x坐标
        bomb_y = -100  # y坐标固定在所见见面外
        hp = hp - 1  # 碰撞则表示扣血

    player_rect = pygame.Rect(player_x, player_y, player_image.get_width(),
                              player_image.get_height())  # (左上角x,左上角y,矩形宽,矩形宽)
    bomb1_rect = pygame.Rect(bomb1_x, bomb1_y, bomb1_image.get_width(), bomb1_image.get_height())
    if player_rect.colliderect(bomb1_rect):  # 检测碰撞返回是否碰撞:True or Flase
        s += bomb1()
        bomb1_x = random.randint(0, window_width - bomb1_image.get_width())  # 从所见范围内生成一个随机的x坐标
        bomb1_y = random.uniform(-100,-500)  # y坐标固定在所见见面外
        hp = hp - 1  # 碰撞则表示扣分

# 绘制游戏场景
built(choice)

 炸弹的爆炸如何实现?

#炸弹爆炸特效
def bomb():
    f = time.time()
    for i in explode_image:#此处我们将多张照片存入列表中,一次将其展示在界面中
        window.blit(player2_image,(player_x,player_y))
        window.blit(i, ((player_x + bomb_x) / 2, (player_y + bomb_y) / 2))#图面显示的坐标
        pygame.display.update()
        time.sleep(0.03)#让其播放更加合理
    return time.time() - f#将爆炸暂停的时间记下来,当爆炸结束后重新补给倒计时中

def bomb1():
    f = time.time()
    for i in explode_image:
        window.blit(player2_image, (player_x, player_y))
        window.blit(i, ((player_x + bomb1_x) / 2, (player_y + bomb1_y) / 2))
        pygame.display.update()
        time.sleep(0.03)
    return time.time() - f

 废话不多说直接上代码!!!!

 3、游戏代码

import pygame
import random
from pygame import *
import time

#倒计时长
n = 15

#血量
m = 3

#创建人物移动速度
speed = 5

#无尽模式下的人物血量
hp = m

# 初始化分数
score = 0

# 初始化pygame
pygame.init()
pygame.mixer.init()



# 设置游戏窗口尺寸
window_width = 800
window_height = 600
window = pygame.display.set_mode((800,600))
pygame.display.set_caption("吃大便")
start_ck = pygame.Surface(window.get_size())  #充当主菜单的画布
start_ck2 = pygame.Surface(window.get_size())  #充当主菜单的画布
start_ck = start_ck.convert()
start_ck2 = start_ck2.convert()
start_ck.fill((255,255,255))  #填充白色

# 加载背景、金币和人物图片
#分别将图片传给常数

s1 = pygame.image.load(".\素材\开始1.png") #开始
s1.convert()
s2 = pygame.image.load(".\素材\退出.png") #退出
s2.convert()
s3 = pygame.image.load(".\素材\重新游戏.png").convert_alpha()#重新游戏
s4 = pygame.image.load(".\素材\退出游戏.png").convert_alpha()#退出游戏
s5 = pygame.image.load(".\素材\无尽模式.png").convert_alpha() #无尽模式
s6 = pygame.image.load(".\素材\切换模式.png").convert_alpha()#切换模式



background_image = pygame.image.load(".\素材\吃粑粑.png").convert()
coin_image = pygame.image.load(".\素材\未标题-133.png",".\素材\未标题-133.png").convert_alpha()
player_image = pygame.image.load(".\素材\脸.png").convert_alpha()
player2_image = pygame.image.load(".\素材\死亡.png").convert_alpha()
player3_image = pygame.image.load(".\素材\狗头.png").convert_alpha()
bomb_image = pygame.image.load(r".\素材\bomb.png").convert_alpha()
bomb1_image = pygame.image.load(r".\素材\bomb.png").convert_alpha()
coin1_image = pygame.image.load(".\素材\未标题-133.png",".\素材\未标题-133.png").convert_alpha()
coin2_image = pygame.image.load(".\素材\未标题-133.png",".\素材\未标题-133.png").convert_alpha()
coin3_image = pygame.image.load(".\素材\未标题-133.png",".\素材\未标题-133.png").convert_alpha()
hp_image = pygame.image.load(".\素材\血量.png").convert_alpha()
explode_image = [pygame.image.load
                 (r".\素材\b" + str(v) + ".png") for v in range(1, 23)]

# 设置背景图片尺寸与游戏窗口一致
background = pygame.transform.scale(background_image, (window_width, window_height))


# 设置金币和人物和炸弹的初始位置
player_x = window_width // 2 - 30
player_y = window_height - player_image.get_height() - 30

enter_x = window_width // 2 - 200
enter_y = window_height// 2 - s1.get_height() // 2 - 200

exit_x = window_width // 2 - 200
exit_y = window_height// 2 - s2.get_height() // 2 + 200

choice_x = window_width // 2 - 200
choice_y = window_width // 2 - s1.get_height() // 2 - 100

hp_x = 500
hp_y = 10


coin_x = random.randint(0, window_width - coin_image.get_width())
coin_y = random.uniform(-100,-500)

bomb_x = random.randint(0, window_width - bomb_image.get_width())
bomb_y = -300

bomb1_x = random.randint(0, window_width - bomb_image.get_width())
bomb1_y = random.uniform(-100,-500)


coin1_x = random.randint(0, window_width - coin1_image.get_width())
coin1_y = random.uniform(-100,-500)

coin2_x = random.randint(0, window_width - coin2_image.get_width())
coin2_y = random.uniform(-100,-500)

coin3_x = random.randint(0, window_width - coin3_image.get_width())
coin3_y = random.uniform(-100,-500)

#金币的速度
coin_speed = random.uniform(3,6)
bomb_speed = random.uniform(3,5)
bomb1_speed = random.uniform(3,6)
coin1_speed = random.uniform(3,6)
coin2_speed = random.uniform(3,6)
coin3_speed = random.uniform(3,6)

# 创建字体对象
font = pygame.font.SysFont(None, 36)
font1 = pygame.font.SysFont(None, 100)
endscore = pygame.font.SysFont(None, 100)
stopfont = pygame.font.Font('C:\Windows\Fonts\simhei.ttf',48)
hp_font = pygame.font.Font(None,36)
live_font = pygame.font.Font("C:\Windows\Fonts\MISTRAL.ttf",36)
countdown_font = pygame.font.Font("C:\Windows\Fonts\MISTRAL.ttf",120)
load_time_font = pygame.font.Font("C:\Windows\Fonts\OCRAEXT.ttf",150)


#选择模式
choice = 0

#开始界面的检测
n1 = True
n2 = True
clock = pygame.time.Clock()#决定帧率

#绘制游戏场景
def built(choice):
    window.blit(background, (0, 0))  # 绘制背景
    window.blit(player_image, (player_x, player_y))
    window.blit(coin_image, (coin_x, coin_y))
    window.blit(bomb_image, (bomb_x, bomb_y))
    window.blit(bomb1_image, (bomb1_x, bomb1_y))
    window.blit(coin1_image, (coin1_x, coin1_y))
    window.blit(coin2_image, (coin2_x, coin2_y))
    window.blit(coin3_image, (coin3_x, coin3_y))
    if choice == 2:
        window.blit(hp_image, (hp_x, hp_y))

#游戏开始前的选择界面
def mian(n1):
    global choice
    while n1:
        clock.tick(30)
        buttons = pygame.mouse.get_pressed()#得到鼠标点击
        x1,y1 = pygame.mouse.get_pos()#鼠标的坐标
        if x1 >= enter_x and x1 <= s1.get_width() + enter_x and y1 >= enter_y and y1 <= s1.get_height() + enter_y:
            start_ck.blit(s1, (enter_x, enter_y))
            if buttons[0]:
                n1 = False
                choice = 1

        if x1 >= exit_x and x1 <= s2.get_width() + exit_x and y1 >= exit_y and y1 <= s2.get_height() + exit_y:
            start_ck.blit(s2, (exit_x, exit_y))
            if buttons[0]:
                pygame.quit()
                exit()


        if x1 >= choice_x and x1 <= s5.get_width() + choice_x and y1 >= choice_y and y1 <= s5.get_height() + choice_y:
            start_ck.blit(s5, (choice_x, choice_y))
            if buttons[0]:
                n1 = False
                choice = 2
        else:
            start_ck.blit(s1,(enter_x, enter_y))
            start_ck.blit(s2,(exit_x, exit_y))
            start_ck.blit(s5, (choice_x, choice_y))

        window.blit(start_ck,(0,0))
        pygame.display.update()
        #监听事件
        for event in pygame.event.get():

            # 判断事件类型是否是退出事件
            if event.type == pygame.QUIT:
                print("游戏退出...")

                # quit 卸载所有的模块
                pygame.quit()

                # exit() 直接终止当前正在执行的程序
                exit()

    window.blit(start_ck2,(0,0))
    pygame.display.update()
    return n1

#炸弹爆炸特效
def bomb():
    f = time.time()
    for i in explode_image:
        window.blit(player2_image,(player_x,player_y))
        window.blit(i, ((player_x + bomb_x) / 2, (player_y + bomb_y) / 2))
        pygame.display.update()
        time.sleep(0.03)
    return time.time() - f

def bomb1():
    f = time.time()
    for i in explode_image:
        window.blit(player2_image, (player_x, player_y))
        window.blit(i, ((player_x + bomb1_x) / 2, (player_y + bomb1_y) / 2))
        pygame.display.update()
        time.sleep(0.03)
    return time.time() - f

#执行主页面
n1 = mian(n1)

#预备游戏状态
def load_time():
    load_time = int(time.time())
    while int(time.time()) - load_time != 3:
        built(n1)
        load_time_text = load_time_font.render(str(3 - int(time.time() - load_time)),True,(0, 0, 0))
        window.blit(load_time_text,(350,200))
        pygame.display.update()

load_time()

pygame.mouse.set_visible(False)#隐藏鼠标

#游戏计时
stratime = round(time.time(),2)
t = endtime = stratime

#加载音乐
pygame.mixer.music.load(".\素材\活力四射 青春力量 by Alexguz-完整版.mp3")
pygame.mixer.music.set_volume(0.5)
pygame.mixer.music.play(-1)

#游戏的初始化数据
running = True #断关闭页面
paused = False #判断暂停
s = 0#初始累计暂停时间
clock = pygame.time.Clock()

#游戏主循环
while running:
    pygame.mouse.set_visible(False)
    if paused == False:
        stratsleep = endsleep = round(time.time(),2)#默认暂停时间为0
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:#如果接收到空格键并且当前执行
                if paused == False:
                    window.blit(player3_image, (player_x - 5, player_y - 10))
                    pygame.mixer.music.pause()
                    stop_text = stopfont.render("按空格继续游戏", True, (0, 0, 0))
                    window.blit(stop_text, (240, 250))
                    stratsleep = round(time.time(),2)
                    pygame.display.update()

                if paused == True:
                    pygame.mixer.music.unpause()
                    endsleep = round(time.time(),2)
                    s = s+(endsleep-stratsleep)#累计暂停时间
                    window.blit(player_image, (player_x, player_y))
                    pygame.display.update()

                paused = not paused

    if not paused: #paused 为 False 执行
        # print(s)#补偿时间

        # 监听键盘事件
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] or keys[pygame.K_a]:
            if player_x >= 0:
                player_x -= speed

        if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
            if player_x <= 740:
                player_x += speed

        # 更新金币位置
        coin_y += coin_speed
        if coin_y > window_height:
            #如果超出界面的大小就将它移除
            coin_x = random.randint(0, window_width - coin_image.get_width())
            coin_y = random.uniform(-100,-500)#大便的初始位置

        coin1_y += coin1_speed
        if coin1_y > window_height:
            # 如果超出界面的大小就将它移除
            coin1_x = random.randint(0, window_width - coin1_image.get_width())
            coin1_y = random.uniform(-100, -500)

        coin2_y += coin2_speed
        if coin2_y > window_height:
            # 如果超出界面的大小就将它移除
            coin2_x = random.randint(0, window_width - coin2_image.get_width())
            coin2_y = random.uniform(-100, -500)

        coin3_y += coin3_speed
        if coin3_y > window_height:
            # 如果超出界面的大小就将它移除
            coin3_x = random.randint(0, window_width - coin3_image.get_width())
            coin3_y = random.uniform(-100, -500)

        # 更新炸弹位置
        bomb_y += bomb_speed
        if bomb_y > window_height:
            # 如果超出界面的大小就将它移除
            bomb_x = random.randint(0, window_width - bomb_image.get_width())
            bomb_y = random.uniform(-100,-500)

        bomb1_y += bomb1_speed
        if bomb1_y > window_height:
            # 如果超出界面的大小就将它移除
            bomb1_x = random.randint(0, window_width - bomb_image.get_width())
            bomb1_y = random.uniform(-100, -500)

        # 检测碰撞
        # 金币与人物
        #pygame.Rect用来创建矩形对象
        player_rect = pygame.Rect(player_x, player_y, player_image.get_width(), player_image.get_height())#(左上角x,左上角y,矩形宽,矩形宽)
        coin_rect = pygame.Rect(coin_x, coin_y, coin_image.get_width(), coin_image.get_height())
        if player_rect.colliderect(coin_rect):#检测碰撞返回是否碰撞:True or Flase
            coin_x = random.randint(0, window_width - coin_image.get_width())#从所见范围内生成一个随机的x坐标
            coin_y = random.uniform(-100,-500)#y坐标固定在所见见面外
            score += 1#碰撞则表示得分,并且碰撞时生成一个新的金币

        player_rect = pygame.Rect(player_x, player_y, player_image.get_width(),player_image.get_height())  # (左上角x,左上角y,矩形宽,矩形宽)
        coin1_rect = pygame.Rect(coin1_x, coin1_y, coin1_image.get_width(), coin1_image.get_height())
        if player_rect.colliderect(coin1_rect):  # 检测碰撞返回是否碰撞:True or Flase
            coin1_x = random.randint(0, window_width - coin1_image.get_width())  # 从所见范围内生成一个随机的x坐标
            coin1_y = random.uniform(-100, -500)  # y坐标固定在所见见面外
            score += 1  # 碰撞则表示得分,并且碰撞时生成一个新的金币

        player_rect = pygame.Rect(player_x, player_y, player_image.get_width(),
                                  player_image.get_height())  # (左上角x,左上角y,矩形宽,矩形宽)
        coin2_rect = pygame.Rect(coin2_x, coin2_y, coin2_image.get_width(), coin2_image.get_height())
        if player_rect.colliderect(coin2_rect):  # 检测碰撞返回是否碰撞:True or Flase
            coin2_x = random.randint(0, window_width - coin2_image.get_width())  # 从所见范围内生成一个随机的x坐标
            coin2_y = random.uniform(-100, -500)  # y坐标固定在所见见面外
            score += 1  # 碰撞则表示得分,并且碰撞时生成一个新的金币

        player_rect = pygame.Rect(player_x, player_y, player_image.get_width(),
                                  player_image.get_height())  # (左上角x,左上角y,矩形宽,矩形宽)
        coin3_rect = pygame.Rect(coin3_x, coin3_y, coin3_image.get_width(), coin3_image.get_height())
        if player_rect.colliderect(coin3_rect):  # 检测碰撞返回是否碰撞:True or Flase
            coin3_x = random.randint(0, window_width - coin3_image.get_width())  # 从所见范围内生成一个随机的x坐标
            coin3_y = random.uniform(-100, -500)  # y坐标固定在所见见面外
            score += 1  # 碰撞则表示得分,并且碰撞时生成一个新的金币

        #人物与炸弹接触
        if choice == 1:
            player_rect = pygame.Rect(player_x, player_y, player_image.get_width(),player_image.get_height())  # (左上角x,左上角y,矩形宽,矩形宽)
            bomb_rect = pygame.Rect(bomb_x, bomb_y, bomb_image.get_width(), bomb_image.get_height())
            if player_rect.colliderect(bomb_rect):  # 检测碰撞返回是否碰撞:True or Flase
                score -= 5 # 碰撞则表示扣分
                s += bomb()
                bomb_y = -100  # y坐标固定在所见见面外
                bomb_x = random.randint(0, window_width - bomb_image.get_width())  # 从所见范围内生成一个随机的x坐标

            player_rect = pygame.Rect(player_x, player_y, player_image.get_width(),
                                      player_image.get_height())  # (左上角x,左上角y,矩形宽,矩形宽)
            bomb1_rect = pygame.Rect(bomb1_x, bomb1_y, bomb1_image.get_width(), bomb1_image.get_height())
            if player_rect.colliderect(bomb1_rect):  # 检测碰撞返回是否碰撞:True or Flase
                s += bomb1()
                bomb1_x = random.randint(0, window_width - bomb1_image.get_width())  # 从所见范围内生成一个随机的x坐标
                bomb1_y = random.uniform(-100,-500) # y坐标固定在所见见面外
                score -= 5  # 碰撞则表示扣分

        if choice == 2:
            player_rect = pygame.Rect(player_x, player_y, player_image.get_width(),player_image.get_height())  # (左上角x,左上角y,矩形宽,矩形宽)
            bomb_rect = pygame.Rect(bomb_x, bomb_y, bomb_image.get_width(), bomb_image.get_height())
            if player_rect.colliderect(bomb_rect):  # 检测碰撞返回是否碰撞:True or Flase
                s += bomb()
                bomb_x = random.randint(0, window_width - bomb_image.get_width())  # 从所见范围内生成一个随机的x坐标
                bomb_y = -100  # y坐标固定在所见见面外
                hp = hp - 1  # 碰撞则表示扣血

            player_rect = pygame.Rect(player_x, player_y, player_image.get_width(),
                                      player_image.get_height())  # (左上角x,左上角y,矩形宽,矩形宽)
            bomb1_rect = pygame.Rect(bomb1_x, bomb1_y, bomb1_image.get_width(), bomb1_image.get_height())
            if player_rect.colliderect(bomb1_rect):  # 检测碰撞返回是否碰撞:True or Flase
                s += bomb1()
                bomb1_x = random.randint(0, window_width - bomb1_image.get_width())  # 从所见范围内生成一个随机的x坐标
                bomb1_y = random.uniform(-100,-500)  # y坐标固定在所见见面外
                hp = hp - 1  # 碰撞则表示扣分

        # 绘制游戏场景
        built(choice)

        # 显示分数
        score_text = font.render("Score: " + str(score), True, (0, 0, 0))#(字符串,是否反锯齿,颜色)
        window.blit(score_text, (10, 10))#显示的位置

        #显示倒计时
        if choice == 1:#倒计时模式
            endtime = round(time.time(),2)
            t = round(endtime - stratime,2) - s
            time_text = font.render("Time: " + str(round((n-t),1)), True, (0, 0, 0))#倒计时显示
            window.blit(time_text, (680, 10))
            if t >= n:
                window.blit(player3_image, (player_x - 5, player_y - 10))
                pygame.mixer.music.stop()#停止音乐
                pygame.mixer.music.load(".\素材\铃声 - 植物大战僵尸失败音效 (铃声).mp3")
                pygame.mixer.music.set_volume(0.5)
                pygame.mixer.music.play()
                end = font1.render("Game Over" , True, (255, 0, 0))
                endscore_text = endscore.render("Score: " + str(score), True, (0, 0, 0))
                window.blit(endscore_text, (250, 370))#结束得分的位置
                window.blit(end,(200, 170))#结束标志的位置
                pygame.display.update()#刷新界面
                time.sleep(2)
                window.blit(s3, (enter_x, enter_y))
                window.blit(s4, (exit_x, exit_y))
                window.blit(s6, (choice_x, choice_y))
                pygame.mouse.set_visible(True)
                while True:
                    clock.tick(30)
                    buttons = pygame.mouse.get_pressed()  # 得到鼠标点击
                    x1, y1 = pygame.mouse.get_pos()  # 鼠标的坐标
                    if x1 >= enter_x and x1 <= s3.get_width() + enter_x and y1 >= enter_y and y1 <= s3.get_height() + enter_y:
                        window.blit(s3, (enter_x, enter_y))
                        if buttons[0]:
                            t = 0
                            s = 0  #累计时间置零
                            score = 0
                            stratime = round(time.time(),2)
                            endtime = stratime
                            coin_y = coin1_y = coin2_y = coin3_y = bomb_y = 1000#重新刷新掉落

                            load_time()
                            break

                    if x1 >= exit_x and x1 <= s2.get_width() + exit_x and y1 >= exit_y and y1 <= s2.get_height() + exit_y:

                        window.blit(s4, (exit_x, exit_y))
                        if buttons[0]:
                            pygame.quit()
                            exit()

                    if x1 >= choice_x and x1 <= s5.get_width() + choice_x and y1 >= choice_y and y1 <= s5.get_height() + choice_y:
                        window.blit(s6, (choice_x, choice_y))
                        if buttons[0]:
                            choice = 2
                            t = 0
                            s = 0  # 累计时间置零
                            score = 0
                            hp = m
                            stratime = round(time.time(), 2)
                            endtime = stratime
                            coin_y = coin1_y = coin2_y = coin3_y = bomb_y = bomb1_y = 1000  # 重新刷新掉落

                            load_time()
                            break

                    pygame.display.update()
                     # 监听事件
                    for event in pygame.event.get():

                        # 判断事件类型是否是退出事件
                        if event.type == pygame.QUIT:
                            print("游戏退出...")

                            # quit 卸载所有的模块
                            pygame.quit()

                            # exit() 直接终止当前正在执行的程序
                            exit()

                    pygame.mixer.music.load(".\素材\活力四射 青春力量 by Alexguz-完整版.mp3")
                    pygame.mixer.music.play(-1)
                    pygame.display.update()

        if choice == 2:#无尽模式
            endtime = round(time.time(), 2)
            sumtime = endtime - stratime - s
            time_text = font.render("Time: " + str(round((sumtime), 1)), True, (0, 0, 0))  # 倒计时显示
            window.blit(time_text, (670, 10))
            hp_text = hp_font.render("Hp:",True,(0, 0, 0))
            window.blit(hp_text, (450, 10))
            live_text = live_font.render("X " + str(hp),True,(0, 0, 0))
            window.blit(live_text, (540, 8))
            if int(sumtime) % 20 == 0 and sumtime >= 20:
                bomb1_speed += 0.01
                bomb_speed += 0.01
                # 每20秒炸弹速度会加快

            if hp == 0:
                window.blit(player3_image,(player_x - 5,player_y - 10))
                pygame.mixer.music.stop()#停止音乐
                pygame.mixer.music.load(".\素材\铃声 - 植物大战僵尸失败音效 (铃声).mp3")
                pygame.mixer.music.set_volume(0.5)
                pygame.mixer.music.play()
                end = font1.render("Game Over", True, (255, 0, 0))
                endscore_text = endscore.render("Score: " + str(score), True, (0, 0, 0))
                window.blit(endscore_text, (250, 370))  # 结束得分的位置
                window.blit(end, (200, 170))  # 结束标志的位置
                pygame.display.update()  # 刷新界面
                time.sleep(2)
                window.blit(s3, (enter_x, enter_y))
                window.blit(s4, (exit_x, exit_y))
                window.blit(s6, (choice_x, choice_y))
                pygame.display.update()
                pygame.mouse.set_visible(True)
                while True:#结束的循环
                    clock.tick(30)
                    buttons = pygame.mouse.get_pressed()  # 得到鼠标点击
                    x1, y1 = pygame.mouse.get_pos()  # 鼠标的坐标
                    if x1 >= enter_x and x1 <= s3.get_width() + enter_x and y1 >= enter_y and y1 <= s3.get_height() + enter_y:
                        window.blit(s3, (enter_x, enter_y))
                        #切换模式
                        if buttons[0]:
                            t = 0
                            s = 0  #累计时间置零
                            score = 0
                            hp = m #血量重置
                            speed_add = 2 #初速度重置
                            stratime = round(time.time(),2)
                            endtime = stratime
                            coin_y = coin1_y = coin2_y = coin3_y = bomb_y = bomb1_y = 1000#重新刷新掉落
                            load_time()
                            break
                    if x1 >= exit_x and x1 <= s2.get_width() + exit_x and y1 >= exit_y and y1 <= s2.get_height() + exit_y:
                        #退出游戏
                        window.blit(s4, (exit_x, exit_y))
                        if buttons[0]:
                            pygame.quit()
                            exit()
                    if x1 >= choice_x and x1 <= s5.get_width() + choice_x and y1 >= choice_y and y1 <= s5.get_height() + choice_y:
                        window.blit(s6, (choice_x, choice_y))
                        #继续当前模式
                        if buttons[0]:
                            choice = 1
                            t = 0
                            s = 0  # 累计时间置零
                            score = 0
                            speed_add = 2  # 初速度重置
                            coin_y = coin1_y = coin2_y = coin3_y = bomb_y = 1000  # 重新刷新掉落
                            load_time()
                            break

                    pygame.display.update()
                     # 监听事件
                    for event in pygame.event.get():

                        # 判断事件类型是否是退出事件
                        if event.type == pygame.QUIT:
                            print("游戏退出...")

                            # quit 卸载所有的模块
                            pygame.quit()

                            # exit() 直接终止当前正在执行的程序
                            exit()

                pygame.mixer.music.load(".\素材\活力四射 青春力量 by Alexguz-完整版.mp3")
                pygame.mixer.music.play(-1)

    # 更新屏幕显示
    pygame.display.update()
    clock.tick(120)

# 退出游戏
pygame.quit()

如需视频教程图片素材请评论留言,博主会不定期更新更多的Python实训代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值