【Pygame实战】俄罗斯方块 | 太好玩了~停不下来,这种版本(Turtle彩版)你肯定没玩过……(经典怀旧:无人不知的俄罗斯方块)

导语

【Pygame实战】俄罗斯方块 | 太好玩了~停不下来,这种版本(Turtle彩版)你肯定没玩过……(经典怀旧:无人不知的俄罗斯方块)_第1张图片

警报警报!听说CSDN游戏专区火了火了~竟然是因为各种形状的方块。

【Pygame实战】俄罗斯方块 | 太好玩了~停不下来,这种版本(Turtle彩版)你肯定没玩过……(经典怀旧:无人不知的俄罗斯方块)_第2张图片

对!各种游戏都快烂大街了,俄罗斯方块咋滴就不能火一把了?

【Pygame实战】俄罗斯方块 | 太好玩了~停不下来,这种版本(Turtle彩版)你肯定没玩过……(经典怀旧:无人不知的俄罗斯方块)_第3张图片

【Pygame实战】俄罗斯方块 | 太好玩了~停不下来,这种版本(Turtle彩版)你肯定没玩过……(经典怀旧:无人不知的俄罗斯方块)_第4张图片

Python版俄罗斯方块 等你来战!

所有文章完整的素材+源码都在

粉丝白嫖源码福利,请移步至CSDN社区或文末公众hao即可免费。

——小Tips

俄罗斯方块,作为是一款家喻户晓的游戏,陪伴70、80甚至90后,度过无忧的儿时岁月

它上手简单能自由组合、拼接技巧也很多。

【Pygame实战】俄罗斯方块 | 太好玩了~停不下来,这种版本(Turtle彩版)你肯定没玩过……(经典怀旧:无人不知的俄罗斯方块)_第5张图片

【Pygame实战】俄罗斯方块 | 太好玩了~停不下来,这种版本(Turtle彩版)你肯定没玩过……(经典怀旧:无人不知的俄罗斯方块)_第6张图片

你知道么,最原始的俄罗斯方块,是长这样婶儿的~

【Pygame实战】俄罗斯方块 | 太好玩了~停不下来,这种版本(Turtle彩版)你肯定没玩过……(经典怀旧:无人不知的俄罗斯方块)_第7张图片

是不是很有童年的味道?今天小编还要给大家,介绍一个全新版本——

程序员的版本,期待期待

正文

自从俄罗斯猫被制裁以后,很多人不禁担心起俄罗斯方块的命运。

虽然名字的含俄量很高,但这款游戏圈抗衰老神话肯定不会遭殃,因为它的版权归美国人

有,跟俄罗斯没半毛钱关系。很多玩了半辈子俄罗斯方块的铁子现在多少能理解乔峰当年的心

情了吧~

算起来,俄罗斯方块都快39岁高龄了,圈子里比它老的游戏没它好玩,比它好玩的游戏没它

老。所以这一款为人类带来无数欢乐的游戏,值得我们更深入的了解。

【Pygame实战】俄罗斯方块 | 太好玩了~停不下来,这种版本(Turtle彩版)你肯定没玩过……(经典怀旧:无人不知的俄罗斯方块)_第8张图片

一、运行环境

小编使用的环境:Python3、Pycharm社区版、,部分自带的就不一一 展示啦。本文主要是一个

Turtle版本的。

 模块安装:pip install -i https://pypi.douban.com/simple/+模块名 

二、代码展示

import turtle
import random

class Block:
    def __init__(self, color, tiles):
        self.color = color
        self.tiles = tiles
        
I = Block("cyan", [ [ [ 1, 0, 0, 0 ],
                [ 1, 0, 0, 0 ],
                [ 1, 0, 0, 0 ],
                [ 1, 0, 0, 0 ] ],
              
              [ [ 0, 0, 0, 0 ],
                [ 0, 0, 0, 0 ],
                [ 0, 0, 0, 0 ],
                [ 1, 1, 1, 1 ] ] ])
                
J = Block("blue", [ [ [ 0, 1, 0 ],
                [ 0, 1, 0 ],
                [ 1, 1, 0 ] ],
              
              [ [ 0, 0, 0 ],
                [ 1, 1, 1 ],
                [ 0, 0, 1 ] ],
              
              [ [ 1, 1, 0 ],
                [ 1, 0, 0 ],
                [ 1, 0, 0 ] ],
              
              [ [ 0, 0, 0 ],
                [ 1, 0, 0 ],
                [ 1, 1, 1 ] ] ])
                
L = Block("orange", [ [ [ 1, 0, 0 ],
                [ 1, 0, 0 ],
                [ 1, 1, 0 ] ],
              
              [ [ 0, 0, 0 ],
                [ 0, 0, 1 ],
                [ 1, 1, 1 ] ],
              
              [ [ 0, 1, 1 ],
                [ 0, 0, 1 ],
                [ 0, 0, 1 ] ],
              
              [ [ 0, 0, 0 ],
                [ 1, 1, 1 ],
                [ 1, 0, 0 ] ] ])
                
