引言

最近python语言大火,除了在科学计算领域python有用武之地之外,在游戏、后台等方面,python也大放异彩,本篇博文将按照正规的项目开发流程,手把手教大家写个python小游戏,来感受下其中的有趣之处。本次开发的游戏叫做alien invasion。

安装pygame并创建能左右移动的飞船

安装pygame

本人电脑是windows 10、python3.6,pygame下载地址: 传送门

请自行下载对应python版本的pygame 运行以下命令

$ pip install wheel

$ pip install pygame‑1.9.3‑cp36‑cp36m‑win_amd64.whl

创建Pygame窗口及响应用户输入

新建一个文件夹alien_invasion,并在文件夹中新建alien_invasion.py文件,输入如下代码。

import sys

import pygame

def run_game():

#initialize game and create a dispaly object

pygame.init()

screen = pygame.display.set_mode((1200,800))

pygame.display.set_caption("Alien Invasion")

# set backgroud color

bg_color = (230,230,230)

# game loop

while True:

# supervise keyboard and mouse item

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()

# fill color

screen.fill(bg_color)

# visualiaze the window

pygame.display.flip()

run_game()

运行上述代码,我们可以得到一个灰色界面的窗口:

$ python alien_invasion.py

创建设置类

为了在写游戏的过程中能便捷地创建一些新功能,下面额外编写一个settings模块,其中包含一个Settings类,用于将所有设置存储在一个地方。这样在以后项目增大时修改游戏的外观就更加容易。 我们首先将alien_invasion.py中的显示屏大小及显示屏颜色进行修改。 首先在alien_invasion文件夹下新建python文件settings.py,并向其中添加如下代码:

class Settings(object):

"""docstring for Settings"""

def __init__(self):

# initialize setting of game

# screen setting

self.screen_width = 1200

self.screen_height = 800

self.bg_color = (230,230,230)

然后再alien_invasion.py中导入Settings类,并使用相关设置,修改如下:

import sys

import pygame

from settings import Settings

def run_game():

#initialize game and create a dispaly 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")

# set backgroud color

bg_color = (230,230,230)

# game loop

while True:

# supervise keyboard and mouse item

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()

# fill color

screen.fill(ai_settings.bg_color)

# visualiaze the window

pygame.display.flip()

run_game()

添加飞船图像

接下来,我们需要将飞船加入游戏中。为了在屏幕上绘制玩家的飞船,我们将加载一幅图像,再使用Pygame()方法blit()绘制它。 在游戏中几乎可以使用各种类型的图像文件,但是使用位图(.bmp)文件最为简单,这是因为Pygame默认加载位图。虽然其他类型的图像也能加载,但是需要安装额外的库。我们推荐去免费的图片素材网站上去找图像: 传送门 。我们在主项目文件夹(alien_invasion)中新建一个文件夹叫images,将如下bmp图片放入其中。

接下来,我们创建飞船类ship.py:

import pygame

class Ship():

def __init__(self,screen):

#initialize spaceship and its location

self.screen = screen

# load bmp image and get rectangle

self.image = pygame.image.load('image/ship.bmp')

self.rect = self.image.get_rect()

self.screen_rect = screen.get_rect()

#put spaceship on the bottom of window

self.rect.centerx = self.screen_rect.centerx

self.rect.bottom = self.screen_rect.bottom

def blitme(self):

#buld the spaceship at the specific location

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

最后我们在屏幕上绘制飞船,即在alien_invasion.py文件中调用blitme方法:

import sys

import pygame

from settings import Settings

from ship import Settings

def run_game():

#initialize game and create a dispaly object

pygame.init()

ai_settings = Settings()

screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))

ship = Ship(screen)

pygame.display.set_caption("Alien Invasion")

# set backgroud color

bg_color = (230,230,230)

# game loop

while True:

# supervise keyboard and mouse item

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()

# fill color

screen.fill(ai_settings.bg_color)

ship.blitme()

# visualiaze the window

pygame.display.flip()

run_game()

重构:模块game_functions

在大型项目中,经常需要在添加新代码前重构既有代码。重构的目的是为了简化代码的结构,使其更加容易扩展。我们将实现一个game_functions模块,它将存储大量让游戏Alien invasion运行的函数。通过创建模块game_functions,可避免alien_invasion.py太长,使其逻辑更容易理解。

函数check_events()

首先我们将管理事件的代码移到一个名为check_events()的函数中,目的是为了隔离事件循环

import sys

import pygame

def check_events():

#respond to keyboard and mouse item

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()

然后我们修改alien_invasion.py代码,导入game_functions模块,并将事件循环替换成对函数check_events()的调用:

import sys

import pygame

from settings import Settings

from ship import Ship

import game_functions as gf

def run_game():

