Python Forum
[PyGame] I've been programming in a vacuum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] I've been programming in a vacuum
#1
I have been developing a program to emulate the terminal operating system from the fallout universe, I aim to make it a fully functional interface adding a full range of features that the games do not provide because it is simply a minigame. I have been tracking my progress in video logs that can be found HERE.

This started as a school project for developing something for raspberry pi3, but has kind of become a hobby. I plan on fleshing the project out as much as I can, but I never really expected the project to grow to the scale it is at. I am capable of continuing to develop the project, but i'm fearful due to my own inexperience that the project may need to be reorganized before I continue.

I have sort of been organizing things as I go, but the shape has sort of changed based on necessity when I hit walls, and I'm not confident that lots of sections are cleanly implemented. Whenever something breaks I try to iron it out until I can't break it anymore, but that doesn't mean it's fixed just patched until "I" (an inexperienced python programmer) can no longer find a bug. Needless to say, I think the code is unoptimised, full of leaks, and probably not even working the way I think it is.

If someone would be kind enough to take a look at my code so I can try to keep this project from becoming a trainwreck I would appreciate it greatly
Reply
#2
we need a link to your project on github or where ever it is.
Recommended Tutorials:
Reply
#3
Here is the link to a dropbox with the current version
https://www.dropbox.com/sh/vncqyhbmvehq0...wye1a?dl=0
Reply
#4
The first thing i see is the horrendous main file at over 22 hundred lines long. This should definitely be split more organized.

All the static data, such as loading sounds, databases, image loads, and similar should go into some setting/tools file.

def update_volume():

    for i in range(len(sound_typing_multiple)):
        sound_typing_multiple[i].set_volume(volume_typing)
    for i in range(len(sound_typing)):
        sound_typing[i].set_volume(volume_typing)
    for i in range(len(sound_enter)):
        sound_enter[i].set_volume(volume_typing)
This can be "optimized" into more readability by not using range(len) in a for loop as explained here
https://python-forum.io/Thread-Basic-Nev...n-sequence

Another thing i see in main is massive long if/elif structures. This can be better organized by dictionaries. Your one if structure is over 800 lines long. Here is an example that explains it more
https://python-forum.io/Thread-Text-Adve...l?pid=1311

You can remove a lot of unneeded code and organize it better by using classes. You wont need globals all over your program, and massive list of them in a function such as in here
Quote:def user_interaction(terminal_in, x, y):
global backdoor_progress, settings, terminateDisplay, log_status, logged_user, input_visible, mode, root, header, color, menu_elements, user_input
Recommended Tutorials:
Reply
#5
(Jan-16-2018, 04:38 AM)metulburr Wrote: All the static data, such as loading sounds, databases, image loads, and similar should go into some setting/tools file.

Should all the static data be in one file or split into categorical separate files?
Also do I just import these files or is there a more pythonic way to get these values back into the main file?

(Jan-16-2018, 04:38 AM)metulburr Wrote: This can be "optimized" into more readability by not using range(len) in a for loop as explained here
When I changed the code it threw an error saying "TypeError: list indices must be integers or slices, not Sound"

    for i in sound_typing_multiple:
        sound_typing_multiple[i].set_volume(volume_typing)

I'm also not sure how to change my huge if/elif tree into a dictionary. it is working as a lookup table, but it runs the code block that is looked up...I'm not sure how to implement this with a dictionary. My only thought would be to assign the key to the command, and return a command ID that then gets put into an interpreter function in a new file that is again a series of if/elif that instead take the ID as the condition. I know by just reading that, this is clearly not the right step to take, though putting the command handler in a separate file would clean up the main file
Reply
#6
(Jan-16-2018, 05:00 AM)Rabbyte Wrote: Should all the static data be in one file or split into categorical separate files?
Also do I just import these files or is there a more pythonic way to get these values back into the main file?
I would start with 1 file for simplicity. I would say the main goal for now should be breaking up the main file. You can do that later depending on how complex that part is after.
(Jan-16-2018, 05:00 AM)Rabbyte Wrote: When I changed the code it threw an error saying "TypeError: list indices must be integers or slices, not Sound"
for i in sound_typing_multiple:
    sound_typing_multiple[i].set_volume(volume_typing)
it would be roughly this
for sound in sound_typing_multiple:
    sound.set_volume(volume_typing)
(Jan-16-2018, 05:00 AM)Rabbyte Wrote: I'm also not sure how to change my huge if/elif tree into a dictionary. it is working as a lookup table, but it runs the code block that is looked up...I'm not sure how to implement this with a dictionary. My only thought would be to assign the key to the command, and return a command ID that then gets put into an interpreter function in a new file that is again a series of if/elif that instead take the ID as the condition. I know by just reading that, this is clearly not the right step to take, though putting the command handler in a separate file would clean up the main file
I think its harder to rearrange a program than it is to do it from scratch. So its going to be a lot of work to rearrange the program a different way.

