Python Project for A SHIP THAT FIRES BULLETS

1.Planning Your Project

2.Installing Pygame

python3 -m pip install --user pygame

3.Starting the Game Project

3.1 Creating a Pygame Window and Responding to User Input.


# import modules
import sys
import pygame

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()

        # 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))
        pygame.display.set_caption("Alien Invasion")

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            # Watch for keyboard and mouse events.
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

            # Make the most recently drawn screen visible
            pygame.display.flip()

if __name__ == '__main__':
    # Make a game instance, and run the game.
    ai = AlienInvasion()
    ai.run_game()

3.2 Controlling the Frame Rate


# import modules
import sys
import pygame

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))
        pygame.display.set_caption("Alien Invasion")

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            # Watch for keyboard and mouse events.
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

            # Make the most recently drawn screen visible
            pygame.display.flip()
            # the tick() method takes one argument: the frame rate for the game.
            self.clock.tick(60)

if __name__ == '__main__':
    # Make a game instance, and run the game.
    ai = AlienInvasion()
    ai.run_game()

3.3 Setting the Background Color


# import modules
import sys
import pygame

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))
        pygame.display.set_caption("Alien Invasion")

        # Set the background color.
        self.bg_color = (230, 230, 230)

    def run_game(self):
        """Start the main loop for the game."""
        while True:
            # Watch for keyboard and mouse events.
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                    
            # Redraw the screen during each pass through the loop.
            self.screen.fill(self.bg_color)

            # Make the most recently drawn screen visible
            pygame.display.flip()
            # the tick() method takes one argument: the frame rate for the game.
            self.clock.tick(60)

if __name__ == '__main__':
    # Make a game instance, and run the game.
    ai = AlienInvasion()
    ai.run_game()

3.4 Creating a Setting Class

class Settings:
    """A class to store all settings for Alien Invasion"""
    def __init__(self):
        """Initialize the game's settings."""
        # Screen settings
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230, 230, 230)
        

To make an instance of Settings in the project and use it to access our settings, we need to modify alien_invasion.py as follows:


# import modules
import sys
import pygame
from settings import Settings

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值