Untitled

 avatar
unknown
plain_text
a year ago
3.2 kB
8
Indexable
import pygame
import sys

WINDOW_HEIGHT = 1000
WINDOW_WIDTH = 1000
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
FRAME_INTERVAL=10
ANIMATION_INTERVAL = 200

pygame.init() 
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))

SHIP_SPEED = 1

class Spaceship(pygame.sprite.Sprite):
  def __init__(self):
    super().__init__()          #only need here, not for method
    self.window = window
    self.image = pygame.image.load('Ship-Idle.png')
    self.rect = self.image.get_rect()
    self.rect.center = (WINDOW_WIDTH // 2, WINDOW_HEIGHT//2)
    self.frame_list = init_animation_frames("Ship-Move.png", 6)
    self.current_frame_index = 0
    self.last_time_frame_updated = pygame.time.get_ticks()
    
  def move(self):
    keys = pygame.key.get_pressed()
    if keys[pygame.K_RIGHT] and self.rect.x <= WINDOW_WIDTH:
        self.rect.x += SHIP_SPEED
    if keys[pygame.K_LEFT] and self.rect.x >= 0:
        self.rect.x -= SHIP_SPEED
    if keys[pygame.K_UP] and self.rect.y >= 0:
        self.rect.y -= SHIP_SPEED
    if keys[pygame.K_DOWN] and self.rect.y <= WINDOW_HEIGHT:
        self.rect.y += SHIP_SPEED
        

def init_animation_frames(file_name, frame_count):
    sprite_sheet_image = pygame.image.load(file_name).convert_alpha()   # load the image
    frame_width = sprite_sheet_image.get_width() / frame_count  # set the width of each frame by dividing the total width by the number of frames
    frame_height = sprite_sheet_image.get_height()  # get the height of the image
    animation_frames = []   # set up our frame list

    # loop for the number of frames in our image and fill up our frame list
    for frame in range(frame_count):
        # create a new surface for our image
        frame_surface = pygame.Surface((frame_width, frame_height)).convert_alpha()
        # set the background color that will be removed
        frame_surface.fill(BLUE)
        # draw the image onto the new surface, determining the loatation of the frame by frame number * frame width
        frame_surface.blit(sprite_sheet_image, (0, 0), ((frame * frame_width), 0, frame_width, frame_height))
        # the transparent part of the picture will be replaced with blue, so we need to set it back to transparent
        frame_surface.set_colorkey(BLUE)
        # add the new image to the end of the frame list
        animation_frames.append(frame_surface)

    return animation_frames
    
def animate_sprite(obj):
    current_time = pygame.time.get_ticks()
    
    if current_time - obj.last_time_frame_updated >= ANIMATION_INTERVAL:
        obj.current_frame_index += 1
        obj.last_time_frame_updated = current_time
        if obj.current_frame_index >= len(obj.frame_list):
            obj.current_frame_index = 0

        obj.image = obj.frame_list[obj.current_frame_index]

spaceship = Spaceship()
all_sprites = pygame.sprite.Group()
spaceship_group = pygame.sprite.Group()

while True:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      sys.exit()
  all_sprites.update()
  all_sprites.draw(window)
  
  spaceship.move()
  animate_sprite(spaceship)
  
  window.fill(WHITE)
  window.blit(spaceship.image, spaceship.rect)

  pygame.display.update()     
Editor is loading...
Leave a Comment