pygame游戏之旅 如何制作游戏障碍


Posted in Python onNovember 20, 2018

本文为大家分享了pygame游戏之旅的第6篇,供大家参考,具体内容如下

定义一个障碍模型函数:

def things(thingx, thingy, thingw, thingh, color):
 pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])

在游戏循环中调用:

things(thing_startx, thing_starty, thing_width, thing_height, black)
thing_starty += thing_speed

障碍消失之后修改x值:

if thing_starty > display_height:
 thing_starty = 0 - thing_height
 thing_startx = random.randrange(0, display_width)

全部代码:

import pygame
import time
import random
 
pygame.init()
 
white = (255,255,255)
black = (0,0,0)
gray = (128,128,128)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
 
car_width = 100
 
display_width = 800
display_height = 600
 
 
gameDisplay = pygame.display.set_mode( (display_width,display_height) )
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
 
carImg = pygame.image.load('car.png')
 
def things(thingx, thingy, thingw, thingh, color):
 pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
 
 
 
def car(x, y):
 gameDisplay.blit(carImg, (x,y))
 
 
def text_objects(text, font):
 textSurface = font.render(text, True, white)
 return textSurface, textSurface.get_rect()
 
def message_diaplay(text):
 largeText = pygame.font.Font('freesansbold.ttf',115)
 TextSurf, TextRect = text_objects(text, largeText)
 TextRect.center = ((display_width/2),(display_height/2))
 gameDisplay.blit(TextSurf, TextRect)
 pygame.display.update()
 time.sleep(2)
 game_loop()
 
def crash():
 message_diaplay('You Crashed')
 
 
def game_loop():
 x = display_width * 0.45
 y = display_height * 0.8
 x_change = 0
 
 gameExit = False
 
 thing_startx = random.randrange(0, display_width)
 thing_starty = -600
 thing_speed = 7
 thing_width = 100
 thing_height = 100
 
 while not gameExit:
 for event in pygame.event.get():
  if event.type == pygame.QUIT:
  gameExit = True
  if event.type == pygame.KEYDOWN:
  if event.key == pygame.K_LEFT:
   x_change = -5
  elif event.key == pygame.K_RIGHT:
   x_change = 5
  if event.type == pygame.KEYUP:
  if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
   x_change = 0
  print(event)
 x += x_change
 gameDisplay.fill(white)
 
 things(thing_startx, thing_starty, thing_width, thing_height, black)
 thing_starty += thing_speed
 
 car(x,y)
 if x > display_width - car_width or x < 0:
  gameExit = True
 if thing_starty > display_height:
  thing_starty = 0 - thing_height
  thing_startx = random.randrange(0, display_width)
 pygame.display.update()
 clock.tick(60)
crash()
#game_loop()
pygame.quit()
quit()

结果图:

pygame游戏之旅 如何制作游戏障碍pygame游戏之旅 如何制作游戏障碍pygame游戏之旅 如何制作游戏障碍

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中使用 Selenium 实现网页截图实例
Jul 18 Python
Python的Flask站点中集成xhEditor文本编辑器的教程
Jun 13 Python
python DataFrame获取行数、列数、索引及第几行第几列的值方法
Apr 08 Python
使用python3构建文件传输的方法
Feb 13 Python
详解python实现数据归一化处理的方式:(0,1)标准化
Jul 17 Python
Python hmac模块使用实例解析
Dec 24 Python
python-xpath获取html文档的部分内容
Mar 06 Python
解决TensorFlow程序无限制占用GPU的方法
Jun 30 Python
浅谈anaconda python 版本对应关系
Oct 07 Python
python修改微信和支付宝步数的示例代码
Oct 12 Python
Python语法学习之进程的创建与常用方法详解
Apr 08 Python
详解NumPy中的线性关系与数据修剪压缩
May 25 Python
用Python编写一个简单的CS架构后门的方法
Nov 20 #Python
python pygame实现2048游戏
Nov 20 #Python
python pygame模块编写飞机大战
Nov 20 #Python
Python Scapy随心所欲研究TCP协议栈
Nov 20 #Python
python版飞机大战代码分享
Nov 20 #Python
pygame实现雷电游戏雏形开发
Nov 20 #Python
pygame游戏之旅 游戏中添加显示文字
Nov 20 #Python
You might like
收音机术语解释
2021/03/01 无线电
php三维数组去重(示例代码)
2013/11/26 PHP
PHP防止post重复提交数据的简单例子
2014/06/07 PHP
php生成固定长度纯数字编码的方法
2015/07/09 PHP
Javascript连接多个数组不用concat来解决
2014/03/24 Javascript
js实现的简洁网页滑动tab菜单效果代码
2015/08/24 Javascript
JS组件系列之Bootstrap Icon图标选择组件
2016/01/28 Javascript
RequireJS 依赖关系的实例(推荐)
2017/01/21 Javascript
vue 项目打包通过命令修改 vue-router 模式 修改 API 接口前缀
2018/06/13 Javascript
Vue不能检测到Object/Array更新的情况的解决
2018/06/26 Javascript
jQuery中each遍历的三种方法实例分析
2018/09/07 jQuery
JS函数节流和防抖之间的区分和实现详解
2019/01/11 Javascript
详解webpack引入第三方库的方式以及注意事项
2019/01/15 Javascript
JavaScript 正则应用详解【模式、欲查、反向引用等】
2020/05/13 Javascript
Anaconda多环境多版本python配置操作方法
2017/09/12 Python
python实现切割url得到域名、协议、主机名等各个字段的例子
2019/07/25 Python
Python字典的概念及常见应用实例详解
2019/10/30 Python
Python实现序列化及csv文件读取
2020/01/19 Python
快速查找Python安装路径方法
2020/02/06 Python
Python如何存储数据到json文件
2020/03/09 Python
python小程序基于Jupyter实现天气查询的方法
2020/03/27 Python
Python第三方库的几种安装方式(小结)
2020/04/03 Python
使用Python将语音转换为文本的方法
2020/08/10 Python
详解Python中string模块除去Str还剩下什么
2020/11/30 Python
浅谈CSS3中的变形功能-transform功能
2017/12/27 HTML / CSS
路易威登和香奈儿手袋:LuxeDH
2017/01/12 全球购物
售后服务经理岗位职责
2014/02/25 职场文书
副董事长岗位职责
2014/04/02 职场文书
幼师求职自荐信
2014/05/31 职场文书
人力资源本科毕业生求职信
2014/06/04 职场文书
公司采购主管岗位职责
2014/06/17 职场文书
大学生村官工作总结2015
2015/04/09 职场文书
工作态度检讨书范文
2015/05/06 职场文书
省级三好学生主要事迹材料
2015/11/03 职场文书
导游词之任弼时故居
2020/01/07 职场文书
Python+Pillow+Pytesseract实现验证码识别
2022/05/11 Python