python贪吃蛇包含难度选择(tk and pygame)

import tkinter as tk
from tkinter import ttk
import pygame
import random

root = tk.Tk()
root.title('选择你的难度')
options = {"简单": 10, "中等": 24, "困难": 60}
choice = tk.StringVar()
combobox = ttk.Combobox(root, textvariable=choice)
combobox['values'] = list(options.keys())
combobox.pack()
root.geometry("500x100+700+400")
def set_framerate_and_start():
    framerate = options[choice.get()]

    pygame.init()
    # 游戏窗口大小
    screen_width = 800
    screen_height = 600

    # 颜色定义(R,G,B)
    black = (0, 0, 0)
    white = (255, 255, 255)
    red = (213, 50, 80)
    green = (0, 255, 0)
    blue = (50, 153, 213)

    game_display = pygame.display.set_mode((screen_width, screen_height))
    pygame.display.set_caption('引力透镜-贪吃蛇')

    clock = pygame.time.Clock()

    font_style = pygame.font.SysFont(None, 36)

    # 计分板显示
    def display_score(score):
        value = font_style.render("Score:" + str(score), True, white)
        game_display.blit(value, [0, 0])

    # 贪吃蛇定义
    def snake(snake_block_size, snake_list):
        for x in snake_list:
            pygame.draw.rect(game_display, green, [x[0], x[1], snake_block_size, snake_block_size])

    # 游戏结束提示信息显示
    def message(msg, color):
        mesg = font_style.render(msg, True, color)
        game_display.blit(mesg, [screen_width / 6.5, screen_height / 3])

    # 主函数
    def gameLoop():
        # 游戏初始化信息
        game_over = False  # 游戏是否结束标志位
        game_close = False  # 游戏关闭标志位

        x1 = screen_width / 2
        y1 = screen_height / 2

        # 记录蛇的位置信息
        x1_change = 0
        y1_change = 0

        snake_block_size = 10  # 蛇的大小
        snake_list = []  # 记录蛇的位置列表
        Length_of_snake = 1  # 蛇的长度

        # 随机生成豆子初始位置
        foodx = round(random.randrange(0, screen_width - snake_block_size) / 10.0) * 10.0
        foody = round(random.randrange(0, screen_height - snake_block_size) / 10.0) * 10.0

        # 游戏主循环

        while not game_over:
            while game_close == True:  # 游戏关闭,显示游戏结束提示信息
                game_display.fill(black)
                message("You failed!Press R tto replay,Or Press Q to quit ", white)
                display_score(Length_of_snake - 1)
                pygame.display.update()

                for event in pygame.event.get():
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_q:  # 关闭游戏
                            game_over = True
                            game_close = False
                        if event.key == pygame.K_r:  # 重新开始游戏
                            gameLoop()

            for event in pygame.event.get():
                if event.type == pygame.QUIT:  # 关闭游戏
                    game_over = True
                if event.type == pygame.KEYDOWN:  # 监听键盘事件
                    if event.key == pygame.K_LEFT:
                        x1_change = -snake_block_size
                        y1_change = 0
                    elif event.key == pygame.K_RIGHT:
                        x1_change = snake_block_size
                        y1_change = 0
                    elif event.key == pygame.K_UP:
                        y1_change = -snake_block_size
                        x1_change = 0
                    elif event.key == pygame.K_DOWN:
                        y1_change = snake_block_size
                        x1_change = 0
            # 判断是否碰到边缘,是则游戏结束
            if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
                game_close = True

            # 更新蛇的位置信息
            x1 += x1_change
            y1 += y1_change

            game_display.fill(black)  # 窗口填充黑色背景

            # 在窗口上画出豆子
            pygame.draw.rect(game_display, blue, [foodx, foody, snake_block_size, snake_block_size])

            # 将蛇的位置记录到snake_list中
            snake_head = []
            snake_head.append(x1)
            snake_head.append(y1)
            snake_list.append(snake_head)

            # 若蛇吃到了豆子,则生成新的豆子,同时更新得分和蛇的长度
            if x1 == foodx and y1 == foody:
                foodx = round(random.randrange(0, screen_width - snake_block_size) / 10.0) * 10.0
                foody = round(random.randrange(0, screen_height - snake_block_size) / 10.0) * 10.0
                Length_of_snake += 1

                # 判断蛇是否撞到自己,是则游戏结束
            if len(snake_list) > Length_of_snake:
                del snake_list[0]

            for x in snake_list[:-1]:
                if x == snake_head:
                    game_close = True

            # 画出蛇并更新得分
            snake(snake_block_size, snake_list)
            display_score(Length_of_snake - 1)

            pygame.display.update()  # 更新窗口

            clock.tick(framerate)  # 设置游戏帧率

        pygame.quit()
        quit()

    gameLoop()




# 当按下按钮时,将运行上述函数
button = tk.Button(root, text="开始游戏", command=set_framerate_and_start)
button.pack()

# 窗口运行
root.mainloop()


写代码不容易,希望读者们能点下赞^_^

有问题请在评论区说明

展示效果如下

 

 

 

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值