S = Block("lime", [ [ [ 0, 0, 0 ],
                [ 0, 1, 1 ],
                [ 1, 1, 0 ] ],
              
              [ [ 1, 0, 0 ],
                [ 1, 1, 0 ],
                [ 0, 1, 0 ] ] ])
                
Z = Block("red", [ [ [ 0, 0, 0 ],
                [ 1, 1, 0 ],
                [ 0, 1, 1 ] ],
              
              [ [ 0, 1, 0 ],
                [ 1, 1, 0 ],
                [ 1, 0, 0 ] ] ])
                
O = Block("yellow", [ [ [ 1, 1 ],
                     [ 1, 1 ] ] ])
                     
T = Block("magenta", [ [ [ 0, 0, 0 ],
                [ 0, 1, 0 ],
                [ 1, 1, 1 ] ],
              
              [ [ 0, 1, 0 ],
                [ 1, 1, 0 ],
                [ 0, 1, 0 ] ],
              
              [ [ 0, 0, 0 ],
                [ 1, 1, 1 ],
                [ 0, 1, 0 ] ],
              
              [ [ 1, 0, 0 ],
                [ 1, 1, 0 ],
                [ 1, 0, 0 ] ] ])
                



tile_size = 25
map_rows = 20
map_cols = 10
map_x = -125
map_y = 250


map_turtle = turtle.Turtle()
map_turtle.hideturtle()
map_turtle.up()

game_map = [["" for _ in range(map_cols)] for _ in range(map_rows)]


active_block = None
active_block_row = 0
active_block_col = 0
active_block_index = 0

block_turtle = turtle.Turtle()
block_turtle.hideturtle()
block_turtle.up()


game_update_interval = 250


score = 0
score_turtle = turtle.Turtle()
score_turtle.hideturtle()
score_turtle.up()
score_turtle.goto(170, 210)
score_turtle.write("Score: " + str(score), font=("Calibri", 20, "bold"))


game_over_turtle = turtle.Turtle()
game_over_turtle.hideturtle()
game_over_turtle.color("red")



def draw_box(t, width, height, pencolor, fillcolor):
    t.color(pencolor, fillcolor)
    t.down()
    t.begin_fill()
    for _ in range(2):
        t.forward(width)
        t.right(90)
        t.forward(height)
        t.right(90)
    t.end_fill()
    t.up()



def draw_map():
    map_turtle.clear()
    for row in range(map_rows):
        for col in range(map_cols):
            map_turtle.goto(map_x + tile_size * col, map_y - tile_size * row)
            draw_box(map_turtle, tile_size, tile_size, "black", game_map[row][col].color if game_map[row][col] else "mintcream")



def make_new_block():
    global active_block
    global active_block_row, active_block_col
    global active_block_index

    active_block = random.choice((I, J, L, S, Z, O, T))
    active_block_row = 0
    active_block_col = 4
    active_block_index = 0



def draw_block():
    block_turtle.clear()

    # Find the x and y position of the block
    x = map_x + active_block_col * tile_size
    y = map_y - active_block_row * tile_size

    block_tiles = active_block.tiles[active_block_index]
    block_color = active_block.color
    for row in range(len(block_tiles)):
        for col in range(len(block_tiles[row])):
            if block_tiles[row][col] == 1:
                block_turtle.goto(x+col*tile_size, y-row*tile_size)
                draw_box(block_turtle, tile_size, tile_size, "black", block_color)
    


def is_valid_block(block_type, block_row, block_col, block_index):

    block_tiles = block_type.tiles[block_index]
    for row in range(len(block_tiles)):
        for col in range(len(block_tiles[row])):
            if block_tiles[row][col] == 1:
                if block_row + row not in range(0, map_rows):
                    return False
                if block_col + col not in range(0, map_cols):
                    return False
                if game_map[block_row + row][block_col + col] != "":
                    return False

    return True



def set_block_on_map():
    block_tiles = active_block.tiles[active_block_index]
    for row in range(len(block_tiles)):
        for col in range(len(block_tiles[row])):
            if block_tiles[row][col] == 1:
                game_map[active_block_row + row][active_block_col + col] = active_block
    draw_map()



r = 0
def remove_completed_rows():
    global game_map
    global score
    global game_update_interval
    global r

    new_map = []
    for row in range(len(game_map)):
        game_row = game_map[row]
        if "" in game_row:
            new_map.append(game_row)
        else:
            score += 10
            score_turtle.clear()
            score_turtle.write("Score: " + str(score), font=("Calibri", 20, "bold"))
            r += 1
            if r == 5:
                game_update_interval = int(game_update_interval/1.1)
                r = 0
        
    for row in range(0, map_rows - len(new_map)):
        game_row = ["" for _ in range(map_cols)]
        new_map.insert(0, game_row)

    game_map = new_map
    draw_map()

    # Task: increase the score and difficulty when a row is completed


pause = False
def game_loop():
    global active_block, active_block_row

    if active_block is None:
        make_new_block()
        if not is_valid_block(active_block, active_block_row, active_block_col, active_block_index):
            active_block = None
            game_over_turtle.write("Game over!", align="center", font=("Calibri", 60, "bold"))
            return
        draw_block()

    else:
        if is_valid_block(active_block, active_block_row + 1, active_block_col, active_block_index):
            if not pause:
                active_block_row += 1
                draw_block()
        else:
            set_block_on_map()
            active_block = None
            remove_completed_rows()
    
    turtle.update()

    # Set the next update

    turtle.ontimer(game_loop, game_update_interval)



