码迷,mamicode.com
首页 > 编程语言 > 详细

python pygame模块 打飞机游戏

时间:2017-08-30 21:38:22      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:blog   执行   飞机   style   quit   背景图   center   事件   itme   

settings.py用于设置屏幕的基本设置

# -*- coding: gbk -*- 
__author__ = HZQ
import  pygame
class  Settings():
    ‘‘‘
    存储所有设置
    ‘‘‘
    def  __init__(self):
        #屏幕大小设置
        self.screen_width = 600
        self.screen_height = 400
        #屏幕背景
        self.pwd=E:\\Python35\\images\\bg.jpg
        #飞船速度设置
        self.ship_speed_factor = 1.5
    def bg(self):
        background = pygame.image.load(self.pwd).convert()
        return background

Update_Screen.py设置屏幕背景

# -*- coding: gbk -*- 
__author__ = HZQ
import  pygame
class Update_Screen():
    def __init__(self):
        self.pwd=E:\\Python35\\images\\bg.jpg
    def up_sc(self):
        background = pygame.image.load(self.pwd).convert()

 

 

ship.py设置飞机机器位置属性

# -*- coding: gbk -*- 
__author__ = HZQ
import  pygame
class Ship():
    def __init__(self,ai_settings,screen):
        """初始化飞机并设置位子"""

        self.screen = screen
        self.ai_settings = ai_settings
        # 加载飞船图像并获取其外接矩形
        self.image = pygame.image.load(E:\\Python35\\images\\hero.gif)
        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
        #在飞船的属性center中存储小数值
        self.center = float(self.rect.centerx)
        self.bottom =float(self.rect.bottom)
        #移动标志
        self.moving_right =False
        self.moving_left =False
        self.moving_up =False
        self.moving_down =False
    def update(self):
        """根据移动标志调整飞船,不得超出屏幕"""
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.center += self.ai_settings.ship_speed_factor
        if self.moving_left and self.rect.left > 0:
            self.center -= self.ai_settings.ship_speed_factor
        if self.moving_up and self.rect.top > 0:
            self.bottom -= self.ai_settings.ship_speed_factor
        if self.moving_down and self.rect.bottom < 600:
            self.bottom += self.ai_settings.ship_speed_factor
        #根据self.center/bottom跟新rect
        self.rect.centerx=self.center
        self.rect.bottom=self.bottom
    def blitme(self):
        """在指定位置绘制飞船"""
        self.screen.blit(self.image,self.rect)

game_funtions.py  游戏主功能设置

# -*- coding: gbk -*- 
__author__ = HZQ
import  sys
import pygame
def check_keydown_events(event,ship):
    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_UP:
        ship.moving_up=True
    elif event.key == pygame.K_DOWN:
        ship.moving_down=True


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
    elif event.key == pygame.K_UP:
         ship.moving_up=False
    elif event.key == pygame.K_DOWN:
         ship.moving_down=False
        
def check_events(ship):
    """响应按键和鼠标事件"""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event,ship)
        elif event.type == pygame.KEYUP:
            check_keyup_events(event,ship)



def update_screen(ai_settings,screen,ship):
    """更新屏幕上的图像,并切换到新屏幕"""
    # 每次循环时都重绘屏幕
    #screen.fill(ai_settings.bg_color)
    ship.blitme()
    #让最近绘制屏幕可见
    pygame.display.flip()
    pygame.display.update()

alien_invasion.py  游戏执行

# -*- coding: gbk -*-
__author__ = HZQ

import sys
import pygame
from  settings import  Settings
from  ship import  Ship
import  game_functions as  gf
from  update_screen import  Update_Screen

def run_game():
# 初始化游戏并创建一个屏幕对象
    pygame.init()
    #创建实例
    ai_settings =Settings()
    screen = pygame.display.set_mode((ai_settings.screen_height, ai_settings.screen_width))
    pygame.display.set_caption("星际争霸")
    #设置背景颜色
    #bg_color =(230,230,230)
    #创建飞船
    ship=Ship(ai_settings,screen)
    #设置背景图片
    #background = pygame.image.load(ai_settings.pwd).convert()
    # 开始游戏的主循环
    while True:
        #监视键盘鼠标事件
        gf.check_events(ship)
        ship.update()
        #每次循环都重绘屏幕
        #将背景画上去
        screen.blit(ai_settings.bg(),(0,0))
        #让最近的屏幕可见
        gf.update_screen(ai_settings,screen,ship)
run_game()

当前实现屏幕绘制,飞机键盘按键控制上下左右,马克,待续

技术分享

python pygame模块 打飞机游戏

标签:blog   执行   飞机   style   quit   背景图   center   事件   itme   

原文地址:http://www.cnblogs.com/HZQHZA/p/7455274.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!