Python-Pygames-help.me
Python-Pygames-help.me

Reputation: 101

Pygame creating keyboard animation

   import pygame

pygame.init()

window = pygame.display.set_mode((800,600))

pygame.display.set_caption("TEST2")

black=(0,0,0)
white=(255,255,255)

moveX,moveY=0,0

clock = pygame.time.Clock()

class Sprite:

    def __init__(self,x,y):

        self.x=x

        self.y=y

        self.width=50

        self.height=50

        self.i1=pygame.image.load("Sprite0.PNG")

        self.i2 = pygame.image.load("Sprite1.PNG")

        self.i3 = pygame.image.load("Sprite2.PNG")

        self.i4 = pygame.image.load("Sprite3.PNG")

        self.i5 = pygame.image.load("Sprite4.PNG")

        self.i6 = pygame.image.load("Sprite5.PNG")

        self.i7 = pygame.image.load("Sprite6.PNG")

        self.i8 = pygame.image.load("Sprite7.PNG")

        self.i9 = pygame.image.load("Sprite8.PNG")

        self.i10 = pygame.image.load("Sprite9.PNG")

        self.i11 = pygame.image.load("Sprite10.PNG")

        self.i12 = pygame.image.load("Sprite11.PNG")

        self.timeTarget=10

        self.timeNum=0

        self.currentImage=0

    def update(self):
        self.timeNumber+=1

        if(self.timeNume==self.timeTarget):

            if (self.currentImage==0):

                self.currentImage+=1

            else:
                self.currentImage=0

            self.timeNum=0

        self.render()

    def render(self):

        if (self.currentImage==0):

            window.blit(self.i1, (self.x,self.y))

        else:

            window.blit(self.i2, (self.x,self.y))

player=Sprite(100,150)
gameLoop = True
while gameLoop:

    for event in pygame.event.get():

        if event.type==pygame.QUIT:

            gameLoop = False

        if (event.type==pygame.KEYDOWN):

            if (event.key==pygame.K_LEFT):

                moveX = -3

            if (event.key==pygame.K_RIGHT):

                moveX = 3

            if (event.key==pygame.K_UP):

                moveY = -3

        if (event.key==pygame.K_DOWN):

            moveY = 3

        if (event.type==pygame.KEYUP):

            if (event.key==pygame.K_LEFT):

                moveX=0

            if (event.key==pygame.K_RIGHT):

                moveX=0

            if (event.key==pygame.K_UP):

                moveY=0

            if (event.key==pygame.K_DOWN):

                moveY=0

    window.fill(black)

    player.x+=moveX

    player.x+=moveY

    player.update()

    clock.tick(50)

    pygame.display.flip()

pygame.quit()

So heres my problem I want to make a moving keyboard animation and when I click run not even a window pops up, Idle doesn't seem to have an errors with the code but nothing happens when I click run. if you see an error I have please tell me im trying to learn this whole pygame thing still and im quite new to python in general. Thanks!

edit: (I found the error I think but don't know how to fix it still)

Traceback (most recent call last):
  File "C:/Users/Trevor/Downloads/TEST/images/TEST2", line 84, in <module>
    player=Sprite(100,150)
  File "C:/Users/Trevor/Downloads/TEST/images/TEST2", line 28, in __init__
    self.i0=pygame.image.load("Sprite0.PNG")
error: Couldn't open Sprite0.PNG

Upvotes: 3

Views: 151

Answers (2)

ZenOfPython
ZenOfPython

Reputation: 901

The above error tells you exactly what it could not do.

There are two things you could do. Use cd to enter the directory of the images, and move them to the directory of your program.

You could also use the path to the image, for example:

Instead of image.png, if the image was in my home directory I would use /Users/ZenOfPython/image.png.

Upvotes: 2

XrXr
XrXr

Reputation: 2067

You will have to run the script in the directory where you put Sprite0.png. First use cd to change to that directory then run the script using python myprogram.py. Or you can use an absolute path for loading. So instead of pygame.image.load("Sprite0.PNG"), you would do pygame.image.load("my/path/to/Sprite0.png")

Upvotes: 3

Related Questions