Untitled

 avatar
unknown
python
2 years ago
6.4 kB
4
Indexable
import pygame
import tkinter as tk

colors = [
    (0,0,0),
    (211,211,211),
    (103,106,106)
]

class Figure:
    x = 0
    y = 0

    figures = [
        [[1, 5, 9], [4, 5, 6]],
        [[0,1,2,5,8,9,10],[0,2,4,5,6,8,10]]
    ]

    def __init__(self, x, y, figure_type):
        self.x = x
        self.y = y
        self.type = figure_type
        self.color = figure_type + 1
        self.rotation = 0

    def image(self):
        return self.figures[self.type][self.rotation]

    def rotate(self):
        self.rotation = (self.rotation + 1) % len(self.figures[self.type])


class Tetris:
    level = 2
    
    state = "start"
    field = []
    height = 0
    width = 0
    x = 100
    y = 60
    zoom = 20
    figure = None
    figure_type = 0

    def __init__(self, height, width):
        self.height = height
        self.width = width
        for i in range(height):
            new_line = []
            for j in range(width):
                new_line.append(0)
            self.field.append(new_line)

    def new_figure(self):
        self.figure = Figure(Width//2-1, 0, self.figure_type)

    def intersects(self):
        intersection = False
        for i in range(4):
            for j in range(4):
                if i * 4 + j in self.figure.image():
                    if i + self.figure.y > self.height - 1 or \
                            j + self.figure.x > self.width - 1 or \
                            j + self.figure.x < 0 or \
                            self.field[i + self.figure.y][j + self.figure.x] > 0:
                        intersection = True
        return intersection

    def go_space(self):
        while not self.intersects():
            self.figure.y += 1
        self.figure.y -= 1
        self.freeze()

    def go_down(self):
        self.figure.y += 1
        if self.intersects():
            self.figure.y -= 1
            self.freeze()

    def freeze(self):
        for i in range(4):
            for j in range(4):
                if i * 4 + j in self.figure.image():
                    self.field[i + self.figure.y][j + self.figure.x] = self.figure.color
        self.new_figure()
        if self.intersects():
            game.state = "gameover"

    def go_side(self, dx):
        old_x = self.figure.x
        self.figure.x += dx
        if self.intersects():
            self.figure.x = old_x

    def rotate(self):
        old_rotation = self.figure.rotation
        self.figure.rotate()
        if self.intersects():
            self.figure.rotation = old_rotation

Height=30
Width=20


import tkinter as tk


window = tk.Tk()
window.title('Tetris')
window.geometry('410x300')
window.resizable(False, False)
window.config(bg="white")

# 自訂函數
def StartFunc():
    global Height,Width
    Height=int(Height_Value.get())
    Width=int(Width_Value.get())
    window.destroy()

# 建立按鈕
label = tk.Label(text="俄羅斯方塊遊戲",fg="black",bg="white",font=("Arial", 30, "bold"))
label.place(x=90,y=50)

Height_Label = tk.Label(text="高度:",fg="black",bg="white",font=("Arial", 20, "bold"))
Height_Label.place(x=90,y=150)

Height_Value = tk.Entry(width=5, font=("Arial", 15, "bold"), bg="white", fg="black")
Height_Value.place(x=140,y=150)


Weight_Label = tk.Label(text="寬度:",fg="black",bg="white",font=("Arial", 20, "bold"))
Weight_Label.place(x=200,y=150)

Width_Value = tk.Entry(width=5, font=("Arial", 15, "bold"), bg="white", fg="black")
Width_Value.place(x=250,y=150)

button = tk.Button(window, text = 'Start', command = StartFunc,width=20,height=2,borderwidth=0) 
button.place(x=100,y=220)

window.mainloop()

# 初始化遊戲引擎
pygame.init()

# 定義一些顏色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)

size = (Width*20+200, Height*20+150)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Tetris")

# 迴圈,直到使用者點選關閉按鈕
done = False
clock = pygame.time.Clock()
fps = 25
game = Tetris(Height, Width)
counter = 0

pressing_down = False

while not done:
    if game.figure is None:
        game.new_figure()
    counter += 1
    if counter > 100000:
        counter = 0

    if counter % (fps // game.level // 2) == 0 or pressing_down:
        if game.state == "start":
            game.go_down()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                game.rotate()
            if event.key == pygame.K_DOWN:
                pressing_down = True
            if event.key == pygame.K_LEFT:
                game.go_side(-1)
            if event.key == pygame.K_RIGHT:
                game.go_side(1)
            if event.key == pygame.K_SPACE:
                game.go_space()
            if event.key == pygame.K_q:
                done=True
            if event.key == pygame.K_c:
                game.figure_type = not game.figure_type
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_DOWN:
                pressing_down = False

    screen.fill(WHITE)

    for i in range(game.height):
        for j in range(game.width):
            pygame.draw.rect(screen, GRAY, [game.x + game.zoom * j, game.y + game.zoom * i, game.zoom, game.zoom], 1)
            if game.field[i][j] > 0:
                pygame.draw.rect(screen, colors[game.field[i][j]],
                                 [game.x + game.zoom * j + 1, game.y + game.zoom * i + 1, game.zoom - 2, game.zoom - 1])

    if game.figure is not None:
        for i in range(4):
            for j in range(4):
                p = i * 4 + j
                if p in game.figure.image():
                    pygame.draw.rect(screen, colors[game.figure.color],
                                     [game.x + game.zoom * (j + game.figure.x) + 1,
                                      game.y + game.zoom * (i + game.figure.y) + 1,
                                      game.zoom - 2, game.zoom - 2])

    font = pygame.font.SysFont('Calibri', 25, True, False)
    font1 = pygame.font.SysFont('Calibri', (Width*20+200)//10, True, False)
    
    text_game_over = font1.render("Game Over", True, (255, 0, 0))

    
    if game.state == "gameover":
        screen.blit(text_game_over, [(Width*20+200)//2-text_game_over.get_width()//2,(Height*20+150)//2-text_game_over.get_height()//2])  

    pygame.display.flip()
    clock.tick(fps)

pygame.quit()
Editor is loading...