Note: A dictionary key can also be a function, callable at any time (minus the parenthesis that call it within the dictionary value). This function would contain whatever is inside the main if/elif blocks. If it is another long if/elif inside that, then it would be a nested key/value pair for that if/elif structure. And so on and so on. So at the very worst you could spit the main if/elif into a dictionary, and everything nested in a function. Then mesh those functions in the dictionary after slowly.

Your dictionary then would become the "lookup table" as you put it.

Im not sure what else to say. That tutorial's example puts it very well on how you would execute from the dictionary. However the more nested you have the more nested dictionary you are going to have.
Quote:
    if command in directions:
        if command in current_room:
            current_room = rooms[current_room[command]]
        else:
            # bad movement
            print("You can't go that way.")

The alternative is what you have.
    if command.lower() in ('n', 'north'):
        if current_room == 'empty':
            current_room = 'temple'
        elif current_room == 'bedroom':
            current_room = 'torture'
        else:
            print("You can't go that way.")
    elif command.lower() in ('s', 'south'):
        if current_room == 'temple':
            current_room = 'empty'
        elif current_room == 'torture':
            current_room = 'bedroom'
        else:
            print("You can't go that way.")
    elif command.lower() in ('e', 'east'):
        if current_room == 'empty':
            current_room = 'bedroom'
        elif current_room == 'temple':
            current_room = 'torture'
        else:
            print("You can't go that way.")
    elif command.lower() in ('w', 'west'):
        if current_room == 'bedroom':
            current_room = 'empty'
        elif current_room == 'torture':
            current_room = 'temple'
        else:
            print("You can't go that way.")
    # quit game
    elif command.lower() in ('q', 'quit'):
        break
    # bad command
    else:
        print("I don't understand that command.")
This part is so repetitive that you could just instead use a function to handle all this and plug in the differences.
Quote:
def event_handler():
    global terminateDisplay, user_input, terminal_input
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            terminateDisplay = True

        elif event.type == pygame.KEYDOWN:

            if event.key == pygame.K_0 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += ")"

                else:
                    user_input += "0"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_1 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "!"

                else:
                    user_input += "1"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_2 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "@"

                else:
                    user_input += "2"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_3 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "#"

                else:
                    user_input += "3"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_4 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "$"

                else:
                    user_input += "4"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_5 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "%"

                else:
                    user_input += "5"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_6 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "^"

                else:
                    user_input += "6"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_7 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "&"

                else:
                    user_input += "7"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_8 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "*"

                else:
                    user_input += "8"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_9 and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "("

                else:
                    user_input += "9"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_a and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "A"

                else:
                    user_input += "a"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_b and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "B"

                else:
                    user_input += "b"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_c and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "C"

                else:
                    user_input += "c"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_d and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "D"

                else:
                    user_input += "d"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_e and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "E"

                else:
                    user_input += "e"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_f and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "F"

                else:
                    user_input += "f"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_g and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "G"

                else:
                    user_input += "g"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_h and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "H"

                else:
                    user_input += "h"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_i and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "I"

                else:
                    user_input += "i"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_j and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "J"

                else:
                    user_input += "j"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_k and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "K"

                else:
                    user_input += "k"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_l and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "L"

                else:
                    user_input += "l"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_m and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "M"

                else:
                    user_input += "m"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_n and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "N"

                else:
                    user_input += "n"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_o and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "O"

                else:
                    user_input += "o"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_p and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "P"

                else:
                    user_input += "p"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_q and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "Q"

                else:
                    user_input += "q"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_r and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "R"

                else:
                    user_input += "r"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_s and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "S"

                else:
                    user_input += "s"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_t and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "T"

                else:
                    user_input += "t"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_u and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "U"

                else:
                    user_input += "u"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_v and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "V"

                else:
                    user_input += "v"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_w and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "W"

                else:
                    user_input += "w"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_x and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "X"

                else:
                    user_input += "x"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_y and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "Y"

                else:
                    user_input += "y"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_z and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "Z"

                else:
                    user_input += "z"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_SLASH and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "?"

                else:
                    user_input += "/"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_BACKSLASH and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "|"

                else:
                    user_input += "\\"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_COMMA and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "<"

                else:
                    user_input += ","

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_PERIOD and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += ">"

                else:
                    user_input += "."

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_SEMICOLON and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += ":"

                else:
                    user_input += ";"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_QUOTE and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += '"'

                else:
                    user_input += "'"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_BACKQUOTE and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "~"

                else:
                    user_input += "`"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_LEFTBRACKET and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "{"

                else:
                    user_input += "["

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_RIGHTBRACKET and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "}"

                else:
                    user_input += "]"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_EQUALS and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "+"

                else:
                    user_input += "="

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_MINUS and len(user_input) < 1045:

                if pygame.key.get_mods() & pygame.KMOD_SHIFT:
                    user_input += "_"

                else:
                    user_input += "-"

                sound_typing[random.randint(0, 4)].play()

            elif event.key == pygame.K_BACKSPACE and len(user_input) > 0:

                user_input = user_input[:-1]
                sound_enter[random.randint(0, 2)].play()

            elif event.key == pygame.K_SPACE and len(user_input) < 1045:

                user_input += " "
                sound_enter[random.randint(0, 2)].play()

            elif event.key == pygame.K_RETURN and len(user_input) > 0:

Also while looking at your code more i found another repetition that should be removed. You have what i call "sentdex syndrome". Which is basically numerous event loops / updates instead of having one single event loop and update to handle states. It gets repetitive and confusing and makes it spaghetti code. Here is an example of what i mean.
https://python-forum.io/Thread-PyGame-wa...-tutorials

Also you should never ever have a time sleep in a GUI program. Even if it is a replication of a terminal. Time sleep pauses everything. You should use a GUI timer instead.
Recommended Tutorials:
Reply
#7
Yes I do believe you need to start from scratch.
Never use python time module in pygame. It stop the world.
pygame has ticks and timers.

To get most special keys. I do this.
if 32 <= event.key < 123:
    buffer.append(str(event.unicode))
example. My first attempt for message_step_through.
import pygame
import random

messages = {'intro':
"""The ROBCO Model E-330 is the
most reliable client terminal ever developed.
Finally, a personalized home computer
for the whole family to enjoy!

        Keep track of activities and tasks,
        and maintain privacy
        by adding passwords to logins.*

        When connnected to a ROBCO-brand Mainframe,
        stay organized and on the ball with our
                - state-of-the-art calendar
                - contact manager
                - messaging system!*

* No users detected. Type "SETUP" to create an account.
* No network connection."""
}

class Ticker:
    def __init__(self, ticks, length):
        self.next_tick = ticks + length
        self.length = length

    def elapse(self, ticks):
        if ticks > self.next_tick:
            self.next_tick += self.length
            return True
        return False

class Screen:
    WIDTH = 800
    HEIGHT = 600

    def __init__(self):
        pygame.init()
        pygame.display.set_caption("Scanlines")
        self.screen = pygame.display.set_mode(self.get_size())
        self.clock = pygame.time.Clock()
        self.running = False
        self.font = pygame.font.Font(None, 24)

        self.create_scanline()
        self.lines_position = list(range(0, Screen.HEIGHT + 1, 200))
        self.scanline_ticker = Ticker(0, 2)
        self.scanline_speed = 10

        self.terminal = pygame.Surface((Screen.WIDTH - 100, Screen.HEIGHT - 50))
        self.terminal = self.terminal.convert_alpha()
        self.terminal_position = 50, 25

        self.message_box = self.terminal.copy()
        self.messages = messages['intro'].split('\n')
        self.message_ticker = Ticker(0, 80)
        self.message_letter = 1
        self.message_line = -1
        self.message_nextline = True

    def create_scanline(self):
        self.scanline = pygame.Surface((1, 200))
        self.scanline = self.scanline.convert_alpha()
        color = pygame.Color(0, 180, 0)
        color.a = 15
        for a in range(100):
            color.g -= 1
            self.scanline.set_at((0, a), color)
            self.scanline.set_at((0, 199 - a), color)

        self.scanline = pygame.transform.scale(self.scanline, (Screen.WIDTH, 200))

    def update_terminal(self, ticks):
        if self.messages:
            if self.message_ticker.elapse(ticks):
                if self.message_line > -1:
                    text = self.messages[self.message_line]
                    # Test to see if there anymore letters to be shown
                    if self.message_letter >= len(text):
                        self.message_nextline = True
                        self.message_letter = 0

                # Is text ready for next line
                if self.message_nextline:
                    self.message_nextline = False
                    self.message_line += 1
                    # check to see if anymore lines left
                    if self.message_line >= len(self.messages):
                        self.messages = None
                        self.message_line = -1
                    else:
                        text = self.messages[self.message_line]
                        text = self.font.render(text, 1, (11, 188, 12))
                        self.message_box.blit(text, (5, (5 + self.font.get_height()) * self.message_line))

                self.terminal.blit(self.message_box, (0,0))
                if self.messages:
                    text = self.messages[self.message_line]
                    size = self.font.size(text[self.message_letter:])
                    pos = self.font.size(text[:self.message_letter])
                    rect = pygame.Rect( pos[0] + 5,
                        (5 + self.font.get_height()) * self.message_line,
                        size[0] + 5, size[1])

                    # black bar
                    self.terminal.fill((0,0,0), rect)
                    self.message_letter += 1

    def update_scanlines(self, ticks):
        if self.scanline_ticker.elapse(ticks):
            for i in range(len(self.lines_position)):
                self.lines_position[i] -= self.scanline_speed
                if self.lines_position[i] < -199:
                    self.lines_position[i] = Screen.HEIGHT
                elif self.lines_position[i] > Screen.HEIGHT:
                    self.lines_position[i] = -200

        for line in self.lines_position:
            self.screen.blit(self.scanline, (0, line), None, pygame.BLEND_RGBA_MULT)
            self.screen.blit(self.scanline, (0, line), None)

    def draw(self):
        self.screen.fill((0,0,0))

        ticks = pygame.time.get_ticks()
        self.update_terminal(ticks)
        self.screen.blit(self.terminal, self.terminal_position)
        self.update_scanlines(ticks)

    def get_size(self):
        return Screen.WIDTH, Screen.HEIGHT

    def get_rect(self):
        return pygame.Rect(0,0,Screen.WIDTH,Screen.HEIGHT)

    def loop(self):
        self.running = True
        while self.running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False

            self.draw()
            pygame.display.flip()
            self.clock.tick(60)

        pygame.quit()