#initialize game and create a dispaly object

pygame.init()

ai_settings = Settings()

总结

以上所述是小编给大家介绍的Python写一个小游戏,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

python编程小游戏-使用Python写一个小游戏相关推荐

  1. python写游戏脚本-使用Python写一个小游戏

    引言 最近python语言大火,除了在科学计算领域python有用武之地之外,在游戏.后台等方面,python也大放异彩,本篇博文将按照正规的项目开发流程,手把手教大家写个python小游戏,来感受下 ...

  2. 使用python制作聊天框解谜游戏_使用Python写一个小游戏alien invasion!

    最近python语言大火,除了在科学计算领域python有用武之地之外,在游戏.后台等方面,python也大放异彩,本篇博文将按照正规的项目开发流程,手把手教大家写个python小游戏,来感受下其中的 ...

  3. 手机版python3h如何自制游戏_教你如何用 Python 写一个小游戏

    教你如何用 Python 写一个小游戏 引言 最近 python 语言大火, 除了在科学计算领域 python 有用武之地之外, 在游戏后台等方面, python 也大放异彩, 本篇博文将按照正规的项 ...

  4. 关于python小游戏的毕业论文_使用Python写一个小游戏

    引言 最近python语言大火,除了在科学计算领域python有用武之地之外,在游戏.后台等方面,python也大放异彩,本篇博文将按照正规的项目开发流程,手把手教大家写个python小游戏,来感受下 ...

  5. 用 Python 写一个俄罗斯方块游戏

    使用 Python 的 PyGame 库写一个俄罗斯方块游戏的逐步指南 在这篇教程中,我们会用 Python 的 PyGame 库写一个简单的俄罗斯方块游戏.里面的算法很简单,但对新手可能有一点挑战性 ...

  6. #用python的pygame写一个大富翁游戏(单机版)# 一 : 初步架构

    用python写一个简易大富翁小游戏 | 一 : 初步架构 系列文章: #用python的pygame写一个大富翁游戏(单机版)# 二:地图初始化 如题,俗话说得好,人生苦短,我用python,在大二 ...

  7. 用python写一个麻将游戏

    嗯,写一个麻将游戏的Python代码并不是什么困难的事情.首先,你需要准备一些基本的Python编程知识,然后写出一个基本的麻将游戏框架,包括设置游戏规则和定义玩家的行为.接着,你需要制作一些麻将牌, ...

  8. python俄罗斯方块算法详解_用 Python 写一个俄罗斯方块游戏 (

    @@ -2,34 +2,34 @@ > * 原文作者:[Dr Pommes](https://medium.com/@pommes) > * 译文出自:[掘金翻译计划](https://g ...

  9. Python 写一个俄罗斯方块游戏

    使用 Python 的 PyGame 库写一个俄罗斯方块游戏的逐步指南 很多人学习python,不知道从何学起. 很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手. 很多已经做案例 ...

最新文章

  1. 2012 iis php mysql_Win2012 R2 IIS8.5+PHP(FastCGI)+MySQL运行环境搭建wordpress博客教程
  2. spring 注解上传文件 @RequestParam,FormData上传文件
  3. [Teamcenter 2007 开发实战] 获取错误或提示信息
  4. url pattern
  5. 深度学习中的自动编码器:TensorFlow示例
  6. Tomcat内存溢出,解决方法
  7. 不要VIP,想看啥就看啥的在线网站!
  8. 软件测试的测试数据分析,软件测试结果归纳与分析
  9. 博弈论中SG函数的解释与运用
  10. 生物计算机是未来型计算机吗,生物计算机特点及未来发展
  11. 均衡器--时域均衡,频域均衡,无限长迫零(Zero force\ZF)均衡器,有限长时域迫零(ZF)均衡器,无限长MSE均衡器
  12. 三阶魔方7步还原法详解 简单
  13. 异常(Exception)
  14. 快递企业如何完成运单订阅消息的推送
  15. 百度地图-新手入门教程
  16. c语言编译器只有9行怎么添加,avr单片机c语言编译器(9页)-原创力文档
  17. mac下的c语言贪吃蛇
  18. Google Android 开发者网站更新了
  19. 国开《工业通风及除尘》终结性考试
  20. C++:shared_ptr简介以及常见问题

热门文章

  1. JQuery dataTable 扩展+Ajax Post,Get一些基本操作(一)
  2. Vue之动态class写法总结
  3. 使用宝塔面板部署tp5网站
  4. Golang内建库学习笔记(1)-sort和container
  5. JAVA 笔记no.2
  6. 图像修复中的TV模型
  7. Laravel 中查询 where 记录
  8. 给jar包进行数字签名(2014-06-28记)
  9. Dapper 多数据库优化
  10. windows环境中利用NMake工具编译连接C++源代码