gpt4 book ai didi

python - 在列表中添加矩形以在 pygame 中以 7 段数字显示

转载 作者:行者123 更新时间:2023-12-04 07:59:43 26 4
gpt4 key购买 nike

我有两个 python 文件,一个用于保存以前制作的所有数据,另一个用于使用 pygame 在屏幕上正确显示数据的函数文件。
--功能--
我的想法是,段的显示按字母顺序命名为 A、B、C、D、E、F、G
PLZ refer to this image
在这里,到目前为止,每个段都在显示,但是如果你在我的 data.py 代码中看到,你可以看到有一个名为“final_data”的字典,其中每个数字都有它的二进制代码,这只是意味着“有多少段必须发光......” 注意:- '1' 表示发光,'0' 表示不发光,你可以说这些是按钮。 每个按钮都有一个 [x,y,width,height] 列表来显示在屏幕上。我还给出了二进制文件和位置。字典工作正常,问题在于它在我的第二个文件“functions.py”中的实现。
--代码--
我的第一个文件是data.py,代码是......

nums = (0,1,2,3,4,5,6,7,8,9)

binaries = ([1,1,1,1,1,1,0],[1,1,0,0,0,0,0],[1,0,1,1,0,1,1],[1,1,1,0,0,1,1],
[1,1,0,0,1,0,1],[0,1,1,0,1,1,1],[0,1,1,1,1,0,1],[1,1,0,0,0,1,0],[1,1,1,1,1,1,1],[1,1,1,0,1,1,1])

positions = ([200,110,10,50],[200,170,10,50],[150,220,50,10],[140,170,10,50],[140,110,10,50],[150,100,50,10],[150,160,50,10])


final_data = {'0':{"1": [[200, 110, 10, 50], [200, 170, 10, 50], [150, 220, 50, 10], [140, 170, 10, 50], [140, 110, 10, 50], [150, 100, 50, 10]], "0": [[150, 160, 50, 10]]},
'1':{"1": [[200, 110, 10, 50], [200, 170, 10, 50]], "0": [[150, 220, 50, 10], [140, 170, 10, 50], [140, 110, 10, 50], [150, 100, 50, 10], [150, 160, 50, 10]]},
'2':{"1": [[200, 110, 10, 50], [150, 220, 50, 10], [140, 170, 10, 50], [150, 100, 50, 10], [150, 160, 50, 10]], "0": [[200, 170, 10, 50], [140, 110, 10, 50]]},
'3':{"1": [[200, 110, 10, 50], [200, 170, 10, 50], [150, 220, 50, 10], [150, 100, 50, 10], [150, 160, 50, 10]], "0": [[140, 170, 10, 50], [140, 110, 10, 50]]},
'4':{"1": [[200, 110, 10, 50], [200, 170, 10, 50], [140, 110, 10, 50], [150, 160, 50, 10]], "0": [[150, 220, 50, 10], [140, 170, 10, 50], [150, 100, 50, 10]]},
'5':{"1": [[200, 170, 10, 50], [150, 220, 50, 10], [140, 110, 10, 50], [150, 100, 50, 10], [150, 160, 50, 10]],"0": [[200, 110, 10, 50], [140, 170, 10, 50]]},
'6':{"1": [[200, 170, 10, 50], [150, 220, 50, 10], [140, 170, 10, 50], [140, 110, 10, 50], [150, 160, 50, 10]],"0": [[200, 110, 10, 50], [150, 100, 50, 10]]},
'7':{"1": [[200, 110, 10, 50], [200, 170, 10, 50], [150, 100, 50, 10]], "0": [[150, 220, 50, 10], [140, 170, 10, 50], [140, 110, 10, 50], [150, 160, 50, 10]]},
'8':{"1": [[200, 110, 10, 50], [200, 170, 10, 50], [150, 220, 50, 10], [140, 170, 10, 50], [140, 110, 10, 50], [150, 100, 50, 10], [150, 160, 50, 10]]},
'9':{"1": [[200, 110, 10, 50], [200, 170, 10, 50], [150, 220, 50, 10], [140, 110, 10, 50], [150, 100, 50, 10], [150, 160, 50, 10]], "0": [[140, 170, 10, 50]]}
}

"""
A,B,C,D,E,F,G
1,1,1,1,1,1,0
1,1,0,0,0,0,0
1,0,1,1,0,1,1
1,1,1,0,0,1,1
1,1,0,0,1,0,1
0,1,1,0,1,1,1
0,1,1,1,1,0,1
1,1,0,0,0,1,0
1,1,1,1,1,1,1
1,1,1,0,1,1,1
"""
我的第二个文件functions.py
import pygame, sys
pygame.init()

size = width,height = 400,400
running = True
segments = []
screen = pygame.display.set_mode(size)

def draw_rect(x,y,width,height):
pygame.draw.rect(screen,(0,0,0),(x,y,width,height))

def digits():
#A
draw_rect(200,110,10,50)
#B
draw_rect(200,170,10,50)
#C
draw_rect(150,220,50,10)
#D
draw_rect(140,170,10,50)
#E
draw_rect(140,110,10,50)
#F
draw_rect(150,100,50,10)
#G
draw_rect(150,160,50,10)



while running:
screen.fill((148,241,251))

for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

key = pygame.key.get_pressed()
if key[pygame.K_q]:
run = False
sys.exit()

digits()
pygame.display.update()

**请在评论中告诉我你喜欢我的想法吗?并且提问的格式是否正确。 **

最佳答案

你不需要final_data根本。只写一个函数draw_digit :

def draw_digit(surf, color, i):
for j, on in enumerate(binaries[i]):
if on:
pygame.draw.rect(surf, color, positions[j])
并这样称呼它:
draw_digit(screen, "black", 0)

添加额外的 offset连续显示多个数字的参数:
def draw_digit(surf, color, offset, i):
for j, on in enumerate(binaries[i]):
if on:
pygame.draw.rect(surf, color, pygame.Rect(positions[j]).move(offset, 0))
draw_digit(screen, "black", 0, 2)
draw_digit(screen, "black", 100, 3)


最小的例子:
repl.it/@Rabbid76/PyGame-7SegementDisplay

数据文件
binaries = ([1,1,1,1,1,1,0],[1,1,0,0,0,0,0],[1,0,1,1,0,1,1],[1,1,1,0,0,1,1],
[1,1,0,0,1,0,1],[0,1,1,0,1,1,1],[0,1,1,1,1,0,1],[1,1,0,0,0,1,0],[1,1,1,1,1,1,1],[1,1,1,0,1,1,1])

positions = ([200,110,10,50],[200,170,10,50],[150,220,50,10],[140,170,10,50],[140,110,10,50],[150,100,50,10],[150,160,50,10])
函数.py
import pygame, sys
from data import *

pygame.init()

size = width,height = 400,400
run = True
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()

def draw_digit(surf, color, offset, i):
for j, on in enumerate(binaries[i]):
if on:
pygame.draw.rect(surf, color, pygame.Rect(positions[j]).move(offset, 0))

count = 0
while run:
clock.tick(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

screen.fill((148,241,251))
draw_digit(screen, "black", 0, count // 10)
draw_digit(screen, "black", 100, count % 10)
count += 1
if count >= 100:
count = 0
pygame.display.update()

关于python - 在列表中添加矩形以在 pygame 中以 7 段数字显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66533451/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com