Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1269

Python • Trouble Adapting Pygame Code for Portrait Mode

$
0
0
Hello everyone,

I'm currently working on a project involving a Raspberry Pi and Pygame for a touchscreen interface. I've encountered a challenge while trying to adapt my code to function properly in portrait mode. I've made adjustments to the code, but I'm facing difficulties with the layout and orientation of elements on the screen.

Issue:
The code I've written works well in landscape mode. However, when I switch to portrait mode, the layout becomes distorted, and the elements are not properly rotated or positioned on the screen. Specifically, the numbers for the pin code interface appear in the wrong order and are positioned at the top of the screen instead of being rotated for readability in portrait mode.

Attempts:
I've tried using pygame.transform.rotate() to rotate the elements such as text and buttons, but it didn't resolve the issue entirely. The numbers still appear in the wrong order, and the layout remains inconsistent.

Code:
Below is the relevant portion of the code that I'm working with:
Working landscape mode code

Code:

import pygameimport sysimport RPi.GPIO as GPIOfrom mfrc522 import SimpleMFRC522# Pygame-initialisatie for the touchscreen initialisationpygame.init()screen_width, screen_height = 800, 480screen = pygame.display.set_mode((screen_width, screen_height))pygame.display.set_caption('Toegangscontrole HUD')# ColorsZWART = (0, 0, 0)WIT = (255, 255, 255)GROEN = (0, 255, 0)ROOD = (255, 0, 0)BLAUW = (0, 0, 255)GRIJS = (169, 169, 169)# RFID-initialisationreader = SimpleMFRC522()def lees_rfid():    try:        print("Hold the card next to the reader: ")        id, _ = reader.read()        return str(id)    except KeyboardInterrupt:        GPIO.cleanup()        sys.exit()# Function to display the HUD with the pincode interfacedef toon_hud():    global pincode  # Declare pincode as a global variable    pincode = ""    invoer_klaar = False    font = pygame.font.Font(None, 24)    block_font = pygame.font.Font(None, 48)    clock = pygame.time.Clock()    while not invoer_klaar:        for event in pygame.event.get():            if event.type == pygame.QUIT:                pygame.quit()                sys.exit()            elif event.type == pygame.MOUSEBUTTONDOWN:                x, y = event.pos                if 50 <= x <= 150 and 150 <= y <= 250:                    pincode += "1"                elif 150 <= x <= 250 and 150 <= y <= 250:                    pincode += "2"                elif 250 <= x <= 350 and 150 <= y <= 250:                    pincode += "3"                elif 50 <= x <= 150 and 250 <= y <= 350:                    pincode += "4"                elif 150 <= x <= 250 and 250 <= y <= 350:                    pincode += "5"                elif 250 <= x <= 350 and 250 <= y <= 350:                    pincode += "6"                elif 50 <= x <= 150 and 350 <= y <= 450:                    pincode += "7"                elif 150 <= x <= 250 and 350 <= y <= 450:                    pincode += "8"                elif 250 <= x <= 350 and 350 <= y <= 450:                    pincode += "9"                elif 350 <= x <= 450 and 250 <= y <= 350:                    invoer_klaar = controleer_pincode(pincode)                    if invoer_klaar:                        weergeef_resultaat(GROEN, "Toegang vrijgegeven")                    else:                        weergeef_resultaat(ROOD, "Pincode of RFID onjuist, geen toegang")                elif 450 <= x <= 550 and 350 <= y <= 450:  # Aangepaste positie voor Delete-knop                    pincode = pincode[:-1]        screen.fill(WIT)        tekst_wi = block_font.render("WI", True, BLAUW)        screen.blit(tekst_wi, (20, 20))        tekst_tec = block_font.render("TEC", True, GRIJS)        screen.blit(tekst_tec, (65, 20))        for i in range(1, 10):            x = 50 + (i - 1) % 3 * 100            y = 150 + (i - 1) // 3 * 100            tekst = font.render(str(i), True, ZWART)            screen.blit(tekst, (x + 20, y + 20))        pygame.draw.rect(screen, ZWART, (350, 250, 100, 100))        tekst_enter = font.render("Enter", True, WIT)        screen.blit(tekst_enter, (375, 300))        pygame.draw.rect(screen, ZWART, (450, 350, 100, 100))  # Aangepaste positie voor Delete-knop        tekst_delete = font.render("Delete", True, WIT)  # Aangepaste kleur        screen.blit(tekst_delete, (470, 400))  # Aangepaste positie voor Delete-knop        pincode_tekst = font.render("Voer pincode in: {}".format(pincode[-4:]), True, ZWART)        screen.blit(pincode_tekst, (50, 100))        pygame.display.flip()        clock.tick(30)# Function to display the result ( GROEN = GREEN when the correct pincode is entered, RED = Rood when the incorrect pincode is entered, after displaying this message you go back to the home screen/ main HUD after a while)def weergeef_resultaat(kleur, tekst):    global pincode  # Declare pincode as a global variable     screen.fill(WIT)    if kleur == GROEN:        pygame.draw.line(screen, kleur, (300, 250), (330, 280), 5)        pygame.draw.line(screen, kleur, (330, 280), (380, 230), 5)    elif kleur == ROOD:        pygame.draw.line(screen, kleur, (300, 230), (380, 310), 5)        pygame.draw.line(screen, kleur, (300, 310), (380, 230), 5)    pygame.draw.rect(screen, kleur, (400, 250, 400, 100))    font = pygame.font.Font(None, 24)    tekstoppervlak = font.render(tekst, True, WIT)    tekstrect = tekstoppervlak.get_rect(center=(600, 300))    screen.blit(tekstoppervlak, tekstrect)    pygame.display.flip()    pygame.time.delay(5000)    # Reset pincode and update the screen    pincode = ""    toon_hud()def controleer_pincode(pincode):    return pincode[-4:] == "1234"  def controleer_rfid(kaart_id):    toegestane_rfid_ids = ["123456789", "987654321"]    return kaart_id in toegestane_rfid_idstry:    toon_hud()    while True:        for event in pygame.event.get():            if event.type == pygame.QUIT:                pygame.quit()                sys.exit()except KeyboardInterrupt:    pygame.quit()
almost working code in portrait mode that almost works as intended:

