I
I
Ivan Lobynichev2020-04-22 20:58:17
Python
Ivan Lobynichev, 2020-04-22 20:58:17

How to make animation in a starship game?

Here is the cat of the program:

1) Game_main.py

import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf

def run_game():
  # Инициализирование pygame, settings и объекта экрана
  pygame.init()
  ai_settings = Settings()
  screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
  pygame.display.set_caption("Alien Invasion")
  # Создание корабля
  ship = Ship(screen)
  # Назначение цвета фона
  #bg_color = (230, 230, 230)
  
  # Запуск основного цикла игры
  while True:
    gf.check_events(ship)
    gf.update_screen(ai_settings, screen, ship)
run_game()

2) ship.py

import pygame

class Ship():
  def __init__(self, screen):
    """Инициализация корабля и задавание ему начальной позиции."""
    self.screen = screen

    SHIPS = ["images/ship1.png", "images/ship2.png"]
    i = 0
    while i < 2:
      # Загрузка изображения корабля и получение прямоугольника
      self.image = pygame.image.load(SHIPS[i])
      i += 1
      #self.image = pygame.image.load("images/ship.png")
      self.rect = self.image.get_rect()
      self.screen_rect = screen.get_rect()
      # Каждый новый корабль появляется у нижнего края экрана
      self.rect.centerx = self.screen_rect.centerx
      self.rect.bottom = self.screen_rect.bottom
      
  def blitme(self):
    """Рисует корабль в текущей позиции"""
    self.screen.blit(self.image, self.rect)

I wanted to do it in the 2nd file through an array, where the file names are inserted into While - < 2. I thought that I would have such a simple animation, but I don’t see it, because the file at once 2nd for an eye is shown. Is there anything else I need to do with the blitme() function? Maybe here somehow again in a cycle, how to do it in the first function?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question