Page 1 of 1

Rotating a sprite with pygame

Posted: Tue Nov 18, 2014 7:11 pm
by najanaja73
Hi, I've just started using Python/Pygame as a hobby. I've gotten the code to draw my Plane.png and move it using the wsad keys, and I mapped the q and e keys to rotate it which it does, but each time it rotates, it 'falls' down the screen to the bottom right and after a few key presses, disappears. I read that this is because when it turns the sprite, it adds padding to resize the image while it is turning. I think that each time it turns, it adds more padding, making it add exponentially for each keypress. I can't figure out how to stop this from happening. Any help for a beginner greatly appreciated! The Colours import is just a file where I keep my pre-defined colours for easier colour filling, and Plane.gif is an inch by inch and a half gif of a black plane with transparent background, and loads no problem, it just flies away when i use the e or q keys

Code: Select all


import pygame, sys, time, random
from pygame.locals import *
from Colours import *
from PIL import Image, ImageDraw
 
 
class MySprite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('Plane.gif').convert()
        self.rect = self.image.get_rect()
        self.x = 0
        self.y = 0
 
    def draw(self,surface):
 
            surface.blit(self.image,(self.x,self.y))
 
 
    def onKeyPress(self):
 
        key = pygame.key.get_pressed()
        distance = 5
        if key[ord('s')] or key[pygame.K_DOWN]: #down
            self.y += distance
        elif key[ord('w')] or key[pygame.K_UP]: #up
            self.y -= distance
        if key[ord('d')] or key[pygame.K_RIGHT]: #right
            self.x += distance
        elif key[ord('a')] or key[pygame.K_LEFT]: #left
            self.x -= distance
 
        rotate = pygame.transform.rotate
        if key[ord('e')]:
            self.image = rotate(self.image, 10)
        elif key[ord('q')]:
            self.image = rotate(self.image, -10)
 
class Event(object):
 
    def __init__(self):
 
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit()
 
 
pygame.init()
 
fps = 30
fpsClock = pygame.time.Clock()
 
size = width,height = 800,600
screen = pygame.display.set_mode(size)
mySprite = MySprite()
 
while True:
 
    event = Event()
    screen.fill(Blue)
 
    mySprite.draw(screen)
    mySprite.onKeyPress()
 
    pygame.display.update()
 
    fpsClock.tick(fps)

This is the code, hope you can help, thanks!

Re: Rotating a sprite with pygame

Posted: Thu Jan 15, 2015 1:45 am
by chad
I'm not sure you'll get any traction on that question here... Might want to check the pygame docs or the answers you got when you posted it on this other forum...

Re: Rotating a sprite with pygame

Posted: Wed Jun 10, 2015 8:16 am
by serwusek
everything is explained good in links