Code:

import pygameimport sysimport RPi.GPIO as GPIOfrom mfrc522 import SimpleMFRC522# Pygame initialization for the touchscreenpygame.init()screen_width, screen_height = 480, 800  # Switched width and height for portrait modescreen = pygame.display.set_mode((screen_width, screen_height))pygame.display.set_caption('Toegangscontrole HUD')# ColorsBLACK = (0, 0, 0)WHITE = (255, 255, 255)GREEN = (0, 255, 0)RED = (255, 0, 0)BLUE = (0, 0, 255)GRAY = (169, 169, 169)# RFID initializationreader = SimpleMFRC522()def lees_rfid():    try:        print("Hold the card next to the reader:")        id, _ = reader.read()        return str(id)    except KeyboardInterrupt:        GPIO.cleanup()        sys.exit()# Function to display the HUD with pin code interfacedef toon_hud():    global pincode    pincode = ""    invoer_klaar = False    font = pygame.font.Font(None, 24)    block_font = pygame.font.Font(None, 48)    clock = pygame.time.Clock()    while not invoer_klaar:        for event in pygame.event.get():            if event.type == pygame.QUIT:                pygame.quit()                sys.exit()            elif event.type == pygame.MOUSEBUTTONDOWN:                x, y = event.pos                if 50 <= x <= 150 and 50 <= y <= 150:  # Adjusted button positions for portrait mode                    pincode += "1"                elif 50 <= x <= 150 and 150 <= y <= 250:                    pincode += "2"                elif 50 <= x <= 150 and 250 <= y <= 350:                    pincode += "3"                elif 150 <= x <= 250 and 50 <= y <= 150:                    pincode += "4"                elif 150 <= x <= 250 and 150 <= y <= 250:                    pincode += "5"                elif 150 <= x <= 250 and 250 <= y <= 350:                    pincode += "6"                elif 250 <= x <= 350 and 50 <= y <= 150:                    pincode += "7"                elif 250 <= x <= 350 and 150 <= y <= 250:                    pincode += "8"                elif 250 <= x <= 350 and 250 <= y <= 350:                    pincode += "9"                elif 350 <= x <= 450 and 50 <= y <= 150:                    invoer_klaar = controleer_pincode(pincode)                    if invoer_klaar:                        weergeef_resultaat(GREEN, "Toegang vrijgegeven")                    else:                        weergeef_resultaat(RED, "Pincode of RFID onjuist, geen toegang")                elif 350 <= x <= 450 and 150 <= y <= 250:                      pincode = pincode[:-1]        screen.fill(WHITE)        # Rotate background        rotated_background = pygame.transform.rotate(screen, 90)        screen.blit(rotated_background, (0, 0))        # Render text with rotation        tekst_wi = block_font.render("WI", True, BLUE)        tekst_wi = pygame.transform.rotate(tekst_wi, 90)        screen.blit(tekst_wi, (20, 20))        tekst_tec = block_font.render("TEC", True, GRAY)        tekst_tec = pygame.transform.rotate(tekst_tec, 90)        screen.blit(tekst_tec, (65, 20))        # Rotate buttons        buttons = []        for i in range(1, 10):            x = 50 + (i - 1) // 3 * 100            y = 50 + (i - 1) % 3 * 100            button = pygame.draw.rect(screen, BLACK, (x, y, 100, 100))            buttons.append(button)            tekst = font.render(str(i), True, WHITE)            tekst = pygame.transform.rotate(tekst, 90)            screen.blit(tekst, (x + 20, y + 20))        pygame.draw.rect(screen, BLACK, (350, 50, 100, 100))        tekst_enter = font.render("Enter", True, WHITE)        tekst_enter = pygame.transform.rotate(tekst_enter, 90)        screen.blit(tekst_enter, (375, 100))        pygame.draw.rect(screen, BLACK, (350, 150, 100, 100))          tekst_delete = font.render("Delete", True, WHITE)          tekst_delete = pygame.transform.rotate(tekst_delete, 90)          screen.blit(tekst_delete, (370, 200))        pincode_tekst = font.render("Voer pincode in: {}".format(pincode[-4:]), True, WHITE)        pincode_tekst = pygame.transform.rotate(pincode_tekst, 90)        screen.blit(pincode_tekst, (50, 400))        pygame.display.flip()        clock.tick(30)# Function to display the result (green checkmark or red cross with corresponding text)def weergeef_resultaat(kleur, tekst):    global pincode    screen.fill(WHITE)    if kleur == GREEN:        pygame.draw.line(screen, kleur, (300, 250), (330, 280), 5)        pygame.draw.line(screen, kleur, (330, 280), (380, 230), 5)    elif kleur == RED:        pygame.draw.line(screen, kleur, (300, 230), (380, 310), 5)        pygame.draw.line(screen, kleur, (300, 310), (380, 230), 5)    pygame.draw.rect(screen, kleur, (50, 500, 400, 100))    font = pygame.font.Font(None, 24)    tekstoppervlak = font.render(tekst, True, WHITE)    tekstrect = tekstoppervlak.get_rect(center=(250, 550))    screen.blit(tekstoppervlak, tekstrect)    pygame.display.flip()    pygame.time.delay(5000)    # Reset pincode and update the screen    pincode = ""    toon_hud()# Function to check the pincodedef controleer_pincode(pincode):    return pincode[-4:] == "1234"# Function to check RFIDdef controleer_rfid(kaart_id):    toegestane_rfid_ids = ["123456789", "987654"]    return kaart_id in toegestane_rfid_idstry:    toon_hud()    while True:        for event in pygame.event.get():            if event.type == pygame.QUIT:                pygame.quit()                sys.exit()except KeyboardInterrupt:    pygame.quit()
If any extra information is necessary about the problem itself, or if you need any translation/extra explanation which function does what exactly since it probably is in my native language, please feel free to ask about it and i will provide it as soon as possible to the best of my capabilities.

Thanks in advance,

Mathon

Statistics: Posted by Mathon2003 — Mon Mar 25, 2024 12:06 pm



Viewing all articles
Browse latest Browse all 1269

Trending Articles