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数据结构之Array用法实例
Oct 09 Python
Python打包文件夹的方法小结(zip,tar,tar.gz等)
Sep 18 Python
解决os.path.isdir() 判断文件夹却返回false的问题
Nov 29 Python
TensorFlow 多元函数的极值实例
Feb 10 Python
在Python中通过threshold创建mask方式
Feb 19 Python
TensorFlow2.1.0最新版本安装详细教程
Apr 08 Python
python对批量WAV音频进行等长分割的方法实现
Sep 25 Python
PyCharm2020最新激活码+激活码补丁(亲测最新版PyCharm2020.2激活成功)
Nov 25 Python
Python如何实现Paramiko的二次封装
Jan 30 Python
10张动图学会python循环与递归问题
Feb 06 Python
python基于机器学习预测股票交易信号
May 25 Python
python中redis包操作数据库的教程
Apr 19 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 文件上传系统手记
2009/10/26 PHP
yii2中LinkPager增加总页数和总记录数的实例
2017/08/28 PHP
php打开本地exe程序,js打开本地exe应用程序,并传递相关参数方法
2018/02/06 PHP
jQuery 关于伪类选择符的使用说明
2013/04/24 Javascript
jquery validate 自定义验证方法介绍 日期验证
2014/02/27 Javascript
浅析Node在构建超媒体API中的作用
2014/07/30 Javascript
JQuery给网页更换皮肤的方法
2015/05/30 Javascript
jQuery Easyui Datagrid实现单行的上移下移及保存移动的结果
2016/08/15 Javascript
JavaScript数组迭代方法
2017/03/03 Javascript
php register_shutdown_function函数详解
2017/07/23 Javascript
原生js实现简单的模态框示例
2017/09/08 Javascript
基于vue展开收起动画的示例代码
2018/07/05 Javascript
开发用到的js封装方法(20种)
2018/10/12 Javascript
vue+element+Java实现批量删除功能
2019/04/08 Javascript
[52:08]DOTA2上海特级锦标赛主赛事日 - 3 败者组第三轮#2Fnatic VS OG第一局
2016/03/05 DOTA
[37:50]VP vs TNC Supermajor小组赛B组 BO3 第一场 6.2
2018/06/03 DOTA
python基于phantomjs实现导入图片
2016/05/13 Python
利用Python画ROC曲线和AUC值计算
2016/09/19 Python
python的exec、eval使用分析
2017/12/11 Python
Python实现二维数组输出为图片
2018/04/03 Python
TFRecord格式存储数据与队列读取实例
2020/01/21 Python
Edwaybuy西班牙:小米在线商店
2019/12/04 全球购物
主要的Ajax框架都有什么
2013/11/14 面试题
顶碗少年教学反思
2014/02/21 职场文书
优秀实习生感言
2014/03/01 职场文书
师范生自荐信模板
2014/05/28 职场文书
心理学专业求职信
2014/06/16 职场文书
农民工预备党员思想汇报
2014/09/14 职场文书
新员工考核评语
2014/12/31 职场文书
安全保证书怎么写
2015/02/28 职场文书
2015年预算员工作总结
2015/05/14 职场文书
学校后勤工作总结2015
2015/05/15 职场文书
2015年乡镇组织委员工作总结
2015/10/23 职场文书
那些美到让人窒息的诗句,值得你收藏!
2019/08/20 职场文书
Django使用redis配置缓存的方法
2021/06/01 Redis
zabbix配置nginx监控的实现
2022/05/25 Servers