Python

Python pygame小游戏

2018-02-08  本文已影响17人  獨荹儛臨
import sys
import pygame
from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
from alien import Alien

def run_game():

    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(ai_settings, screen)

    bullets = Group()
    aliens = Group()
    # 创建外星人群
    gf.create_fleet(ai_settings, screen, aliens)
    while True:
        #  封装检查键盘事件
        gf.check_events(ai_settings, screen, ship, bullets)
        ship.update()
        #  删除消失的子弹
        gf.update_bullets(bullets)
        gf.update_screen(ai_settings, screen, ship, aliens, bullets)

run_game()

class Settings():

    def __init__(self):

        self.screen_width = 1200  # 界面宽度
        self.screen_height = 800  # 界面高度
        self.bg_color = (230, 230, 230)
        self.ship_speed_factor = 15   # 飞船/子弹移动速度
        self.bullet_width = 3
        self.bullet_height = 15  # 子弹高度
        self.bullet_color = 60, 60, 60  # 子弹的颜色
        self.bullets_allowed = 3  # 允许有多少子弹
import pygame
from pygame.sprite import Sprite


class Alien(Sprite):

    def __init__(self, aisettings, screen):
        super(Alien, self).__init__()
        self.screen = screen
        self.aisettings = aisettings
        self.image = pygame.image.load('images/alien.bmp')
        self.rect = self.image.get_rect()
        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        self.x = float(self.rect.x)

    def blitme(self):

        self.screen.blit(self.image, self.rect)

import pygame
from pygame.sprite import Sprite

#  继承Sprite
class Bullet(Sprite):

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

        super(Bullet, self).__init__()
        self.screen = screen
        self.rect = pygame.Rect(0,
                                0,
                                aisettings.bullet_width,
                                aisettings.bullet_height)
        self.rect.centerx = ship.rect.centerx
        self.rect.top = ship.rect.top
        self.y = float(self.rect.y)
        self.color = aisettings.bullet_color
        self.speed_factor = aisettings.ship_speed_factor

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

    def draw_bullet(self):

        pygame.draw.rect(self.screen, self.color, self.rect)
import sys
import pygame
from bullet import Bullet
from alien import Alien

#  按键弹起来
def check_keydown_events(event, aisettings, screen, ship, bullets):

    if event.key == pygame.K_RIGHT:
        ship.moving_right = True
    elif event.key == pygame.K_LEFT:
        ship.moving_left = True
    elif event.key == pygame.K_SPACE:
        fire_bullet(aisettings, screen, ship, bullets)
    elif event.key == pygame.K_q:
        sys.exit()


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

#  按键按下去
def check_keyup_events(event, ship):

    if event.key == pygame.K_RIGHT:
        ship.moving_right = False
    elif event.key == pygame.K_LEFT:
        ship.moving_left = False


def check_events(ai_settings, screen, ship, bullets):
    for event in pygame.event.get():

        if event.type == pygame.QUIT:
            print('exit')
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, screen, ship, bullets)
        elif event.type == pygame.KEYUP:
            check_keyup_events(event, ship)


def update_screen(ai_settings, screen, ship, aliens, bullets):

    screen.fill(ai_settings.bg_color)
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    ship.blitme()
    # alien.blitme()
    aliens.draw(screen)
    #  让最近绘制的屏幕可见
    pygame.display.flip()


def update_bullets(bullets):

    bullets.update()
    for bullet in bullets.copy():  # 用copy 因为下面在修改list值
        if bullet.rect.bottom <= 0:
            bullets.remove(bullet)

def create_fleet(aisettings, screen, aliens):

    alien = Alien(aisettings, screen)
    alien_width = alien.rect.width
    available_space_x = aisettings.screen_width - 2*alien_width
    number_aliens_x = int(available_space_x/(2*alien_width))

    for alien_number in range(number_aliens_x):

        alien = Alien(aisettings, screen)
        alien.x = alien_width + 2*alien_width *alien_number
        alien.rect.x = alien.x
        aliens.add(alien)

import pygame

class Ship():

    def __init__(self, aisettings, screen):
        self.screen = screen
        self.aisettings = aisettings
        self.image = pygame.image.load('images/ship.bmp')
        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
        self.center = float(self.rect.centerx)
        self.moving_right = False
        self.moving_left = False

    def blitme(self):
        self.screen.blit(self.image, self.rect)


    def update(self):

        if self.moving_right and self.rect.right < self.screen_rect.right:

            self.center += self.aisettings.ship_speed_factor
        if self.moving_left and self.rect.left > 0:
            self.center -= self.aisettings.ship_speed_factor

        self.rect.centerx = self.center
上一篇下一篇

猜你喜欢

热点阅读