Python Project for A SHIP THAT FIRES BULLETS Upgrade One

we can learn more about Pygame and about managing a large project. And also learn to detect collisions between game objects,like bullets and aliens. Detecting collisions helps you define interactions between elements in your games.

1.Reviewing the Project

we'll do the following:

  • Add a single alien to the top-left corner of the screen, with appropriate spacing around it.
  • Fill the upper portion of the screen with as many aliens as we can fit horizontally.We'll then create additional rows of aliens until we have a full fleet.
  • Make the fleet move sideways and down until the entire fleet is shot down, an alien hits the ship, or an alien reaches the ground.If the entire fleet is shot down, we'll create a new fleet. If an alien hits the ship or the ground, we'll destroy the ship and create a new fleet,
  • Limit the number of ships the player can use, and end the game when the player has used up the allotted number of ships.

Creating the First Alien

import pygame
from pygame.sprite import Sprite

class Alien(Sprite):
    """A class to represent a single alien in the fleet."""
    def __init__(self, ai_game):
        """Initialize the alien and set its starting position."""
        super().__init__()
        self.screen = ai_game.screen

        # Load the alien image and set its rect attribute.
        self.image = pygame.image.load('images/alien.bmp')
        self.rect = self.image.get_rect()

        # Start each new alien near the top left of the screen.
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        # Store the alien's exact horizontal position.
        self.x = float(self.rect.x)

2.Creating an Instance of the Alien


# import modules
import sys
import pygame
from settings import Settings
from ship import Ship
from bullet import Bullet
from alien import Alien

class AlienInvasion:
    """Overall class to manage game assets and behavior."""

    def __init__(self):
        """Initialize the game, and create game resources."""

        # the pygame.init() function initializes the background settings that Pygame need to work properly.
        pygame.init()
        # define the clock in the __init() method.ensure the clock ticks once on each pass through the main loop.
        self.clock = pygame.time.Clock()

        # create a display window. the argument (1200, 800) is a tuple that defines the dimensions of the game window,which will be 1200 pixels wide by 800 pixels high.
        #self.screen = pygame.display.set_mode((1200, 800))
        self.settings = Settings()
        #self.screen = pygame.display.set_mode((self.settings.screen_width,self.settings.screen_height))
        # Running the Game in Fullscreen Mode
        self.screen = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
        self.settings.screen_width = self.screen.get_rect().width
        self.settings.screen_height = self.screen.get_rect().height
        pygame.display.set_caption("Alien Invasion")


        # the call to Ship() requires one argument:an instance of AlienInvasion.
        self.ship = Ship(self)

        # create the group that holds the bullets in __init__()
        self.bullets = pygame.sprite.Group()
        self.aliens = pygame.sprite.Group()

        self._create_fleet()

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            self._check_events()
            self.ship.update()
            self.bullets.update()
            self._update_bullets()

            self._update_screen()
            # the tick() method takes one argument: the frame rate for the game.
            self.clock.tick(60)

    def _check_events(self):
        """Respond to keypresses and mouse events."""
        for event in pygame.event.get():
            if even
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值