# Set up the turtle window
turtle.setup(800, 600)
turtle.title("Tetris")
turtle.bgcolor("navajowhite")
turtle.up()
turtle.hideturtle()
turtle.tracer(False)

# Draw the background border around the map
turtle.goto(map_x - 10, map_y + 10)
draw_box(turtle, tile_size * map_cols + 20, tile_size * map_rows + 20, \
         "", "lightslategray")

# Draw the empty map in the window
draw_map()
turtle.update()

# Set up the game loop
turtle.ontimer(game_loop, game_update_interval)



def rotate():
    global active_block_index

    if active_block is None:
        return
    new_block_index = (active_block_index + 1) % len(active_block.tiles)
    if is_valid_block(active_block, active_block_row, active_block_col, new_block_index):
        active_block_index = new_block_index
        draw_block()
turtle.onkeypress(rotate, "Up")


def move_left():
    global active_block_col

    if active_block is None:
        return
    if is_valid_block(active_block, active_block_row, active_block_col - 1, active_block_index):
        active_block_col -= 1
        draw_block()
turtle.onkeypress(move_left, "Left")


def move_right():
    global active_block_col

    if active_block is None:
        return
    if is_valid_block(active_block, active_block_row, active_block_col + 1, active_block_index):
        active_block_col += 1
        draw_block()
turtle.onkeypress(move_right, "Right")


def drop():
    global active_block_row

    if active_block is None:
        return
    while is_valid_block(active_block, active_block_row + 1, active_block_col, active_block_index):
        active_block_row += 1
    draw_block()
turtle.onkeypress(drop, "Down")


def pause_game():
    global pause
    pause = not pause

turtle.onkeypress(pause_game, "space")


def change_block_type():
    global active_block
    global active_block_index
  
    new_block = random.choice((I, J, L, S, Z, O, T))
    new_block_index = 0
    if is_valid_block(new_block, active_block_row, active_block_col, new_block_index):
        active_block = new_block
        active_block_index = new_block_index
        draw_block()
turtle.onkeypress(change_block_type, "c")



turtle.listen()

turtle.done()

【Pygame实战】俄罗斯方块 | 太好玩了~停不下来,这种版本(Turtle彩版)你肯定没玩过……(经典怀旧:无人不知的俄罗斯方块)_第9张图片三、效果展示

1)游戏开始

【Pygame实战】俄罗斯方块 | 太好玩了~停不下来,这种版本(Turtle彩版)你肯定没玩过……(经典怀旧:无人不知的俄罗斯方块)_第10张图片

2)方块儿截图game over

【Pygame实战】俄罗斯方块 | 太好玩了~停不下来,这种版本(Turtle彩版)你肯定没玩过……(经典怀旧:无人不知的俄罗斯方块)_第11张图片

​普通玩家:                                                              高手玩家:

【Pygame实战】俄罗斯方块 | 太好玩了~停不下来,这种版本(Turtle彩版)你肯定没玩过……(经典怀旧:无人不知的俄罗斯方块)_第12张图片【Pygame实战】俄罗斯方块 | 太好玩了~停不下来,这种版本(Turtle彩版)你肯定没玩过……(经典怀旧:无人不知的俄罗斯方块)_第13张图片

 总结

有颜色的方块儿还是玩起来有意思些,空格暂停、方向键移动的哈。不管是高手玩家还是普通玩家

都是我可望不可及的程序,仰望.jpg 看我40分的总成绩就知道了!哈哈哈

完整的免费源码领取处:找我吖!文末公众hao可自行领取,滴滴我也可!

推荐往期文章——

项目1.0  超级玛丽

​​​​​​程序员自制游戏:超级玛丽100%真实版,能把你玩哭了~【附源码】

项目1.1   扫雷

 Pygame实战:据说这是史上最难扫雷游戏,没有之一,你们感受下......

项目1.2   魂斗罗

Pygame实战:多年后“魂斗罗”像素风归来 不止是经典与情怀@全体成员

项目1.4  水果忍者

【Pygame实战】风靡全球的切水果游戏升级版“水果忍者”上线啦,你敢来PK嘛?

项目1.0  病毒版蛇蛇大作战

【Pygame实战】这游戏有毒,刷爆朋友圈:小编已与病毒版贪吃蛇大战了三百回合,最高分339?

文章汇总——

汇总合集  Python—2022 |已有文章汇总 | 持续更新,直接看这篇就够了

(更多内容+源码都在✨文章汇总哦!!欢迎阅读喜欢的文章~)

【Pygame实战】俄罗斯方块 | 太好玩了~停不下来,这种版本(Turtle彩版)你肯定没玩过……(经典怀旧:无人不知的俄罗斯方块)_第14张图片

你可能感兴趣的:(python合集,绘图合集,Pygame合集,pygame,python,turtle,俄罗斯方块,游戏源码)