1
import pygame



class Sprite:

    def __init__(self, x, y, curren_time):

        self.rect = pygame.Rect(x, y, 100, 110)

        self.images = []

#Tells pygame the image list that you want to stitch together
        for x in range(10):
            img = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST6/Cernunnos" + str(x) +".PNG")
#Append the image to each other to create the animation effect
            self.images.append( img )

        self.current_image = 0

        self.time_num = 100
        self.time_target = curren_time + self.time_num

    def update(self, curren_time):

        if curren_time >= self.time_target:

            self.time_target = curren_time + self.time_num

            self.current_image += 1

            if self.current_image == len(self.images):
            self.current_image = 0

    def render(self, window):

        window.blit(self.images[self.current_image], self.rect)
#Colors
black = (0,0,0)
white = (255,255,255)




def main():

    pygame.init()

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

    pygame.display.set_caption("Sprites")

    move_x, move_y = 0, 0

    clock = pygame.time.Clock()
    curren_time = pygame.time.get_ticks()

    player = Sprite(110,100,curren_time)

    font = pygame.font.SysFont(None, 150)
    pause_text = font.render("PAUSE",1,white)
    pause_rect = pause_text.get_rect( center = window.get_rect().center )

#Adding how many places the image moves when using the key function
    state_game = True
    state_pause = False

#So while True
    while state_game:

        curren_time = pygame.time.get_ticks()



        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                state_game = False

            elif event.type == pygame.KEYDOWN:

                if event.key ==pygame.K_ESCAPE:
                    state_game = False

                elif event.key == pygame.K_SPACE:
                    state_pause = not state_pause

                if event.key == pygame.K_LEFT:
                    move_x = -3

                elif event.key == pygame.K_RIGHT:
                    move_y = 3

                elif event.key == pygame.K_UP:
                    move_x = -3

                elif event.key == pygame.K_DOWN:
                    move_y = 3

            elif event.type == pygame.KEYUP:

                if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
                    move_x = 0

                elif event.key in (pygame.K_UP, pygame.K_DOWN):
                    move_y = 0



        if not state_pause:
            player.rect.x += move_x
            player.rect.y += move_y
            player.update(curren_time)

#Fills the window with a color

        window.fill(black)

        player.render(window)

        if state_pause:
            window.blit(pause_text,pause_rect)

        pygame.display.flip()



        clock.tick(50)



    pygame.quit()



if __name__ == '__main__':
    main()
#Code constructed by furas

So the problem is whenever I hit the right key the animation slides off the screen on the left. the animation does not stop when you take your finger off the key. I have searched for the problem myself and am unable to find any problems. Please if you see something that may be causing the problem let me know. Thanks! (Up key = Down, Down key = Down, Right key = Left, Left key = Left)

3
  • Is the problem affecting both pygame.K_LEFT and pygame.K_RIGHT? Commented Jul 13, 2014 at 4:01
  • The left arrow works fine it moves the animation left but the right key just makes the animation move down Commented Jul 13, 2014 at 4:04
  • Up key = left, down key = down, right key = down, left key = left...so two work two dont Commented Jul 13, 2014 at 4:06

1 Answer 1

2

Your problem is that you have move_x and move_y swapped for K_RIGHT and K_UP.

            elif event.key == pygame.K_RIGHT:
                move_y = 3

            elif event.key == pygame.K_UP:
                move_x = -3

Should be:

            elif event.key == pygame.K_RIGHT:
                move_x = 3

            elif event.key == pygame.K_UP:
                move_y = -3
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.