pygame游戏之旅 计算游戏中躲过的障碍数量


Posted in Python onNovember 20, 2018

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

定义一个计数函数:

def things_dodged(count):
 font = pygame.font.SysFont(None, 25)
 text = font.render("Dodged:"+str(count), True, black)
 gameDisplay.blit(text,(0,0))

在游戏循环中加入计数,然后增加一些游戏难度,例如加速障碍,增加障碍的宽度:

dodged += 1
thing_speed += 1
thing_width += (dodged * 1.2)

全部代码:

import pygame
import time
import random
 
pygame.init()
 
white = (255,255,255)
black = (0,0,0)
 
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_dodged(count):
 font = pygame.font.SysFont(None, 25)
 text = font.render("Dodged:"+str(count), True, black)
 gameDisplay.blit(text,(0,0))
 
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, black)
 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
 
 dodged = 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:
  pygame.quit()
  quit()
  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)
 things_dodged(dodged)
 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)
  dodged += 1
  thing_speed += 1
  thing_width += (dodged * 1.2)
 if y < thing_starty + thing_height:
  print('y crossover')
  if x > thing_startx and x < thing_startx + thing_width or x + car_width > thing_startx and x + car_width < thing_startx + thing_width:
  print('x crossover')
  crash()
 pygame.display.update()
 clock.tick(60)
#crash()
game_loop()
pygame.quit()
quit()

效果图:

pygame游戏之旅 计算游戏中躲过的障碍数量

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

Python 相关文章推荐
Python中用Decorator来简化元编程的教程
Apr 13 Python
python获取当前时间对应unix时间戳的方法
May 15 Python
Python3.遍历某文件夹提取特定文件名的实例
Apr 26 Python
python实现批量解析邮件并下载附件
Jun 19 Python
Django 通过JS实现ajax过程详解
Jul 30 Python
python 3.6.7实现端口扫描器
Sep 04 Python
python scrapy重复执行实现代码详解
Dec 28 Python
python如何编写win程序
Jun 08 Python
零基础学python应该从哪里入手
Aug 11 Python
pandas apply多线程实现代码
Aug 17 Python
用Python实现童年贪吃蛇小游戏功能的实例代码
Dec 07 Python
python实现简单倒计时功能
Apr 21 Python
pygame游戏之旅 添加碰撞效果的方法
Nov 20 #Python
pygame游戏之旅 如何制作游戏障碍
Nov 20 #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
You might like
让PHP COOKIE立即生效,不用刷新就可以使用
2011/03/09 PHP
zend api扩展的php对象的autoload工具
2011/04/18 PHP
ThinkPHP框架实现数据增删改
2017/05/07 PHP
PHP微信模板消息操作示例
2017/06/29 PHP
PHP实现基于栈的后缀表达式求值功能
2017/11/10 PHP
PHPCrawl爬虫库实现抓取酷狗歌单的方法示例
2017/12/21 PHP
php DES加密算法实例分析
2019/09/18 PHP
jquery里的each使用方法详解
2010/12/22 Javascript
jQuery EasyUI API 中文文档 - PropertyGrid属性表格
2011/11/18 Javascript
js获取height和width的方法说明
2013/01/06 Javascript
jQuery Mobile的loading对话框显示/隐藏方法分享
2013/11/26 Javascript
JS+CSS实现淡入式焦点图片幻灯切换效果的方法
2015/02/26 Javascript
bootstrap按钮插件(Button)使用方法解析
2017/01/13 Javascript
react实现pure render时bind(this)隐患需注意!
2017/03/09 Javascript
详解使用nvm安装node.js
2017/07/18 Javascript
vue 的keep-alive缓存功能的实现
2018/03/22 Javascript
JavaScript canvas动画实现时钟效果
2020/02/10 Javascript
微信小程序实现自定义底部导航
2020/11/18 Javascript
[02:26]2018DOTA2亚洲邀请赛赛前采访-Newbee篇
2018/04/03 DOTA
Python3实现从文件中读取指定行的方法
2015/05/22 Python
Python实现多属性排序的方法
2018/12/05 Python
python celery分布式任务队列的使用详解
2019/07/08 Python
关于Python解包知识点总结
2020/05/05 Python
HTML5移动开发图片压缩上传功能
2016/11/09 HTML / CSS
Fanatics英国官网:美国体育电商
2018/11/06 全球购物
广州一家公司的.NET面试题
2016/06/11 面试题
new修饰符是起什么作用
2015/06/28 面试题
师范大学毕业自我鉴定
2013/11/21 职场文书
行政管理毕业生自荐信
2014/02/24 职场文书
2014审计局领导班子民主生活会对照检查材料思想汇报
2014/09/20 职场文书
检讨书范文2000字
2015/01/28 职场文书
迎新生欢迎词2015
2015/07/16 职场文书
2016思想纪律作风整顿心得体会
2016/01/23 职场文书
用python基于appium模块开发一个自动收取能量的小助手
2021/09/25 Python
python实现层次聚类的方法
2021/11/01 Python
如何Tomcat中使用ipv6地址
2022/05/06 Servers