【用EXCEL编写俄罗斯方块小游戏(基于VBA)】

本文介绍如何使用Excel和VBA编写俄罗斯方块小游戏,详细讲解了设置游戏窗口、初始化游戏对象、实现游戏交互和保持游戏运行的过程,包括方块下落、碰撞检测、键盘交互等关键功能。
摘要由CSDN通过智能技术生成


工作属性原因,工作中使用excel办公是常态。前一阵子因工作业务需求,需要用到VBA。研究了一阵子VBA,解决了当时的需求。
后来想想,VBA可以如此彻底的控制excel,那么可不可以编个小游戏呢。
说干就干,先拿与表格最像的俄罗斯方块试试手。

预览成品效果 (文末附下载地址┗( ▔, ▔ )┛)

在这里插入图片描述

第一步:准备工作

首先,俄罗斯方块游戏需要完成哪些工作。

  1. 设置游戏窗口大小:俄罗斯方块游戏窗口大小为横10个方格、竖20个方格。
  2. 设置可变形方块元素:俄罗斯方块一共7种不同样式的方块。
  3. 设置游戏交互:俄罗斯方块有4种操作:,左移方块、右移方块、方块加速下落、方块变形。
  4. 保持游戏正常进行:随机形状方块下落,至底部或遇到方块后停止。任意行方块满行则分数+100,此行消除。方块堆满窗口游戏结束。

第二步:分步解决

(一)设置游戏窗口

设置游戏窗口大小及外观,对于有着多年做表经验的我来说,简直是信手拈来。(原来编游戏如此简单,这么快就完成了第一步。休息一天O(∩_∩)O哈哈~)

(二)初始化游戏各对象

设计思路:标准的俄罗斯方块共有7个方块,分别是“一”、“J”、“L”、“T”、“S”、“Z”、“田”。
我们注意到每个不同形状的俄罗斯方块均有4个方格,我们选取其中一个作为形状的旋转中心,并通过相对中心的偏移坐标储存不同方块。
在这里插入图片描述

	shape_0 = Array(Array(0, 0), Array(0, 1), Array(0, -1), Array(0, 2)) '初始化长方块
    shape_1 = Array(Array(0, 0), Array(0, 1), Array(0, -1), Array(-1, 1)) '初始化L1方块
    shape_2 = Array(Array(0, 0), Array(0, 1), Array(0, -1), Array(-1, -1)) '初始化L2方块
    shape_3 = Array(Array(0, 0), Array(0, 1), Array(0, -1), Array(-1, 0)) '初始化T方块
    shape_4 = Array(Array(0, 0), Array(0, 1), Array(-1, -1), Array(-1, 0)) '初始化Z方块
    shape_5 = Array(Array(0, 0), Array(0, -1), Array(-1, 1), Array(-1, 0)) '初始化S方块
    shape_6 = Array(Array(0, 0), Array(-1, 0), Array(-1, -1), Array(0, -1)) '初始化田方块
    shape_base = Array(shape_0, shape_1, shape_2, shape_3, shape_4, shape_5, shape_6) '所有方块数据存入数组

通过数组嵌套(非三维数组)完成方块坐标数据的存储。
随机产生0–6的随机数,根据随机数选取方块坐标数据。以焦点坐标为中心利用Offset函数偏移出四个range单元格,使用Union函数连接四个range单元格生成。然后对方块着色并加边框。 并对当前方块的下壁碰撞值赋值(用于碰撞检测,判定方块是否到底或者已落至某一方块上方)。

