NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io


 
import tkinter as tk
import tkinter.messagebox
import random

# 游戏界面的宽度和高度
WIDTH, HEIGHT = 800, 600
# 格子的大小
GRID_SIZE = 30
# 游戏区域的行数和列数
ROW, COL = 10, 10
# 颜色列表
COLORS = ['red', 'blue', 'green', 'yellow', 'purple', 'orange', 'pink']
# 得分字体大小
SCORE_SIZE = 30


def create_board():
"""
创建游戏棋盘

Returns:
board: 一个二维列表,代表游戏棋盘
"""
board = [[' ' for _ in range(COL)] for _ in range(ROW)]
# 在棋盘上随机生成一些方块
for i in range(10):
row = random.randint(0, ROW - 1)
col = random.randint(0, COL - 1)
color = random.choice(COLORS)
board[row][col] = color
return board


def check_win(board):
"""
检查是否有方块连成直线

Parameters:
board: 游戏棋盘

Returns:
winner: 如果有玩家获胜,返回玩家的颜色;如果没有玩家获胜,返回 None
"""
# 检查横向是否有三个或三个以上相同颜色的方块连成直线
for row in board:
count = 1
for col in range(len(row) - 1):
if row[col] == row[col + 1]:
count += 1
else:
count = 1
if count >= 3:
return row[col]
# 检查纵向是否有三个或三个以上相同颜色的方块连成直线
for col in range(COL):
count = 1
for row in range(len(board) - 1):
if board[row][col] == board[row + 1][col]:
count += 1
else:
count = 1
if count >= 3:
return board[row][col]
# 检查主对角线是否有三个或三个以上相同颜色的方块连成直线
count = 1
for i in range(len(board) - 1):
if board[i][i] == board[i + 1][i + 1]:
count += 1
else:
count = 1
if count >= 3:
return board[i][i]
# 检查副对角线是否有三个或三个以上相同颜色的方块连成直线
count = 1
for i in range(len(board) - 1):
if board[i][COL - i - 1] == board[i + 1][COL - i - 2]:
count += 1
else:
count = 1
if count >= 3:
return board[i][COL - i - 1]
# 如果没有玩家获胜,返回 None
return None


def play_game():
"""
玩消消乐游戏

Returns:
None
"""
# 创建游戏窗口
root = tk.Tk()
root.title('消消乐')
root.resizable(0, 0)
# 设置游戏窗口的宽度和高度
root.geometry(f'{WIDTH}x{HEIGHT}')
# 创建游戏画布
canvas = tk.Canvas(root, width=WIDTH * GRID_SIZE, height=HEIGHT * GRID_SIZE, bg='white')
canvas.pack()
# 定义画布上的方块大小和颜色
canvas.create_rectangle(0, 0, GRID_SIZE, GRID_SIZE, fill='', outline='black')
# 获取画布上的坐标范围
x_min, y_min, x_max, y_max = canvas.bbox('all')

score = 0
# 游戏区域的初始状态
board = create_board()
# 绘制游戏区域
for row in range(ROW):
for col in range(COL):
color = board[row][col]
if color != ' ':
canvas.create_rectangle(
col * GRID_SIZE + 1,
row * GRID_SIZE + 1,
GRID_SIZE - 1,
GRID_SIZE - 1,
fill=color,
outline='black'
)

# 监听画布上的鼠标左键点击事件
canvas.bind("<Button-1>", onclick)
# 进入游戏主循环
root.mainloop()

# 显示游戏结束窗口
tk.messagebox.showinfo('游戏结束', f'得分: {score}')


def onclick(event):
"""
处理鼠标点击事件

Parameters:
event: 鼠标事件

Returns:
None
"""
x, y = event.x, event.y
# 将鼠标坐标转换为游戏区域的坐标
row = (y - y_min) // GRID_SIZE
col = (x - x_min) // GRID_SIZE
# 如果点击的位置在游戏区域内
if 0 <= row < ROW and 0 <= col < COL:
# 获取点击位置的方块颜色
color = board[row][col]
# 如果点击的位置是空白方块
if color == ' ':
# 获取相邻方块的颜色
neighbors = [board[row - 1][col - 1], board[row - 1][col], board[row - 1][col + 1],
board[row][col - 1], board[row][col + 1], board[row + 1][col - 1],
board[row + 1][col], board[row + 1][col + 1]]
# 如果相邻的方块中有相同颜色的方块
if any(color == other for other in neighbors):
# 消除这些方块
for r, c in [(row - 1, col - 1), (row - 1, col), (row - 1, col + 1),
(row, col - 1), (row, col + 1), (row + 1, col - 1),
(row + 1, col), (row + 1, col + 1)]:
if 0 <= r < ROW and 0 <= c < COL and board[r][c] == color:
board[r][c] = ' '
score += 10
else:
# 随机生成一个方块并放在点击的位置
new_color = random.choice(COLORS)
while new_color == color:
new_color = random.choice(COLORS)
board[row][col] = new_color
# 如果生成的方块连成了直线,得分加 100
winner = check_win(board)
if winner:
score += 100
# 重新绘制游戏区域
canvas.delete('all')
for row in range(ROW):
for col in range(COL):
color = board[row][col]
if color != ' ':
canvas.
     
 
what is notes.io
 

Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 14 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.