引言

最近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写一个小游戏

本文地址: http://www.cppcns.com/jiaoben/python/224353.html

python50行小游戏_使用Python写一个小游戏相关推荐

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

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

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

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

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

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

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

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

  5. 【python小项目】用python写一个小工具——番茄钟

    用python写一个小工具--番茄钟 最近听到朋友说在用番茄钟,有点兴趣也想下载一个来用用,后面仔细一想这玩意做起来也不难,索性自己顺手写一个算了,在这里也分享给大家了 一.功能简述 番茄钟即番茄工作 ...

  6. python制作贪吃蛇游戏_用Python写贪吃蛇游戏的代码实例

    这篇文章主要为大家详细介绍了Python贪吃蛇游戏的编写代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 最近在学Python,想做点什么来练练手,命令行的贪吃蛇一般是C的练手项目,但是一时之间 ...

  7. python抽奖游戏_利用Python写一个抽奖程序,解密游戏内抽奖的秘密

    原标题:利用Python写一个抽奖程序,解密游戏内抽奖的秘密 前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 极客 ...

  8. python3.7魔塔游戏_用python写一个魔塔50层怪物伤害计算器

    最近迷上了玩魔塔.完了几次都没有通关.到后面的层数基本都是血量或者钥匙不够了.所以想到写一个小程序来计算攻击或者防御的提升能使受到来自怪物的伤害减少多少,这样的话就可以很直观的看出该吃红宝石还是蓝宝石 ...

  9. 如何用python写小工具_用python写一个录音小工具

    Python的paramiko,wxPython库的应用 Sound eXchange 命令行 需求 最近在给一个做语音识别的项目做QA工作.众所周知,此类人工智能方面的项目都需要一些数据收集的工作. ...

  10. 基于python的2048游戏设计_用python写一个2048游戏

    self.randomGenerateNumberself.randomGenerateNumber 然后,当玩家按下方向键(↑↓←→)时,这个二维列表要根据玩家的操作指令进行更新,主要分为两个部分: ...

最新文章

  1. Cache多核之间的一致性MESI
  2. pygame.mixer.Sound()语句提示错误:pygame.error: Unable to open file 'test.wav'
  3. Oracle按用户进行统计信息更新
  4. Python3中异常处理和try/except,try/finally的用法
  5. 手把手教我班小姐姐学java之面向对象
  6. iphone已停用怎么解锁_iPhone 已停用怎么办?使用锁屏密码需要注意
  7. Boolean占几个字节
  8. oracle 用户表、字段信息
  9. 如何在不支持双面打印的打印机上实现双面打印
  10. java服务写在哪里_【Java学习笔记】如何写一个简单的Web Service
  11. onlyoffice源码编译环境搭建破解
  12. android 微博一键关注,新浪微博怎样一键关注多个好友
  13. 工业机器人调运角度_工业机器人的应用案例
  14. Windy数 数位DP
  15. 4th 【最小生成树并查集】征兵
  16. 手机软键盘的发送按键和确认按键调用方法
  17. 智能家居之红外遥控---手机万能红外遥控器
  18. 【BBC纪录片】无人驾驶汽车的黎明(观后总结)
  19. java中hashmap遍历的几种方法
  20. COVID-19疫苗到底是如何杀死新冠病毒的?

热门文章

  1. 楼主,不知道为什么这么流行
  2. PDF to Word Document Converter for Mac(PDF格式转换必备)
  3. macOS Monterey兼容哪些Mac电脑?
  4. 在BetterZip的收藏夹中如何添加经常使用的文件夹?
  5. 从源码入手,一文带你读懂Spring AOP面向切面编程
  6. 使用Boostrap,左侧菜单栏固定宽度,右侧自适应宽度。
  7. MongoDB 复制集(Replica Set) 配置(Windows 版)
  8. Daily Scrum8, Oct. 12th
  9. c#中Hashtable用法简述
  10. 波形捕捉:(3)捕捉设备性能