Python Forum
Pygame in 90 Minutes - For Beginners syntax issues - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: Pygame in 90 Minutes - For Beginners syntax issues (/thread-40124.html)



Pygame in 90 Minutes - For Beginners syntax issues - GoldenHarvey - Jun-06-2023

I am watching and copying the methods of how to make a game on Pygame via the video "Pygame in 90 Minutes - For Beginners" by Tech with Tim.

I wrote this code in idel

Python 3.11.3 (tags/v3.11.3:f3909b8, Apr 4 2023, 23:49:59) [MSC v.1934 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import pygame
pygame 2.4.0 (SDL 2.26.4, Python 3.11.3)
Hello from the pygame community. https://www.pygame.org/contribute.html
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
def main():
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run=False
                pygame.quit()
                if _name_ == "_main_":
                    Main()
Based on his work it should have opened up a blank window.

I got an invalid syntax message instead.

I am wondering what went wrong,

Thank you.


RE: Pygame in 90 Minutes - For Beginners syntax issues - deanhystad - Jun-07-2023

Your indentation is incorrect. In Python, indenting is syntax. The way your program is indented, the following is part of the "for event:" loop.
if _name_ == "_main_":
    Main()



RE: Pygame in 90 Minutes - For Beginners syntax issues - Windspar - Jun-08-2023

Just a few typos. This is mainly use for testing.
# It double underscore.
if __name__ == '__main__':
    # It lower case main to match your function.
    main()
Example
import pygame

def main():
    # Active all pygame modules.
    pygame.init()
    surface = pygame.display.set_mode((900, 500))
    # Get the area of the surface as a rect.
    rect = surface.get_rect()
    clock = pygame.time.Clock()
    fps = 60

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

        surface.fill('black')
        # Render surface to screen.
        pygame.display.flip()
        # Let the computer do other jobs or rest.
        clock.tick(fps)

    pygame.quit()

main()



RE: Pygame in 90 Minutes - For Beginners syntax issues - deanhystad - Jun-08-2023

You should write programs in a file instead typing them in the Python interactive interpreter.

This is what you file would look like. I fixed and commented some errors/inconcistencies.
import pygame

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))

# Should have 1 or two blank lines before a function
def main():
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False  # Should have 1 space before and after =
    pygame.quit()

# You had an indentation error
if __name__ == "__main__":   # Double underscores.  You had single
    main()  # Function name is lower case.  You called Main()
If the file is named "mypygame.py" you run the file like this:
Output:
python mypygame.py' pygame 2.1.3.dev8 (SDL 2.0.22, Python 3.11.1) Hello from the pygame community. https://www.pygame.org/contribute.html
Depending on what OS you are using, you might have to type "python3 mypygame,py"


RE: Pygame in 90 Minutes - For Beginners syntax issues - GoldenHarvey - Jun-08-2023

(Jun-08-2023, 11:34 AM)deanhystad Wrote: You should write programs in a file instead typing them in the Python interactive interpreter.

This is what you file would look like. I fixed and commented some errors/inconcistencies.
import pygame

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))

# Should have 1 or two blank lines before a function
def main():
    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False  # Should have 1 space before and after =
    pygame.quit()

# You had an indentation error
if __name__ == "__main__":   # Double underscores.  You had single
    main()  # Function name is lower case.  You called Main()
If the file is named "mypygame.py" you run the file like this:
Output:
python mypygame.py' pygame 2.1.3.dev8 (SDL 2.0.22, Python 3.11.1) Hello from the pygame community. https://www.pygame.org/contribute.html
Depending on what OS you are using, you might have to type "python3 mypygame,py"

Thank you, I learned that Idel may not the best means to write code. I know this may be a dumb question but should I use a text document as a file or something else?


RE: Pygame in 90 Minutes - For Beginners syntax issues - deanhystad - Jun-14-2023

IDLE is fine for what you are doing, but you are using it the wrong way. Look for a tutorial video.


RE: Pygame in 90 Minutes - For Beginners syntax issues - KeeleyGreenfelder - Dec-18-2023

Tech with Tim extensively explores all facets of game production via Pygame, including configuring the game window, managing user input, handling game objects, implementing game logic, and designing graphic elements. By actively participating in the tutorial and coding in parallel with the video, you can acquire practical experience and a more comprehensive comprehension of Pygame's functionality