[Python Crash Course] Alien

  • Basic Structure

 

# import sys                      # exit the game

import pygame

from setting import Settings

from ship import Ship

import game_functions as gf

from pygame.sprite import Group                               # manage bullets


def run_game():
    # Initialize game and create a screen object #
    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, ai_settings)

    bullets = Group()

    # the main loop for the game #
    while True:
        # listen to keyboards and mouse events #
        gf.check_events(ai_settings, screen, ship, bullets)

        ship.update()

        gf.update_bullets(bullets)

        gf.update_screen(ai_settings, screen, ship, bullets)


run_game()
  • Game Functions 

import sys

import pygame

from bullet import Bullet


def fire_bullet(ai_settings, screen, ship, bullets):
    if len(bullets) < ai_settings.bullet_allowed:
        new_bullet = Bullet(ai_settings, screen, ship)
        bullets.add(new_bullet)


def check_events_down(event, ai_settings, screen, ship, bullets):
    """ respond to keyboard and mouse events """
    if event.key == pygame.K_RIGHT:
        # ship.rect.centerx += 1                          # each press for one movement
        ship.moving_flag_right = True
    elif event.key == pygame.K_LEFT:
        ship.moving_flag_left = True

    elif event.key == pygame.K_SPACE:
        fire_bullet(ai_settings, screen, ship, bullets)


def check_events_up(event, ship):
    if event.key == pygame.K_RIGHT:
        ship.moving_flag_right = False
    elif event.key == pygame.K_LEFT:
        ship.moving_flag_left = False


def check_events(ai_settings, screen, ship, bullets):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:                  # DOWN: press
            check_events_down(event, ai_settings, screen, ship, bullets)
        elif event.type == pygame.KEYUP:                    # UP: release
            check_events_up(event, ship)


def update_screen(ai_settings, screen, ship, bullets):
    """ upload image and flip to the new screen  """
    screen.fill(ai_settings.bg_color)

    ship.blitme()

    for bullet in bullets:
        bullet.draw_bullet()

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


def update_bullets(bullets):
    bullets.update()
    for bullet in bullets.copy():
        if bullet.rect.bottom <= 0:
            bullets.remove(bullet)
    # print(len(bullets))
  • Settings 

class Settings:
    """ store all settings """

    def __init__(self):

        # screen settings #
        self.screen_width = 1200
        self.screen_height = 600
        self.bg_color = (230, 230, 230)                     # background color: 0-255

        self.ship_speed_factor = 1.5                        # 1.5 pixels per time

        self.bullet_speed_factor = 1
        self.bullet_width = 3                               # 3×15 pixels
        self.bullet_height = 15
        self.bullet_color = 60, 60, 60
        self.bullet_allowed = 3
  •  Bullet

 

import pygame

from pygame.sprite import Sprite                 # act on all grouped elements at once


class Bullet(Sprite):
    """ manage bullets fired from the ship """

    def __init__(self, ai_settings, screen, ship):

        super().__init__()                                    # inherit

        self.screen = screen

        # position #
        self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height)  # Rect(x,y,width,height)
        self.rect.centerx = ship.rect.centerx
        self.rect.top = ship.rect.top

        self.y = float(self.rect.y)                           # decimal

        self.color = ai_settings.bullet_color
        self.speed_factor = ai_settings.bullet_speed_factor

    def update(self):
        """ update position """
        self.y -= self.speed_factor
        self.rect.y = self.y

    def draw_bullet(self):
        pygame.draw.rect(self.screen, self.color, self.rect)
  • ship

import pygame


class Ship:

    def __init__(self, screen, ai_settings):
        """ initialize the ship and set the start position """
        self.screen = screen
        self.ai_settings = ai_settings

        # load the ship image and get its rect #
        self.image = pygame.image.load('ship.bmp')
        self.rect = self.image.get_rect()                       # rect: integer position
        self.screen_rect = screen.get_rect()

        # start each ship at the center-bottom of screen  #
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        # store a decimal value for the ship's center #
        self.center = float(self.rect.centerx)

        # flags #
        self.moving_flag_right = False
        self.moving_flag_left = False

    def update(self):
        # update position based on movement_flag #
        if self.moving_flag_right and self.rect.right < self.screen_rect.right:     # limit the range
            self.center += self.ai_settings.ship_speed_factor
        if self.moving_flag_left and self.rect.left > 0:
            self.center -= self.ai_settings.ship_speed_factor                       # rect.centerx: integer

        # update rect object from self.center #
        self.rect.centerx = self.center

    def blitme(self):
        """  draw the ship at its current position """
        self.screen.blit(self.image, self.rect)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值