1

I was working on a game with python and I wanted to add a feature that whenever a key is pressed rotate the player image according to which key, but the self.rotation variable never updates

player class code:

import pygame
from physics import Movement

class Player():
    def __init__(self, x, y, width, height, skin):
        self.x = x
        self.y = y
        self.rotation = 0
        self.width = width
        self.height = height
        self.rect = (x, y, width, height)
        self.speed = 3
        self.skin = skin

    def draw(self, win, skin):
        self.rotation = Movement.rotate(self, self.rotation)
        print(self.rotation)
        player = pygame.transform.rotate(skin.convert_alpha(), self.rotation)
        win.blit(player, self.rect)

    def move(self):
        keys = pygame.key.get_pressed()

        if keys[pygame.K_LEFT] or keys[pygame.K_a]:
            self.x -= self.speed

        if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
            self.x += self.speed

        if keys[pygame.K_UP] or keys[pygame.K_w]:
            self.y -= self.speed

        if keys[pygame.K_DOWN] or keys[pygame.K_s]:
            self.y += self.speed

        self.update()

    def update(self):
        self.rect = (self.x, self.y, self.width, self.height)

physics class code:

import pygame

class Movement():

    def rotate(self, rotation):
        self.rotation = rotation
        keys = pygame.key.get_pressed()

        if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
            if self.rotation < 90 and self.rotation > 0:
                self.rotation += 5

        if keys[pygame.K_DOWN] or keys[pygame.K_s]:
            if self.rotation < 180 and self.rotation > 90:
                self.rotation += 5

        if keys[pygame.K_LEFT] or keys[pygame.K_a]:
            if self.rotation < 270 and self.rotation > 180:
                self.rotation += 5

        if keys[pygame.K_UP] or keys[pygame.K_w]:
            if self.rotation < 360 and self.rotation > 270:
                self.rotation += 5

        return self.rotation

game code:

import pygame
from Network import Network

pygame.font.init()
pygame.init()

width = 700
height = 600
fps = 60
current_id = 0
img = pygame.image.load('game_background.png')
win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Astro party")
info_font = pygame.font.SysFont("Verdana", 26)

player_list = {}
player_count = 0
loaded_skins = {}

n = Network()
name = 'Player'
NAME_FONT = pygame.font.SysFont("comicsans", 24)

def redrawWindow(win, players, id):
    global player_count
    win.blit(img.convert(), (0, 0))

    for player in players:
        p = players[player]
        if player_count < len(players):
            skin = pygame.image.load(p['skin'])
            loaded_skins[player] = skin
        p['player'].draw(win, loaded_skins[player])
        text = NAME_FONT.render(p["name"], 1, (0,0,0))
        win.blit(text, (p['player'].x - text.get_width()/2, p['player'].y - text.get_height()/2))
    player_count = len(players)


def main(name):
    global win, width, height
    run = True
    n = Network()
    clock = pygame.time.Clock()
    current_id = n.connect(name)
    player_list = n.send("get")

    while run:
        clock.tick(fps)

        player_list = n.send("get")

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()

        player_list[current_id]['player'].move()
        data = player_list
        n.send(data, pick=True)

        redrawWindow(win, player_list, current_id)
        pygame.display.update()

    n.disconnect()
    pygame.quit()
    quit()

main(name)

So I was trying to ratote the player image whenever the player clicks a specific key, and I tried printing self.rotation, but it never updates.

Manoj
  • 2,059
  • 3
  • 12
  • 24
Cyber
  • 13
  • 3
  • While `Movement.rotate(self, self.rotation)` works, it's not how you're suppose to use classes. You're suppose to create instances of the class and call the method, like `a = Movement()` and then `a.rotate(some_rotation)`. I think you want to use a function instead, as it is currently a bit misleading and confusing. – Ted Klein Bergman Dec 20 '20 at 08:17
  • Have you debugged your code? Is the if-statements ever entered? If not, what's the value of the rotation when you call the function? – Ted Klein Bergman Dec 20 '20 at 08:17
  • @TedKleinBergman ya I will work on that too, but for now I was just testing the rotation of the player – Cyber Dec 20 '20 at 08:34

1 Answers1

0

The initial value of self.roation is 0. Change the condition in Movement.rotate. You need to check self.rotation >= 0 rather than self.rotation > 0:

if self.rotation < 90 and self.rotation > 0:

if self.rotation < 90 and self.rotation >= 0:

If you want to rotate the player around its center you need to set the center of the rotated rectangle set the center position through the center of the original rectangle. See How do I rotate an image around its center using PyGame?

class Player():

    def draw(self, win, skin):
        self.rotation = Movement.rotate(self, self.rotation)
        print(self.rotation)
        player = pygame.transform.rotate(skin.convert_alpha(), self.rotation)
        
        rect = pygame.Rect(rect)            
        win.blit(player, player.get_rect(centere = rect.center))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174