Python Forum
Pygame in 90 Minutes - For Beginners syntax issues
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pygame in 90 Minutes - For Beginners syntax issues
#1
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.
buran write Jun-07-2023, 09:45 AM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply
#2
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()
Reply
#3
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()
GoldenHarvey likes this post
99 percent of computer problems exists between chair and keyboard.
Reply
#4
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"
GoldenHarvey likes this post
Reply
#5
(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?
Reply
#6
IDLE is fine for what you are doing, but you are using it the wrong way. Look for a tutorial video.
GoldenHarvey likes this post
Reply
#7
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Beginners to share pygame project? michael1789 4 3,018 Dec-04-2019, 07:17 AM
Last Post: michael1789
  [PyGame] Python pygame hangs up after 40 minutes chileflora 10 4,578 Jul-28-2019, 01:30 AM
Last Post: chileflora
  Beginners question Youmanity 4 3,644 Apr-08-2018, 07:02 PM
Last Post: Youmanity

Forum Jump:

User Panel Messages

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