Sub draw_shape(s_n_can, s_s_x, s_s_y)'画出随机方块过程   
    Set drop_rng_focus = Cells(s_s_x, s_s_y) '传入焦点方块X,Y
    Set b_rng_can = Cells(s_s_x, s_s_y)
    Set p_rng_can = b_rng_can
    For dr_i_2 = 0 To UBound(shape_base(s_n_can))
        off_x_can = shape_base(s_n_can)(dr_i_2)(0)
        off_y_can = shape_base(s_n_can)(dr_i_2)(1)
        Set p_rng_can = Union(p_rng_can, b_rng_can.Offset(off_x_can, off_y_can))'偏移并连接单元格
    Next
    p_rng_can.Interior.ColorIndex = shape_color(s_n_can
  • 3
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是一个简单的Python俄罗斯方块小游戏的实现,可以使用Python标准库中的`pygame`模块进行图形化界面的开发: ```python import pygame import random # 定义方块的大小和游戏区域的大小 block_size = 30 game_width = 300 game_height = 600 # 定义颜色 white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) green = (0, 255, 0) blue = (0, 0, 255) yellow = (255, 255, 0) # 定义各种形状的方块,每个方块由四个小方块组成 shapes = [ [[1, 1, 1, 1]], [[1, 1], [1, 1]], [[1, 1, 1], [0, 1, 0]], [[1, 1, 0], [0, 1, 1]], [[0, 1, 1], [1, 1, 0]], [[1, 0, 0], [1, 1, 1]], [[0, 0, 1], [1, 1, 1]], ] class Block: def __init__(self, shape): self.shape = shape self.color = random.choice([red, green, blue, yellow]) self.x = 0 self.y = 0 def rotate(self): self.shape = list(zip(*self.shape[::-1])) def move_down(self): self.y += 1 def move_left(self): self.x -= 1 def move_right(self): self.x += 1 class Game: def __init__(self): self.screen = pygame.display.set_mode((game_width, game_height)) self.clock = pygame.time.Clock() self.score = 0 self.game_over = False self.board = [[0] * (game_width // block_size) for _ in range(game_height // block_size)] self.current_block = Block(random.choice(shapes)) def draw_block(self, block): for i, row in enumerate(block.shape): for j, cell in enumerate(row): if cell == 1: pygame.draw.rect(self.screen, block.color, (block.x + j, block.y + i, 1, 1)) def draw_board(self): for i, row in enumerate(self.board): for j, cell in enumerate(row): if cell != 0: pygame.draw.rect(self.screen, white, (j, i, 1, 1)) def check_collision(self, block): for i, row in enumerate(block.shape): for j, cell in enumerate(row): if cell == 1: if i + block.y >= game_height // block_size or j + block.x < 0 or j + block.x >= game_width // block_size or self.board[i + block.y][j + block.x] != 0: return True return False def add_block_to_board(self, block): for i, row in enumerate(block.shape): for j, cell in enumerate(row): if cell == 1: self.board[i + block.y][j + block.x] = block.color def remove_full_rows(self): new_board = [[0] * (game_width // block_size) for _ in range(game_height // block_size)] new_row = game_height // block_size - 1 for i in range(game_height // block_size - 1, -1, -1): if sum(self.board[i]) != game_width // block_size: new_board[new_row] = self.board[i] new_row -= 1 else: self.score += 1 self.board = new_board def run(self): while not self.game_over: self.clock.tick(10) for event in pygame.event.get(): if event.type == pygame.QUIT: self.game_over = True elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: self.current_block.move_left() if self.check_collision(self.current_block): self.current_block.move_right() elif event.key == pygame.K_RIGHT: self.current_block.move_right() if self.check_collision(self.current_block): self.current_block.move_left() elif event.key == pygame.K_DOWN: self.current_block.move_down() if self.check_collision(self.current_block): self.current_block.move_up() elif event.key == pygame.K_UP: self.current_block.rotate() if self.check_collision(self.current_block): self.current_block.rotate() self.screen.fill(black) self.draw_board() self.draw_block(self.current_block) if self.check_collision(self.current_block): self.add_block_to_board(self.current_block) self.current_block = Block(random.choice(shapes)) if self.check_collision(self.current_block): self.game_over = True else: self.current_block.move_down() self.remove_full_rows() pygame.display.update() pygame.quit() if __name__ == '__main__': pygame.init() game = Game() game.run() ``` 这个游戏包括一个游戏类`Game`和一个方块类`Block`,其中`Game`类负责游戏逻辑和图形化界面的绘制,`Block`类负责方块的移动和变形操作。游戏的主循环中,不断更新屏幕并处理用户输入,实现了俄罗斯方块的基本功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wrwttsy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值