if __name__ == '__main__':
    screen = Screen()
    screen.loop()
99 percent of computer problems exists between chair and keyboard.
Reply
#8
So I'm remaking my overly large event handler and I have made the keylogging 1000% shorter, but how would you recommend I handle if shift is pressed? is there an easy way to handle this?

    def loop(self):
        user_input = ""
        self.running = True
        while self.running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_LSHIFT:
                        print("SHIFT")
                    elif event.key == pygame.K_SPACE:
                        user_input += " "
                    elif event.key == pygame.K_BACKSPACE:
                        user_input = user_input[:-1]
                    elif event.key == pygame.K_RETURN:
                        user_input = ""
                    else:
                        user_input += pygame.key.name(event.key)
                    print(user_input)
Reply
#9
str(event.unicode) will take care of shift.

event.unicode vs event.key
import pygame

class Screen:
    WIDTH = 800
    HEIGHT = 600

    def __init__(self):
        pygame.init()
        pygame.display.set_caption("keys")
        self.screen = pygame.display.set_mode(self.get_size())
        self.clock = pygame.time.Clock()
        self.running = False

        self.font = pygame.font.Font(None, 24)
        self.text = None
        self.text2 = None

    def draw(self):
        self.screen.fill((0,0,0))
        if self.text:
            self.screen.blit(self.text, (10,50))

        if self.text2:
            self.screen.blit(self.text2, (10,100))

    def get_size(self):
        return Screen.WIDTH, Screen.HEIGHT

    def get_rect(self):
        return pygame.Rect(0,0,Screen.WIDTH,Screen.HEIGHT)

    def loop(self):
        self.running = True
        while self.running:
            for event in pygame.event.get():
                if event.type == pygame.KEYDOWN:
                    self.text = self.font.render(pygame.key.name(event.key), 1, (200,0,0))
                    self.text2 = self.font.render(event.unicode, 1, (0,200,0))
                elif event.type == pygame.QUIT:
                    self.running = False

            self.draw()
            pygame.display.flip()
            self.clock.tick(60)

        pygame.quit()

if __name__ == '__main__':
    screen = Screen()
    screen.loop()
99 percent of computer problems exists between chair and keyboard.
Reply
#10
So let's see if I got the basic layout correct:
import pygame
from pygame.locals import *


class Screen:
    def __init__(self):
        self.running = True
        self.screen = None
        self.size = self.width, self.height = 512, 384

    def on_init(self):
        #print("Initializing")
        pygame.init()
        pygame.display.set_caption("Application")
        self.screen = pygame.display.set_mode(self.size, pygame.HWSURFACE)
        self.font = pygame.font.Font(None, 16)
        self.clock = pygame.time.Clock()

    def on_event(self, event):
        print("Events")
        if event.type == pygame.QUIT:
            self.running = False

    def on_loop(self):
        #print("Loop")
        pass

    def on_render(self):
        #print("Rendering")
        self.screen.fill((0, 0, 0))
        pygame.display.flip()

    def on_cleanup(self):
        #print("Cleaning")
        pygame.quit()

    def on_execute(self):
        if self.on_init() == False:
            self.running = False

        while (self.running):
            for event in pygame.event.get():
                self.on_event(event)
                print(event)
            self.on_loop()
            self.on_render()
            self.clock.tick(60)
        self.on_cleanup()


if __name__ == "__main__":
    screen = Screen()
    screen.on_execute()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020