Rotating a sprite with pygame

Wan't to discuss something that's relevant but has no forum, this is the place
Post Reply
najanaja73
Beginner
Posts: 1
Joined: Tue Nov 18, 2014 5:31 pm

Rotating a sprite with pygame

Post 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!
chad
Expert
Posts: 555
Joined: Sun Sep 28, 2014 12:54 am
Location: New York, USA
Contact:

Re: Rotating a sprite with pygame

Post 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...
Crazyflier - my CF journal...
4x Crazyflie Nano (1.0) 10-DOF + NeoPixel Ring mod.
3x Crazyflie 2.0 + Qi Charger and LED Decks.
Raspberry Pi Ground Control.
Mac OS X Dev Environment.
Walkera Devo7e, ESky ET6I, PS3 and iOS Controllers.
serwusek
Beginner
Posts: 3
Joined: Wed Jun 10, 2015 8:14 am

Re: Rotating a sprite with pygame

Post by serwusek »

everything is explained good in links
Post Reply