Adding rects in list for displaying in 7-segment digits in pygame

Viewed 121

I had two python files one for all the data that made previously and the other file of functions to display the data properly on screen using pygame.

--Functioning--

My idea is that the display of the segments is named alphabetically A,B,C,D,E,F,G PLZ refer to this image

Here, till now every segment is displaying but if you see in my data.py code you can see there is a dictionary called 'final_data' where each number has it's binary code which simply means "How many segments had to glow..." Note:- '1' means to glow and '0' means to not glow, you can say these are buttons. each button has a list of [x,y,width,height] to get display on the screen. I also give the binaries and there positions. The dictionary is working properly the problem is in it's implementation in my second file 'functions.py'.

--Code--

My first file is data.py and the code is......

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
"""

My second file 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()

**please tell me in comments do you like my idea? and is the format of asking question is correct. **

1 Answers

You don't need final_data at all. Just write a function draw_digit:

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

And call it like this:

draw_digit(screen, "black", 0)

Add an additional offset argument to display multiple digits in a row:

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)


Minimal example:

repl.it/@Rabbid76/PyGame-7SegementDisplay

data.py

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])

functions.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()
Related