【附源码】Python期末课程设计 :足球看板与计分系统



前言

这篇文章是之前为了应对python期末课程设计而匆忙编写的,纯在一些细小的bug,但是整体观感可以达到期末课设的优秀程度,同时该系统由博主与博主的舍友联合编写,在征得他本人的同意后,把这份文件无私奉献给大家,在此特别鸣谢老王@2201_75665961


一、期末课程设计需求

我们要求能够写出一个独立的系统,能够在一台或多台电脑上进行通信,具体要求如下:
1、能够在不联网情况下独立使用
2、能够多电脑通信
3、能够直观展示系统界面
4、要符合现实,能对现实社会产生良好影响

因此,我们写出了这个足球看板与计分系统

二、代码详情

2.1 接收端

接收端代码是一个基于Tkinter和MQTT的足球比赛计时和信息显示应用。它使用Python编写,主要功能包括显示比赛时间、球队名称、得分、罚球信息和替换球员信息。

2.1.1 导入库

代码如下(示例):

import tkinter as tk
import time
import paho.mqtt.client as mqtt

tkinter: 用于创建图形用户界面(GUI)。
time: 提供时间相关的功能,主要用于计时。
paho.mqtt.client: 用于MQTT协议的客户端实现,支持消息发布和订阅。


2.1.2 创建主窗口

代码如下(示例):

window = tk.Tk()
window.title("football match screen")
window.geometry("800x500")
window.configure(bg="black")

创建一个Tkinter窗口,设置标题、大小和背景颜色。


2.1.3 定义变量

代码如下(示例):

team1_name = tk.StringVar(value="TEAM1")
team2_name = tk.StringVar(value="TEAM2")

score1 = tk.IntVar(value=0)
score2 = tk.IntVar(value=0)

match_time = tk.StringVar(value="00:00:00")

StringVar和IntVar用于存储和跟踪字符串和整数类型的变量,自动更新界面显示。


2.1.4 初始化状态变量

代码如下(示例):

start_time = None
is_paused = False  # 全局变量,跟踪时间是否被暂停

penalties = []
penalty_display_time = 5000

substitutions = []
substitutions_display_time = 5000

start_time: 用于记录比赛开始的时间。
is_paused: 布尔值,指示比赛是否暂停。
penalties和substitutions: 列表,用于存储罚球和替换信息。
penalty_display_time和substitutions_display_time: 用于设置罚球和替换信息的显示时间(毫秒)。


2.1.5 加时功能

代码如下(示例):

def start_overtime(minutes):
    global start_time
    start_time = time.time() + (minutes * 60)  # 将加时时间加到当前时间
    update_match_time()  # 更新时间显示

start_overtime: 接收加时分钟数,计算新的开始时间并调用update_match_time更新显示。


2.1.6 更新时间显示

代码如下(示例):

def update_match_time():
    global start_time, is_paused  # 声明全局变量
    if start_time is not None and not is_paused:
        elapsed_time = time.time() - start_time
        hours, remainder = divmod(int(elapsed_time), 3600)
        minutes, seconds = divmod(remainder, 60)
        match_time.set(f"{
     
     int(hours):02}:{
     
     int(minutes):02}:{
     
     int(seconds):02}")
    window.after(1000, update_match_time)

update_match_time: 每秒更新一次比赛时间,计算经过的时间并格式化为HH:MM:SS,通过match_time更新显示。


2.1.7 罚球和替换信息更新

代码如下(示例):

def update_penalties():
    penalties_text = "\n".join(penalties)
    penalties_label.config(text=penalties_text)

def clear_penalty():
    penalties_label.config(text="")
    penalties.clear()

def clear_substitution():
    substitutions_label_team.config(text="")
    substitutions_label_off.config(text="")
    substitutions_label_on.config(text="")
    substitutions.clear()

update_penalties: 更新罚球信息显示。
clear_penalty和clear_substitution: 清空罚球和替换信息的显示。


2.1.8 MQTT连接和消息处理

代码如下(示例):

def on_connect(client, userdata, flags, reason_code, properties):
    print(f"Connected with result code {
     
     reason_code}")
    client.subscribe("score/update")
    client.subscribe("team/update")
    client.subscribe("match/start")
    client.subscribe("penalty/info")
    client.subscribe("substitution/info")
    client.subscribe("time/pause")
    client.subscribe("time/resume")

on_connect: 当MQTT客户端连接成功时调用,订阅多个主题以接收相关消息。

def on_message(client, userdata, msg):
    global is_paused  # 声明全局变量
    data = msg.payload.decode()
    if msg.topic == "score/update":
        team, value = data.split(',')
        score = int(value)
        if team == "team1":
            score1.set(score1.get() + score)
        elif team == "team2":
            score2.set(score2.get() + score)
    elif msg.topic == "team/update":
        if data.split(",")[0] == "0":
            team1_name.set(data.split(",")[1])
        elif data.split(",")[0] == "1":
            team2_name.set(data.split(",")[1])
    elif msg.topic == "match/start":
        global start_time
        start_time = time.time()
    elif msg.topic == "penalty/info":
        penalties.append(data)
        update_penalties()
        window.after(penalty_display_time, clear_penalty)
    elif msg.topic == "substitution/info":
        data = msg.payload.decode()
        team, off_player, on_player = data.split(",")
        substitutions_label_team.config(text=f"{
     
     team}")
        substitutions_label_off.config(text=f"{
     
     off_player}")
        substitutions_label_on.config(text=f"{
     
     on_player}")
        window.after(substitutions_display_time, clear_substitution)
    elif msg.topic == "time/pause":
        is_paused = True
    elif msg.topic == "time/resume":
        is_paused = False
    elif msg.topic == "overtime/set":
        overtime = int(data)  # 设置加时时间
        print(f"Overtime set to {
     
     overtime} minutes")
    elif msg.topic == "overtime/start":
        start_overtime(overtime)  # 开始加时

on_message: 处理接收到的MQTT消息,根据不同主题更新比赛信息、得分、球队名称、罚球和替换信息,以及控制比赛的开始、暂停和加时。


2.1.9 MQTT客户端设置

代码如下(示例):

mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.connect("localhost", 1883, 60)

mqttc.loop_start()

创建MQTT客户端,设置连接和消息处理函数,连接到本地MQTT代理并启动循环以处理消息。


2.1.10 创建GUI组件

代码如下(示例):

tk.Label(window, text="Match time:", bg="black", fg="white").pack()
tk.Label(window, textvariable=match_time, font=("Arial", 15), bg="black", fg="white")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

爱吃饭团的饭桶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值