Ok im using pygame to make a simple 2d game and i tried to make a rotation script:
here is all my code so far:
CODE
import pygame
from pygame.locals import*
pygame.init()
windowSize = 1024,768
# initializing the game window
gameWindow = pygame.display.set_mode((windowSize)) #, FULLSCREEN )
pygame.display.set_caption('STARBOUND')
screen = pygame.display.get_surface()
#loading textures
# ship sprites
podTex = 'shipPlaceholder.png'
podSprite = pygame.image.load(podTex).convert_alpha()
clock = pygame.time.Clock()
class ship:
def __init__(self):
self.shipName = "pod";
self.xPos = 0
self.yPos = 0
self.position = (self.xPos, self.yPos);
self.width = 120;
self.height = 120;
self.hull = 100;
self.hullMax = 1000;
self.energy = 1000;
self.energyMax = 1000;
self.engineSpeed = 5;
self.thrustingSpeed = 0;
self.thrustingSpeedMax = 10;
self.primaryWeapon = 1;
self.secondaryWeapon = 0;
self.secondaryWeaponMax = 0;
self.angle = 0;
self.isComp = True;
self.shipTexture = podSprite;
def turn(self,amount):
self.angle += amount
self.shipTexture = pygame.transform.rotate(self.shipTexture, self.angle)
def draw(self):
screen.blit(self.shipTexture, (self.position))
playerShip = ship()
playerShip.xPos = windowSize[0] / 2 - 60
playerShip.yPos = windowSize[1] / 2 - 60
playerShip.position = (playerShip.xPos, playerShip.yPos);
playerShip.isComp = False
while True:
# Limit frame speed to 50 FPS
#
time_passed = clock.tick(50)
keyBState = pygame.event.get()
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
ship.turn(playerShip, 5)
ship.draw(playerShip)
pygame.display.flip()
so my little ship was supposed to rotate in the same stop endlessly... but instead it rolled out of the view... its position wasnt supposed to change... only the direction it is facing... if you would like to see what happened check the attached image.
i think a partial reason for this happening was because the sprite's rotational point was in the top left corner and not in the center... but i dont think that explains why it kept moving off the screen