当前位置:Gxlcms > Python > 使用Python开发简单的小游戏

使用Python开发简单的小游戏

时间:2021-07-01 10:21:17 帮助过:84人阅读

这次给大家带来使用Python开发简单的小游戏,使用Python开发小游戏的注意事项有哪些,下面就是实战案例,一起来看一下。

引言

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

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

安装pygame

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

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

  1. $ pip install wheel
  2. $ pip install pygame‑1.9.3‑cp36‑cp36m‑win_amd64.whl

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

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

  1. import sys
  2. import pygame
  3. def run_game():
  4. #initialize game and create a dispaly object
  5. pygame.init()
  6. screen = pygame.display.set_mode((1200,800))
  7. pygame.display.set_caption("Alien Invasion")
  8. # set backgroud color
  9. bg_color = (230,230,230)
  10. # game loop
  11. while True:
  12. # supervise keyboard and mouse item
  13. for event in pygame.event.get():
  14. if event.type == pygame.QUIT:
  15. sys.exit()
  16. # fill color
  17. screen.fill(bg_color)
  18. # visualiaze the window
  19. pygame.display.flip()
  20. run_game()

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

$ python alien_invasion.py

创建设置类

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

  1. class Settings(object):
  2. """docstring for Settings"""
  3. def init(self):
  4. # initialize setting of game
  5. # screen setting
  6. self.screen_width = 1200
  7. self.screen_height = 800
  8. self.bg_color = (230,230,230)

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

  1. import sys
  2. import pygame
  3. from settings import Settings
  4. def run_game():
  5. #initialize game and create a dispaly object
  6. pygame.init()
  7. ai_settings = Settings()
  8. screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
  9. pygame.display.set_caption("Alien Invasion")
  10. # set backgroud color
  11. bg_color = (230,230,230)
  12. # game loop
  13. while True:
  14. # supervise keyboard and mouse item
  15. for event in pygame.event.get():
  16. if event.type == pygame.QUIT:
  17. sys.exit()
  18. # fill color
  19. screen.fill(ai_settings.bg_color)
  20. # visualiaze the window
  21. pygame.display.flip()
  22. run_game()

添加飞船图像

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

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

  1. import pygame
  2. class Ship():
  3. def init(self,screen):
  4. #initialize spaceship and its location
  5. self.screen = screen
  6. # load bmp image and get rectangle
  7. self.image = pygame.image.load('image/ship.bmp')
  8. self.rect = self.image.get_rect()
  9. self.screen_rect = screen.get_rect()
  10. #put spaceship on the bottom of window
  11. self.rect.centerx = self.screen_rect.centerx
  12. self.rect.bottom = self.screen_rect.bottom
  13. def blitme(self):
  14. #buld the spaceship at the specific location
  15. self.screen.blit(self.image,self.rect)

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

  1. import sys
  2. import pygame
  3. from settings import Settings
  4. from ship import Settings
  5. def run_game():
  6. #initialize game and create a dispaly object
  7. pygame.init()
  8. ai_settings = Settings()
  9. screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
  10. ship = Ship(screen)
  11. pygame.display.set_caption("Alien Invasion")
  12. # set backgroud color
  13. bg_color = (230,230,230)
  14. # game loop
  15. while True:
  16. # supervise keyboard and mouse item
  17. for event in pygame.event.get():
  18. if event.type == pygame.QUIT:
  19. sys.exit()
  20. # fill color
  21. screen.fill(ai_settings.bg_color)
  22. ship.blitme()
  23. # visualiaze the window
  24. pygame.display.flip()
  25. run_game()

重构:模块game_functions

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

函数check_events()

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

  1. import sys
  2. import pygame
  3. def check_events():
  4. #respond to keyboard and mouse item
  5. for event in pygame.event.get():
  6. if event.type == pygame.QUIT:
  7. sys.exit()

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

  1. import sys
  2. import pygame
  3. from settings import Settings
  4. from ship import Ship
  5. import game_functions as gf
  6. def run_game():
  7. #initialize game and create a dispaly object
  8. pygame.init()
  9. ai_settings = Settings()

相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!

推荐阅读:

python怎么逐行读写txt文件

Python调用mysql更新数据的方法

以上就是使用Python开发简单的小游戏的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行