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获取电脑硬件信息及状态的实现方法
Aug 29 Python
Python中使用插入排序算法的简单分析与代码示例
May 04 Python
Python基础篇之初识Python必看攻略
Jun 23 Python
python框架django基础指南
Sep 08 Python
Python针对给定列表中元素进行翻转操作的方法分析
Apr 27 Python
Python 实现域名解析为ip的方法
Feb 14 Python
Python基本数据结构与用法详解【列表、元组、集合、字典】
Mar 23 Python
PyQt5组件读取参数的实例
Jun 25 Python
Flask框架学习笔记之消息提示与异常处理操作详解
Aug 15 Python
Django中提示消息messages的设置方式
Nov 15 Python
Django 实现外键去除自动添加的后缀‘_id’
Nov 15 Python
使用pygame编写Flappy bird小游戏
Mar 14 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
我的论坛源代码(三)
2006/10/09 PHP
PHP Ajax中文乱码问题解决方法
2009/02/27 PHP
PHP 获取MySQL数据库里所有表的实现代码
2011/07/13 PHP
浅谈web上存漏洞及原理分析、防范方法(安全文件上存方法)
2013/06/29 PHP
Laravel框架中扩展函数、扩展自定义类的方法
2014/09/04 PHP
对于Laravel 5.5核心架构的深入理解
2018/02/22 PHP
使用户点击后退按钮使效三行代码
2007/07/07 Javascript
使用Microsoft Ajax Minifier减小JavaScript文件大小的方法
2010/04/01 Javascript
js通过googleAIP翻译PHP系统的语言配置的实现代码
2011/10/17 Javascript
js弹出层包含flash 不能关闭隐藏的2种处理方法
2013/06/17 Javascript
JavaScript加强之自定义callback示例
2013/09/21 Javascript
在每个匹配元素的外部插入新元素的方法
2013/12/20 Javascript
使用原生JS实现弹出层特效
2014/12/22 Javascript
angularJS中router的使用指南
2015/02/09 Javascript
JavaScript中使用Math.PI圆周率属性的方法
2015/06/14 Javascript
jQuery+HTML5实现图片上传前预览效果
2015/08/20 Javascript
基于jquery实现省市联动效果
2015/11/23 Javascript
jQuery中JSONP的两种实现方式详解
2016/09/26 Javascript
你可能不知道的JSON.stringify()详解
2017/08/17 Javascript
微信小程序下拉刷新界面的实现
2017/09/28 Javascript
JavaScript简单实现合并两个Json对象的方法示例
2017/10/16 Javascript
js 原生判断内容区域是否滚动到底部的实例代码
2017/11/15 Javascript
JS基于贪心算法解决背包问题示例
2017/11/27 Javascript
关于Vue源码vm.$watch()内部原理详解
2019/04/26 Javascript
详解python使用pip安装第三方库(工具包)速度慢、超时、失败的解决方案
2018/12/02 Python
pandas的to_datetime时间转换使用及学习心得
2019/08/11 Python
python调用函数、类和文件操作简单实例总结
2019/11/29 Python
css3过渡_动力节点Java学院整理
2017/07/11 HTML / CSS
拥有超过850家商店的美国在线派对商店:Party City
2018/10/21 全球购物
Ariat官网:美国马靴和服装品牌
2019/12/16 全球购物
WatchShop法国:英国排名第一的独立手表零售商
2020/02/17 全球购物
敏捷开发的主要原则都有哪些
2015/04/26 面试题
美术师范毕业生自荐信
2013/11/16 职场文书
国际贸易求职信
2014/07/05 职场文书
区域销售大会开幕词
2016/03/04 职场文书
日本读研:怎样写好一篇日本研究计划书?
2019/07/15 职场文书