0

I want to make the "ball.png" flip horizontally when pressing K_RIGHT and K_LEFT during movement.

I tried to insert pygame.transform.flip(self.image, True, False) in the Ball class but then i managed to screw everything when defining the movement keys.

Could you please suggest me the simplest and easiest way to do that? :) I'm trying to learn so every explanation will be super useful.

Here's the code I'm running atm.

import pygame
import os

size = width, height = 750, 422
screen = pygame.display.set_mode(size)

img_path = os.path.join(os.getcwd())
background_image = pygame.image.load('background.jpg').convert()

pygame.display.set_caption("BallGame")

class Ball(object):
    def __init__(self):
        self.image = pygame.image.load("ball.png")
        self.image_rect = self.image.get_rect()
        self.image_rect.x
        self.image_rect.y

    def handle_keys(self):
        key = pygame.key.get_pressed()
        dist = 5
        if key[pygame.K_DOWN] and self.image_rect.y < 321:
            self.image_rect.y += dist
        elif key[pygame.K_UP] and self.image_rect.y > 0:
            self.image_rect.y -= dist
        if key[pygame.K_RIGHT] and self.image_rect.x < 649:
            self.image_rect.x += dist
        elif key[pygame.K_LEFT] and self.image_rect.x > 0:
            self.image_rect.x -= dist

    def draw(self, surface):
        surface.blit(self.image,(self.image_rect.x,self.image_rect.y))

pygame.init()
screen = pygame.display.set_mode((750, 422))

ball = Ball()
clock = pygame.time.Clock()

running = True
while running:
    esc_key = pygame.key.get_pressed()
    for event in pygame.event.get():
        if esc_key[pygame.K_ESCAPE]:
            pygame.display.quit()
            pygame.quit()
            running = False

    ball.handle_keys()

    screen.blit(background_image, [0, 0])
    ball.draw(screen)
    pygame.display.update()

    clock.tick(40)
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Shuratt
  • 89
  • 8
  • Did you look at this? https://stackoverflow.com/questions/45601109/how-do-i-flip-an-image-horizontally-in-pygame . What was the issue when you used `pygame.transform.flip(self.image, True, False)` – RohithS98 Jul 11 '18 at 08:49
  • There i came to know about transform.flip , but i probably used it in the wrong place. I'm a newbie so i'm still trying to learn. I thought about adding that before the self.image_rect.y -= dist in the keys definitions. – Shuratt Jul 11 '18 at 09:20
  • Use pygame.transform.rotate. Refer to [this](https://www.pygame.org/docs/ref/transform.html) – Hoseong Jeon Jul 11 '18 at 09:27

1 Answers1

0

This code is not tested but it should give you the correct logic. Also this assumes that the default direction of the sprite is left facing.

First you need to know which direction they are facing. You can use a variable facing

def __init__(self):
    self.image = pygame.image.load("ball.png")
    self.image_rect = self.image.get_rect()
    self.image_rect.x
    self.image_rect.y
    self.facing = "LEFT"

You need to change facing when you press right or left:

def handle_keys(self):
    key = pygame.key.get_pressed()
    dist = 5
    if key[pygame.K_DOWN] and self.image_rect.y < 321:
        self.image_rect.y += dist
    elif key[pygame.K_UP] and self.image_rect.y > 0:
        self.image_rect.y -= dist
    if key[pygame.K_RIGHT] and self.image_rect.x < 649:
        self.facing = "RIGHT"
        self.image_rect.x += dist
    elif key[pygame.K_LEFT] and self.image_rect.x > 0:
        self.facing = "LEFT"
        self.image_rect.x -= dist

Finally, you need to display the image correctly based on facing:

def draw(self, surface):
    if self.facing == "RIGHT":
        surface.blit(pygame.transform.flip(self.image, True, False),(self.image_rect.x,self.image_rect.y))
    else:
        surface.blit(self.image,(self.image_rect.x,self.image_rect.y))
RohithS98
  • 500
  • 2
  • 14
  • It works! Thank you so much Rohith, i'll study the syntax and the position of what you added for the next time. – Shuratt Jul 11 '18 at 09:44
  • @Shuratt also if you want some more details or help in making games, refer to this pdf https://inventwithpython.com/makinggames.pdf – RohithS98 Jul 11 '18 at 09:54