How TO Rotate pygame?

How to rotate in pygame? I tried this, but it doesn’t work, here is my code

`import pygame, sys, math

pygame.init()

width, height = 800, 600
win = pygame.display.set_mode((width, height))
pygame.display.set_caption(‘Player Looking Test’)

def invsin(x):
math.arcsin(x)

class Player():
def init(self, x, y, width, height, color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.rect = (x, y, width, height)
self.vel = 0.3
def draw(self, win):
pygame.draw.rect(win, self.color, self.rect)

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

if keys[pygame.K_LEFT]:
  self.x -= self.vel

if keys[pygame.K_RIGHT]:
  self.x += self.vel

if keys[pygame.K_UP]:
  self.y -= self.vel

if keys[pygame.K_DOWN]:
  self.y += self.vel

if keys[pygame.K_SPACE]: win.blit(pygame.transform.rotate(self.rect, 45))

self.rect = (self.x, self.y, self.width, self.height)

def redrawWindow(win, player):
win.fill((255, 255, 255))
player.draw(win)
pygame.display.update()

def main():
run = True
p = Player(50, 50, 100, 100, (0, 255, 0))

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

p.move()
redrawWindow(win, p)

main()`

import pygame, sys, math

pygame.init()

width, height = 800, 600
win = pygame.display.set_mode((width, height))
pygame.display.set_caption('Player Looking Test')

def invsin(x):
  math.arcsin(x)

class Player():
  def __init__(self, x, y, width, height, color):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.color = color
    self.rect = (x, y, width, height)
    self.vel = 0.3
  def draw(self, win):
    pygame.draw.rect(win, self.color, self.rect)

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

    if keys[pygame.K_LEFT]:
      self.x -= self.vel

    if keys[pygame.K_RIGHT]:
      self.x += self.vel

    if keys[pygame.K_UP]:
      self.y -= self.vel

    if keys[pygame.K_DOWN]:
      self.y += self.vel

    if keys[pygame.K_SPACE]: win.blit(pygame.transform.rotate(self.rect, 45))

    self.rect = (self.x, self.y, self.width, self.height)



def redrawWindow(win, player):
  win.fill((255, 255, 255))
  player.draw(win)
  pygame.display.update()

def main():
  run = True
  p = Player(50, 50, 100, 100, (0, 255, 0))

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

    p.move()
    redrawWindow(win, p)

main()

Hi @RiyanNayak, it seems that you are not able to rotate Rects in pygame, so instead I would look into using surfaces. This should help you: python - How do I rotate an image around its center using Pygame? - Stack Overflow

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.