Hi, I'm really new to both Pygame and Python. To get started, I just want to create a basic program of a sprite called "Player" in a background that responds to keyboard input. I pretty much just want to program it to move left and right in response to the corresponding arrow keys being pressed. HOWEVER, I've been trying so hard for so long, but all I get is just the image on the background. No response to any of my keyboard inputs. The worst part is that I'm probably making a small mistake somewhere that I just cant identify. I would be ever so grateful if someone could point out where I'm messing up, or maybe even post their code to a similar program. Here is my code (the 2 gif images can be anything you want):

import pygame, sys, os
pygame.init()

#Player Class, moves according to keyboard input
class Player:

#Player Constructor, takes arguments image and position
def __init__(self, image, x, y):
self.image= image
self.xPos=x
self.yPos=y
self.dir=1

#Updates changes
def update(self):
self.move()

#Moves right or left according to key input
def move(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
self.xPos+=15


#Jumps up for a limited time according to key input
def jump(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.yPos-=15
time.delay(1000)
self.ypos+=15

#Draws self
def draw(self):
screen.blit(self.image, (self.xPos, self.yPos))







screen = pygame.display.set_mode((400,315))
player= pygame.image.load('Player.gif').convert()
background= pygame.image.load('Background.gif').convert()
screen.blit(background, (0,0))


#Creates Player
o= Player(player, 200,200)
o.update()
o.draw()

#Updates Screen
pygame.display.update()

Fix your indentations(4 space)
Python will not work with wrong indentations.
Shall look like this.

def __init__(self, image, x, y):
    self.image= image
    self.xPos=x
    self.yPos=y
    self.dir=1

If you dont know this,read some basic lesson before you try pygame.
Like byte og python

Fix your indentations(4 space)
Python will not work with wrong indentations.
Shall look like this.

def __init__(self, image, x, y):
    self.image= image
    self.xPos=x
    self.yPos=y
    self.dir=1

If you dont know this,read some basic lesson before you try pygame.
Like byte og python

Sorry, the indentation just got messed up while i was copying and pasting the code. The indentation is NOT the problem. I'll edit it now though.If only it was as simple as that...

Wont let me edit the first post anymore...Anyway, here is my code with the right indentation. Sorry about that. Again, THE INDENTATION WAS NOT THE PROBLEM.

import pygame, sys, os
pygame.init()

#Player Class, moves according to keyboard input
class Player:

#Player Constructor, takes arguments image and position
    def __init__(self, image, x, y):
        self.image= image
        self.xPos=x
        self.yPos=y
        self.dir=1
       
#Updates changes
    def update(self):
        self.move()
       
#Moves right or left according to key input
    def move(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RIGHT:
                    self.xPos+=15
                if event.key == pygame.K_LEFT:
                    self.xPos-=15
                   
                   

     

#Jumps up for a limited time according to key input       
    def jump(self):
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    self.yPos-=15
                    time.delay(1000)
                    self.ypos+=15

#Draws self
    def draw(self):
        screen.blit(self.image, (self.xPos, self.yPos))


                   

screen = pygame.display.set_mode((400,315))
player= pygame.image.load('Player.gif').convert()
background= pygame.image.load('Background.gif').convert()
screen.blit(background, (0,0))


#Creates Player
o= Player(player, 200,200)
o.update()
o.draw()

     
#Updates Screen
pygame.display.update()
pygame.display.flip()

Here is one of the problems.
I copied your program into notepad (correct indentation) and made the two appropriate gif images. I save the problem as a python script and ran it. It opened, drew, and then closed. The reason for this is that you only run through one cycle of updating and drawing the screen. You need to make a 'while loop'. This keeps the statements inside the loop repeating (in the order that they are in) until the 'while' conditions are not met. Using 'while 1' will give you an infinite loop. Inside the loop, you have the statements you want to carry out, but at the end you need to pause your program for a few milliseconds so that it does not crash from running too quickly for your hardware.
Here is what you need to do. Fill the code in in the right places...it should be simple enough ;)

while 1:
	<Clear screen> #use screen.fill((RGB))
	<Update> #Call all update events for all sprites
	<Draw> # Call all draw events for all sprites and draw background.
	<Pause> # use pygame.time.wait(milliseconds). 10-20milliseconds should be good. Try more if not.

Drop the new code lines in place of the 'o.update' and 'o.draw' lines

Just ask if you need more help

This much I got running, the gifs were missing so I had to replace them with something from myself.

import pygame, sys, os

#Player Class, moves according to keyboard input
class Player:

#Player Constructor, takes arguments image and position
    def __init__(self, image, x, y):
        self.image= image
        self.xPos=x
        self.yPos=y
        self.dir=1
       
#Updates changes
    def update(self):
        self.move()
       
#Moves right, left or up according to key input
    def move(self):
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                self.xPos+=15
            elif event.key == pygame.K_LEFT:
                self.xPos-=15  
            elif event.key == pygame.K_UP:
                 self.yPos-=15 ## was ypos                
            elif event.key == pygame.K_DOWN:
                 self.yPos+=15
                 
            print 'Move',self.xPos,self.yPos ## DEBUG


#Draws self
    def draw(self):
        screen.blit(self.image, (self.xPos, self.yPos))

pygame.init()
screen = pygame.display.set_mode((400,315))
player= pygame.image.load('Player.gif').convert()
background= pygame.image.load('Background.gif').convert()

#Creates Player
o= Player(player, 200,200)

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

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

        o.update()
        o.draw()
             
        #Updates Screen
        pygame.display.update()
        pygame.display.flip()

Have you read at Pygame news? I cannot tell if they are going to abandon Pygame but they are heading on JS. If they are abandoning Python I would not bother learning it. I would look at other modules or Move to another language.

Even SDL is moving towards commercial. I can't understand where are thing heading. I'm not a gamer though...Just spectator

Have you read at Pygame news? I cannot tell if they are going to abandon Pygame but they are heading on JS. If they are abandoning Python I would not bother learning it. I would look at other modules or Move to another language.

pygame rewritten for javascript - Apr 1, 2010

The date give you a hint.

Thanks so much guys!!! A main loop is exactly what was missing.

hey bro! i just started pygame, but i have experience in python. between line 1 and 2, you need to tell pygame to set up the module that alows it to get the keyboard input. the module is: from pygame.locals import * this tells pygame to import everything from the keyboard, mouse, ect. hope this helped, Jason

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.