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脚本操作MongoDB的教程
Apr 16 Python
Python中内建函数的简单用法说明
May 05 Python
简单了解Django模板的使用
Dec 20 Python
python 2.7.14安装图文教程
Apr 08 Python
python 实现返回一个列表中出现次数最多的元素方法
Jun 11 Python
使用PyQt4 设置TextEdit背景的方法
Jun 14 Python
python RC4加密操作示例【测试可用】
Sep 26 Python
django实现用户注册实例讲解
Oct 30 Python
python实现计算器功能
Oct 31 Python
详解centos7+django+python3+mysql+阿里云部署项目全流程
Nov 15 Python
python 串口读取+存储+输出处理实例
Dec 26 Python
Python绘画好看的星空图
Mar 17 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
php提供实现反射的方法和实例代码
2019/09/17 PHP
映彩衣的js随笔(js图片切换效果)
2011/07/31 Javascript
JavaScript获取FCK编辑器信息的具体方法
2013/07/12 Javascript
JavaScript实现维吉尼亚(Vigenere)密码算法实例
2013/11/22 Javascript
JavaScript中的Primitive对象封装介绍
2014/12/31 Javascript
jquery动感漂浮导航菜单代码分享
2020/04/15 Javascript
jQuery实现向下滑出的二级菜单效果实例
2015/08/22 Javascript
基于AngularJS实现iOS8自带的计算器
2016/09/12 Javascript
js前端实现多图图片上传预览的两个方法(推荐)
2016/11/18 Javascript
解析利用javascript如何判断一个数为素数
2016/12/08 Javascript
解决html input验证只能输入数字,不能输入其他的问题
2017/07/21 Javascript
基于Vue过渡状态实例讲解
2017/09/14 Javascript
解决vue动态为数据添加新属性遇到的问题
2018/09/18 Javascript
如何能分清npm cnpm npx nvm
2019/01/17 Javascript
JavaScript常用内置对象用法分析
2019/07/09 Javascript
七行JSON代码把你的网站变成移动应用过程详解
2019/07/09 Javascript
vue-resource post数据时碰到Django csrf问题的解决
2020/03/13 Javascript
使用Element的InfiniteScroll 无限滚动组件报错的解决
2020/07/27 Javascript
Python中的包和模块实例
2014/11/22 Python
详解python校验SQL脚本命名规则
2019/03/22 Python
Python切图九宫格的实现方法
2019/10/10 Python
Python3常用内置方法代码实例
2019/11/18 Python
解决jupyter notebook 前面书写后面内容消失的问题
2020/04/13 Python
HTML5实现QQ聊天气泡效果
2017/06/26 HTML / CSS
英国鹦鹉店:Parrot Essentials
2018/12/03 全球购物
美国艺术和工艺品商店:Hobby Lobby
2020/12/09 全球购物
垃圾回收的优点和原理。并考虑2种回收机制
2016/10/16 面试题
文秘专业毕业生就业推荐信
2013/11/08 职场文书
建筑班组长岗位职责
2014/01/02 职场文书
招商引资工作汇报
2014/10/28 职场文书
学生党员检讨书范文
2014/12/27 职场文书
如何写辞职书
2015/02/26 职场文书
社区工作者个人总结
2015/02/28 职场文书
2015暑期爱心支教策划书
2015/07/14 职场文书
导游词之青岛崂山
2019/12/27 职场文书
uwsgi+nginx代理Django无法访问静态资源的解决
2021/05/10 Servers