每日一个小题

import pygame

import random

 

# 初始化 Pygame

pygame.init()

 

# 屏幕大小

screen_width = 300

screen_height = 600

block_size = 30

 

# 颜色定义

colors = [

    (0, 0, 0),

    (255, 0, 0),

    (0, 150, 0),

    (0, 0, 255),

    (255, 120, 0),

    (255, 255, 0),

    (180, 0, 255),

    (0, 220, 220)

]

 

# 形状定义

shapes = [

    [[1, 1, 1],

     [0, 1, 0]],

    

    [[0, 2, 2],

     [2, 2, 0]],

    

    [[3, 3, 0],

     [0, 3, 3]],

    

    [[4, 4],

     [4, 4]],

    

    [[0, 5, 0],

     [5, 5, 5]],

    

    [[6, 6, 6, 6]],

    

    [[7, 7],

     [7, 7]]

]

 

class Tetris:

    def __init__(self):

        self.width = screen_width // block_size

        self.height = screen_height // block_size

        self.board = [[0 for x in range(self.width)] for y in range(self.height)]

        self.score = 0

        self.gameover = False

        self.current_piece = self.new_piece()

        self.next_piece = self.new_piece()

        self.clock = pygame.time.Clock()

        self.fall_time = 0

        self.level = 1

        self.lines = 0

 

    def new_piece(self):

        shape = random.choice(shapes)

        color = colors[shapes.index(shape) + 1]

        return {'shape': shape, 'color': color, 'x': self.width // 2 - len(shape[0]) // 2, 'y': 0}

 

    def rotate_piece(self):

        piece = self.current_piece['shape']

        new_piece = list(zip(*piece[::-1]))

        self.current_piece['shape'] = new_piece

 

    def valid_space(self, piece, offset):

        off_x, off_y = offset

        for y, row in enumerate(piece):

            for x, cell in enumerate(row):

                if cell and (x + off_x < 0 or x + off_x >= self.width or y + off_y >= self.height or self.board[y + off_y][x + off_x]):

                    return False

        return True

 

    def freeze(self):

        piece = self.current_piece['shape']

        for y, row in enumerate(piece):

            for x, cell in enumerate(row):

                if cell:

                    self.board[y + self.current_piece['y']][x + self.current_piece['x']] = self.current_piece['color']

        self.clear_lines()

        self.current_piece = self.next_piece

        self.next_piece = self.new_piece()

        if not self.valid_space(self.current_piece['shape'], (self.current_piece['x'], self.current_piece['y'])):

            self.gameover = True

 

    def clear_lines(self):

        lines = 0

        for i, row in enumerate(self.board):

            if all(row):

                del self.board[i]

                self.board.insert(0, [0 for _ in range(self.width)])

                lines += 1

        self.score += lines ** 2 * 100

        self.lines += lines

        if self.lines >= 10:

            self.level += 1

            self.lines -= 10

            pygame.time.set_timer(pygame.USEREVENT+1, max(100, 1000 - (self.level-1)*100))

 

    def drop(self):

        self.current_piece['y'] += 1

        if not self.valid_space(self.current_piece['shape'], (self.current_piece['x'], self.current_piece['y'])):

            self.current_piece['y'] -= 1

            self.freeze()

 

    def move(self, dx):

        self.current_piece['x'] += dx

        if not self.valid_space(self.current_piece['shape'], (self.current_piece['x'], self.current_piece['y'])):

            self.current_piece['x'] -= dx

 

    def draw_grid(self, surface):

        for y in range(self.height):

            for x in range(self.width):

                pygame.draw.rect(surface, colors[self.board[y][x]], (x*block_size, y*block_size, block_size, block_size), 0)

                pygame.draw.rect(surface, (255, 255, 255), (x*block_size, y*block_size, block_size, block_size), 1)

 

    def draw_piece(self, surface):

        shape = self.current_piece['shape']

        for y, row in enumerate(shape):

            for x, cell in enumerate(row):

                if cell:

                    pygame.draw.rect(surface, self.current_piece['color'], ((self.current_piece['x'] + x) * block_size, (self.current_piece['y'] + y) * block_size, block_size, block_size), 0)

                    pygame.draw.rect(surface, (255, 255, 255), ((self.current_piece['x'] + x) * block_size, (self.current_piece['y'] + y) * block_size, block_size, block_size), 1)

 

    def run(self):

        screen = pygame.display.set_mode((screen_width, screen_height))

        pygame.display.set_caption('Tetris')

        pygame.time.set_timer(pygame.USEREVENT+1, 1000)

        while not self.gameover:

            for event in pygame.event.get():

                if event.type == pygame.QUIT:

                    pygame.quit()

                    return

                elif event.type == pygame.KEYDOWN:

                    if event.key == pygame.K_LEFT:

                        self.move(-1)

                    elif event.key == pygame.K_RIGHT:

                        self.move(1)

                    elif event.key == pygame.K_DOWN:

                        self.drop()

                    elif event.key == pygame.K_UP:

                        self.rotate_piece()

                elif event.type == pygame.USEREVENT+1:

                    self.drop()

            self.fall_time += self.clock.get_rawtime()

            self.clock.tick()

            if self.fall_time / 1000 > 1:

                self.drop()

                self.fall_time = 0

            screen.fill((0, 0, 0))

            self.draw_grid(screen)

            self.draw_piece(screen)

            pygame.display.flip()

        print("Game Over! Score:", self.score)

        pygame.quit()

 

if __name__ == '__main__':

    game = Tetris()

    game.run()

实现了一个基本的俄罗斯方块游戏,包括方块的移动、旋转、下落以及行的消除等功能,你可以直接运行这个脚本来玩游戏

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/68927.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

物联网 STM32【源代码形式-ESP8266透传】连接OneNet IOT从云产品开发到底层MQTT实现,APP控制 【保姆级零基础搭建】

一、MQTT介绍 MQTT&#xff08;Message Queuing Telemetry Transport&#xff0c;消息队列遥测传输协议&#xff09;是一种基于发布/订阅模式的轻量级通讯协议&#xff0c;构建于TCP/IP协议之上。它最初由IBM在1999年发布&#xff0c;主要用于在硬件性能受限和网络状况不佳的情…

C#常用744单词

1.visual 可见的 2.studio 工作室 3.dot 点 4.net 网 5.harp 尖端的&#xff0c;锋利的。 6.amework 骨架&#xff0c;构架&#xff0c;框架 7.beta 测试版&#xff0c;试用版 8.XML&#xff08;全称&#xff1a;eXtensible Markup Language&#xff09…

w186格障碍诊断系统spring boot设计与实现

&#x1f64a;作者简介&#xff1a;多年一线开发工作经验&#xff0c;原创团队&#xff0c;分享技术代码帮助学生学习&#xff0c;独立完成自己的网站项目。 代码可以查看文章末尾⬇️联系方式获取&#xff0c;记得注明来意哦~&#x1f339;赠送计算机毕业设计600个选题excel文…

题解 洛谷 Luogu P1955 [NOI2015] 程序自动分析 并查集 离散化 哈希表 C++

题目 传送门 P1955 [NOI2015] 程序自动分析 - 洛谷 | 计算机科学教育新生态https://www.luogu.com.cn/problem/P1955 思路 主要用到的知识是并查集 (如何实现并查集&#xff0c;这里不赘述了) 若 xi xj&#xff0c;则合并它们所在的集合。若 xi ! xj&#xff0c;则 i 和 …

教务学籍管理系统的设计与实现

标题:教务学籍管理系统的设计与实现 内容:1.摘要 教务学籍管理系统是学校管理学生信息的重要工具&#xff0c;它可以帮助学校提高管理效率&#xff0c;减少错误率&#xff0c;提高数据安全性。本文介绍了教务学籍管理系统的设计与实现&#xff0c;包括系统的需求分析、功能设计…

无用知识之:std::initializer_list的秘密

先说结论&#xff0c;用std::initializer_list初始化vector&#xff0c;内部逻辑是先生成了一个临时数组&#xff0c;进行了拷贝构造&#xff0c;然后用这个数组的起终指针初始化initializer_list。然后再用initializer_list对vector进行初始化&#xff0c;这个动作又触发了拷贝…

97,【5】buuctf web [极客大挑战 2020]Greatphp

进入靶场 审代码 <?php // 关闭所有 PHP 错误报告&#xff0c;防止错误信息泄露可能的安全隐患 error_reporting(0);// 定义一个名为 SYCLOVER 的类 class SYCLOVER {// 定义类的公共属性 $sycpublic $syc;// 定义类的公共属性 $loverpublic $lover;// 定义魔术方法 __wa…

VSCode 中的 Git Graph扩展使用详解

VSCode 中的 Git Graph 详解 1. 什么是 Git Graph&#xff1f; Git Graph 是 VSCode 中的一款 Git 可视化扩展&#xff0c;它提供了一种 图形化方式 来查看 Git 提交历史、分支、合并记录等信息&#xff0c;使得 Git 版本管理更加直观和高效。 通过 Git Graph&#xff0c;你…

蓝桥杯单片机第七届省赛

前言 这套题不难&#xff0c;相对于第六套题这一套比较简单了&#xff0c;但是还是有些小细节要抓 题目 OK&#xff0c;以上就是全部的题目了&#xff0c;这套题目相对来说逻辑比较简单&#xff0c;四个按键&#xff0c;S4控制pwm占空比&#xff0c;S5控制计时时间&#xff0…

【C语言】自定义类型讲解

文章目录 一、前言二、结构体2.1 概念2.2 定义2.2.1 通常情况下的定义2.2.2 匿名结构体 2.3 结构体的自引用和嵌套2.4 结构体变量的定义与初始化2.5 结构体的内存对齐2.6 结构体传参2.7 结构体实现位段 三、枚举3.1 概念3.2 定义3.3 枚举的优点3.3.1 提高代码的可读性3.3.2 防止…

我问了DeepSeek和ChatGPT关于vue中包含几种watch的问题,它们是这么回答的……

前言&#xff1a;听说最近DeepSeek很火&#xff0c;带着好奇来问了关于Vue的一个问题&#xff0c;看能从什么角度思考&#xff0c;如果回答的不对&#xff0c;能不能尝试纠正&#xff0c;并帮我整理出一篇不错的文章。 第一次回答的原文如下&#xff1a; 在 Vue 中&#xff0c;…

纯后训练做出benchmark超过DeepseekV3的模型?

论文地址 https://arxiv.org/pdf/2411.15124 模型是AI2的&#xff0c;他们家也是玩开源的 先看benchmark&#xff0c;几乎是纯用llama3 405B后训练去硬刚出一个gpt4o等级的LLamA405 我们先看之前的机遇Lllama3.1 405B进行全量微调的模型 Hermes 3&#xff0c;看着还没缘模型…

UbuntuWindows双系统安装

做系统盘&#xff1a; Ubuntu20.04双系统安装详解&#xff08;内容详细&#xff0c;一文通关&#xff01;&#xff09;_ubuntu 20.04-CSDN博客 ubuntu系统调整大小&#xff1a; 调整指南&#xff1a; 虚拟机中的Ubuntu扩容及重新分区方法_ubuntu重新分配磁盘空间-CSDN博客 …

在 Zemax 中使用布尔对象创建光学光圈

在 Zemax 中&#xff0c;布尔对象用于通过组合或减去较简单的几何形状来创建复杂形状。布尔运算涉及使用集合运算&#xff08;如并集、交集和减集&#xff09;来组合或修改对象的几何形状。这允许用户在其设计中为光学元件或机械部件创建更复杂和定制的形状。 本视频中&#xf…

AI作画提示词:Prompts工程技巧与最佳实践

成长路上不孤单&#x1f60a;&#x1f60a;&#x1f60a;&#x1f60a;&#x1f60a;&#x1f60a; 【14后&#x1f60a;///计算机爱好者&#x1f60a;///持续分享所学&#x1f60a;///如有需要欢迎收藏转发///&#x1f60a;】 今日分享关于物联网智能项目之——智能家居项目…

LLMs之DeepSeek:Math-To-Manim的简介(包括DeepSeek R1-Zero的详解)、安装和使用方法、案例应用之详细攻略

LLMs之DeepSeek&#xff1a;Math-To-Manim的简介(包括DeepSeek R1-Zero的详解)、安装和使用方法、案例应用之详细攻略 目录 Math-To-Manim的简介 1、特点 2、一个空间推理测试—考察不同大型语言模型如何解释和可视化空间关系 3、DeepSeek R1-Zero的简介&#xff1a;处理更…

二叉树——429,515,116

今天继续做关于二叉树层序遍历的相关题目&#xff0c;一共有三道题&#xff0c;思路都借鉴于最基础的二叉树的层序遍历。 LeetCode429.N叉树的层序遍历 这道题不再是二叉树了&#xff0c;变成了N叉树&#xff0c;也就是该树每一个节点的子节点数量不确定&#xff0c;可能为2&a…

详解单片机学的是什么?(电子硬件)

大家好&#xff0c;我是山羊君Goat。 单片机&#xff0c;对于每一个硬件行业的从业者或者在校电子类专业的学生&#xff0c;相信对于这个名词都不陌生&#xff0c;但是掌没掌握就另说了。 那单片机到底学的是什么呢&#xff1f; 其实单片机在生活中就非常常见&#xff0c;目前…

JavaScript Navigator:深入理解浏览器导航机制

JavaScript Navigator:深入理解浏览器导航机制 引言 在Web开发中,浏览器导航是用户与网页交互的重要部分。JavaScript Navigator对象提供了丰富的API,允许开发者深入理解并控制浏览器的导航行为。本文将详细介绍JavaScript Navigator对象的功能、使用方法以及在实际开发中…

MoonBit 编译器(留档学习)

MoonBit 编译器 MoonBit 是一个用户友好&#xff0c;构建快&#xff0c;产出质量高的编程语言。 MoonBit | Documentation | Tour | Core This is the source code repository for MoonBit, a programming language that is user-friendly, builds fast, and produces high q…