文章

Python利用Pygame库制作飞机大战游戏

自己写了些,然后发现一下子写不完,先暂时存储一下几个文件,明天继续接着写..(其实就是照抄)
aline_invasion.py

#coding:gbk
#import sys

import pygame

from settings import Settings

from ship import Ship

import game_functions as gf

def run_game():
    #初始化pygame,设置,屏幕对象
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
    pygame.display.set_caption("灰机大战")

    #创建一艘飞船
    ship = Ship(screen)

    #设置背景色
    bg_color = (230, 230, 230)

    #开始游戏主循环
    while True:
        gf.check_events()
        #每次循环时都重新绘制屏幕
        gf.update_screen(ai_settings,screen,ship)


        #让最近绘制的屏幕可见
        pygame.display.flip()
run_game()

settings.py

#coding:gbk
class Settings():
    #存储游戏的所有设置的类

    def __init__(self):
        #初始化游戏设置
        #屏幕设置
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230,230,230)


ship.py

#coding:gbk
import pygame

class Ship():

    def __init__(self,screen):
        #初始化飞船并设置其初始位置
        self.screen = screen

        #加载飞船图形并获取其外接矩形
        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

    def blitme(self):
        #在指定位置绘制飞船
        self.screen.blit(self.image,self.rect)

game_functions.py

#coding:gbk
import sys
import pygame

def check_events():
    #响应按键和鼠标事件

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

def update_screen(ai_settings,screen,ship):
    #更新屏幕的图像,并切换到新屏幕
    #每次循环都重新绘制屏幕
    screen.fill(ai_settings.bg_color)
    ship.blitme()

    #让最近绘制的屏幕可见
    pygame.display.flip()


原文来自:Python利用Pygame库制作飞机大战游戏,尊重自己,尊重每一个人;转发请注明来源!
0 0

发表评论