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()