吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 5567|回复: 19
收起左侧

[Python 原创] 【Python】俄罗斯方块源码

[复制链接]
ansver 发表于 2018-5-31 19:09
本帖最后由 wushaominkk 于 2018-6-1 10:49 编辑

源码可能不容易看懂
附上两张流程图
注意:
    1,图形旋转写的有些粗糙,玩起来可能有些小毛病
    2,使用的是pygame玩的话要先安装库

[Python] 纯文本查看 复制代码
import pygame, sys, random, time


# 第二版
def new_draw():
    screen.fill(white)

    for i in range(1, 21):
        for j in range(10):
            bolck = background[i][j]
            if bolck:
                pygame.draw.rect(screen, blue, (j * 25 + 1, 500 - i * 25 + 1, 23, 23))

    x, y = centre
    for i, j in active:
        i += x
        j += y
        pygame.draw.rect(screen, blue, (j * 25 + 1, 500 - i * 25 + 1, 23, 23))

    pygame.display.update()


def move_LR(n):
    """n=-1代表向左,n=1代表向右"""
    x, y = centre
    y += n
    for i, j in active:
        i += x
        j += y
        if j < 0 or j > 9 or background[i][j]:
            break
    else:
        centre.clear()
        centre.extend([x, y])


def rotate():
    x, y = centre
    l = [(-j, i) for i, j in active]
    for i, j in l:
        i += x
        j += y
        if j < 0 or j > 9 or background[i][j]:
            break
    else:
        active.clear()
        active.extend(l)


def move_down():
    x, y = centre
    x -= 1
    for i, j in active:
        i += x
        j += y
        if background[i][j]:
            break
    else:
        centre.clear()
        centre.extend([x, y])
        return
    # 如果新位置未被占用 通过return结束
    # 如果新位置被占用则继续向下执行
    x, y = centre
    for i, j in active:
        background[x + i][y + j] = 1

    l = []
    for i in range(1, 20):
        if 0 not in background[i]:
            l.append(i)
    # l装 行号,鉴于删去后,部分索引变化,对其降序排列,倒着删除
    l.sort(reverse=True)

    for i in l:
        background.pop(i)
        background.append([0 for j in range(10)])
        # 随删随补

    score[0] += len(l)
    pygame.display.set_caption("分数:%d" % (score[0]))

    active.clear()
    active.extend(list(random.choice(all_block)))
    # all_block保存7种形状的信息,手打出来的
    centre.clear()
    centre.extend([20, 4])

    x, y = centre
    for i, j in active:
        i += x
        j += y
        if background[i][j]:
            break
    else:
        return
    alive.append(1)


pygame.init()
screen = pygame.display.set_mode((250, 500))
pygame.display.set_caption("俄罗斯方块")
fclock = pygame.time.Clock()

all_block = (((0, 0), (0, -1), (0, 1), (0, 2)),
             ((0, 0), (0, 1), (-1, 0), (-1, 1)),
             ((0, 0), (0, -1), (-1, 0), (-1, 1)),
             ((0, 0), (0, 1), (-1, -1), (-1, 0)),
             ((0, 0), (0, 1), (1, 0), (0, -1)),
             ((0, 0), (1, 0), (-1, 0), (1, -1)),
             ((0, 0), (1, 0), (-1, 0), (1, 1)))
background = [[0 for i in range(10)] for j in range(24)]
background[0] = [1 for i in range(10)]
active = list(random.choice(all_block))
centre = [20, 4]
score = [0]


black = 0, 0, 0
white = 255, 255, 255
blue = 0, 0, 255

times = 0
alive = []
press = False
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                move_LR(-1)
            elif event.key == pygame.K_RIGHT:
                move_LR(1)
            elif event.key == pygame.K_UP:
                rotate()
            elif event.key == pygame.K_DOWN:
                press = True
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_DOWN:
                press = False
    if press:
        times += 10

    if times >= 50:
        move_down()
        times = 0
    else:
        times += 1

    if alive:
        pygame.display.set_caption("over分数:%d" % (score[0]))
        time.sleep(3)
        break
    new_draw()
    fclock.tick(100)

左右移动,旋转的

左右移动,旋转的

下落的

下落的

游戏截图

游戏截图

免费评分

参与人数 4吾爱币 +4 热心值 +4 收起 理由
苏紫方璇 + 2 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
kk1212 + 1 谢谢@Thanks!
aerofsm + 1 + 1 用心讨论,共获提升!
Hakon + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| ansver 发表于 2018-5-31 22:21
Hakon 发表于 2018-5-31 22:08
还是不太懂方块要如何与已有的“地形”做碰撞判定。

俄罗斯方块没那么复杂,,只是一个10X20二维的点阵

全局变量background二维数组,保存着背景 (0为空,1为有方块)
拿方块的坐标去挨个查数组  就知道有没有 “重叠”“碰撞”了

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
Hakon + 1 + 1 谢谢@Thanks!

查看全部评分

 楼主| ansver 发表于 2018-5-31 21:31
linuxprobe 发表于 2018-5-31 21:23
老游戏了,现在还有源码写出来。

没办法的呀,适合新手练习的游戏并不多
ww5231159 发表于 2018-5-31 19:34
demon_lin 发表于 2018-5-31 19:34
学习了,。
Taobi 发表于 2018-5-31 19:35
挺不错的 学习了
微风吹过。 发表于 2018-5-31 19:41
大神······················
bmwgtr 发表于 2018-5-31 20:04
感谢分享~~ 学习了~{
Setsuro 发表于 2018-5-31 20:10
学习下大神的思路
吾爱啦啦 发表于 2018-5-31 20:44
学习了,悲催,没学会
勤奋的刘小朵 发表于 2018-5-31 20:47
学习了大佬,你太厉害了
o0你最珍贵0o 发表于 2018-5-31 21:07
感谢分享,学习了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-21 19:50

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表