Aug-25-2021, 04:15 AM
Example
import pygame def main(): # Basic Pygame Setup pygame.display.set_caption("Rotating Sailing Ship") surface = pygame.display.set_mode((400, 300)) clock = pygame.time.Clock() rect = surface.get_rect() running = True delta = 0 fps = 30 # Ship ship = pygame.sprite.Sprite() # Keep orignal image. For it doesn't get distorted. # pygame.SRCALPHA for transparency ship.orignal_image = pygame.Surface((100, 10), pygame.SRCALPHA) ship.image = ship.orignal_image ship.image.fill("white") ship.rect = ship.image.get_rect(center=rect.center) # Hold are floats ship.position = pygame.Vector2(ship.rect.center) # Hold are heading ship.vector = pygame.Vector2(1, 0) ship.rotate_velocity = 60 ship.velocity = 80 # Main Loop while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # Move and Rotate rotate = 0 key_state = pygame.key.get_pressed() if key_state[pygame.K_UP] or key_state[pygame.K_w]: ship.position += ship.vector * ship.velocity * delta ship.rect.center = ship.position if key_state[pygame.K_LEFT] or key_state[pygame.K_a]: rotate = -ship.rotate_velocity * delta if key_state[pygame.K_RIGHT] or key_state[pygame.K_d]: rotate = ship.rotate_velocity * delta if rotate != 0: ship.vector.rotate_ip(rotate) angle = ship.vector.as_polar()[1] ship.image = pygame.transform.rotate(ship.orignal_image, -angle) # Keep ship in center ship.rect = ship.image.get_rect(center=ship.rect.center) # Draw surface.fill("blue") surface.blit(ship.image, ship.rect) # Render To Screen pygame.display.flip() # Delta for smooth movement delta = clock.tick(fps) * 0.001 if __name__ == "__main__": pygame.init() main() pygame.quit()
99 percent of computer problems exists between chair and keyboard.