1

I've just downloaded python 3.3.2 and pygame-1.9.2a0.win32-py3.3.msi. I have decided to try a few tutorials on youtube and see if they work.

I have tried thenewboston's 'Game Development Tutorial - 2 - Basic Pygame Program' to see if it works. It is supposed to produce a black background and a ball that is the mouse (or so i think). It comes up with a syntax error when i try to run it, if i delete it it just produces a black pygame window. Here is the code:

bgg="bg.jpg"
ball="ball.png"

import pygame, sys
from pygame.locals import *

pygame.init()
screen=pygame.display.set_mode((540,341),0,32)

background=pygame.image.load(bgg).convert()
mouse_c=pygame.image.load(ball).convert_alpha()

while True:
    for event in pygame.event.get():
        if event.type ==QUIT:
            pygame.quit()
            sys.exit()

    screen.blit(background), (0,0))

The screen.blit(bakcgorund, (0,0)) command is the problem, when it comes up with the syntax error it highlights the second bracket on the furthest right of the command. If I delete it it just shows a black pygame window. can anyone help me?

2 Answers 2

1

I updated your code:

import pygame
from pygame.locals import * 
#about: pygame boilerplate

class GameMain():
    # handles intialization of game and graphics, as well as game loop
    done = False

    def __init__(self, width=800, height=600):
        """Initialize PyGame window.

        variables:
            width, height = screen width, height
            screen = main video surface, to draw on

            fps_max = framerate limit to the max fps
            limit_fps = boolean toggles capping FPS, to share cpu, or let it run free.
            now = current time in Milliseconds. ( 1000ms = 1second)
        """
        pygame.init()

        # save w, h, and screen
        self.width, self.height = width, height
        self.screen = pygame.display.set_mode(( self.width, self.height ))
        pygame.display.set_caption( "pygame tutorial code" )        

        self.sprite_bg = pygame.image.load("bg.jpg").convert()
        self.sprite_ball = pygame.image.load("ball.png").convert_alpha()


    def main_loop(self):
        """Game() main loop."""
        while not self.done:
            self.handle_events()        
            self.update()
            self.draw()

    def draw(self):
        """draw screen"""
        self.screen.fill(Color('darkgrey'))

        # draw your stuff here. sprites, gui, etc....        
        self.screen.blit(self.sprite_bg, (0,0))
        self.screen.blit(self.sprite_ball, (100,100))


        pygame.display.flip()

    def update(self):
        """physics/move guys."""
        pass

    def handle_events(self):
        """handle events: keyboard, mouse, etc."""
        events = pygame.event.get()
        kmods = pygame.key.get_mods()

        for event in events:
            if event.type == pygame.QUIT:
                self.done = True
            # event: keydown
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE: self.done = True

if __name__ == "__main__":         
    game = GameMain()
    game.main_loop()    
Sign up to request clarification or add additional context in comments.

Comments

1

Your parenthesis are unbalanced; there are 2 opening parenthesis, and 3 closing parenthesis; that is one closing parenthesis too many:

screen.blit(background), (0,0))
#     -----^    ------^    ---^ 

You probably want to remove the closing parenthesis after background:

screen.blit(background, (0,0))

5 Comments

There is still no ball after I have changed it. Is it a problem with the pictures or some other part of the code?- the pics were done in paint, the bg is black and the ball is a red ball with a black background.
Why don't you read the tutorial and see what you are supposed to do with mouse_c? All your code does right now is blit background at position 0, 0.. You don't do anything with the ball image apart from loading it and converting the alpha channel.
What I have is all he shows on the tutorial, what i've figured out is the background also doesn't appear, is it my python version or something? I do know pygame doesn't work as well in pytho version 3 as it does earlier ones
Also, can you recommend any tutorials that would be useful?
Sorry, I haven't looked into tutorials for pygame in ages.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.