pygame游戏之旅 添加碰撞效果的方法


Posted in Python onNovember 20, 2018

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

对car和障碍的宽高进行比较然后打印即可:

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()

全部代码:

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(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
 
 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)
  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)
  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游戏之旅 添加碰撞效果的方法pygame游戏之旅 添加碰撞效果的方法

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

Python 相关文章推荐
Python3实现的腾讯微博自动发帖小工具
Nov 11 Python
使用wxPython获取系统剪贴板中的数据的教程
May 06 Python
python文件操作之目录遍历实例分析
May 20 Python
Python文件读取的3种方法及路径转义
Jun 21 Python
Python利用ansible分发处理任务
Aug 04 Python
Python中return语句用法实例分析
Aug 04 Python
TensorFlow实现RNN循环神经网络
Feb 28 Python
详解关于Django中ORM数据库迁移的配置
Oct 08 Python
python实现京东订单推送到测试环境,提供便利操作示例
Aug 09 Python
Python shelve模块实现解析
Aug 28 Python
python连接mysql数据库并读取数据的实现
Sep 25 Python
Python类方法总结讲解
Jul 26 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
pygame实现雷电游戏雏形开发
Nov 20 #Python
You might like
PHP的password_hash()使用实例
2014/03/17 PHP
php实现无限级分类(递归方法)
2015/08/06 PHP
解析WordPress中控制用户登陆和判断用户登陆的PHP函数
2016/03/01 PHP
LaravelS通过Swoole加速Laravel/Lumen详解
2018/03/02 PHP
PHP crc32()函数讲解
2019/02/14 PHP
Yii框架中使用PHPExcel的方法分析
2019/07/25 PHP
JavaScript call apply使用 JavaScript对象的方法绑定到DOM事件后this指向问题
2011/09/28 Javascript
深入理解JavaScript系列(15) 函数(Functions)
2012/04/12 Javascript
eclipse导入jquery包后报错的解决方法
2014/02/17 Javascript
jquery图片轮播插件仿支付宝2013版全屏图片幻灯片
2014/04/03 Javascript
javascript中sort()的用法实例分析
2015/01/30 Javascript
js跨域资源共享 基础篇
2016/07/02 Javascript
jQuery新窗口打开外链接
2016/07/21 Javascript
jQuery实现点击后高亮背景固定显示的菜单效果【附demo源码下载】
2016/09/21 Javascript
简单实现JavaScript图片切换效果
2016/11/28 Javascript
jquery自定义插件结合baiduTemplate.js实现异步刷新(附源码)
2016/12/22 Javascript
基于vue实现分页/翻页组件paginator示例
2017/03/09 Javascript
Angular动态绑定样式及改变UI框架样式的方法小结
2018/09/03 Javascript
详解Ant Design of React的安装和使用方法
2018/12/27 Javascript
详解微信UnionID作用
2019/05/15 Javascript
python定时检查启动某个exe程序适合检测exe是否挂了
2013/01/21 Python
python实现将html表格转换成CSV文件的方法
2015/06/28 Python
Python实现全角半角字符互转的方法
2016/11/28 Python
python pprint模块中print()和pprint()两者的区别
2020/02/10 Python
Python垃圾回收机制三种实现方法
2020/04/27 Python
基于python 取余问题(%)详解
2020/06/03 Python
css3圆角样式分享自定义按钮样式
2013/12/27 HTML / CSS
Waterford加拿大官方网站:世界著名的水晶杯品牌
2016/11/01 全球购物
简历的自我评价范文
2014/02/04 职场文书
市场专员岗位职责
2014/02/14 职场文书
2014年学校安全工作总结
2014/11/13 职场文书
python正则表达式re.search()的基本使用教程
2021/05/21 Python
Python办公自动化之教你如何用Python将任意文件转为PDF格式
2021/06/28 Python
OpenCV实现普通阈值
2021/11/17 Java/Android
【TED出品】天梯非主流开心游1700 划水骑士
2022/03/31 魔兽争霸
详解Flutter自定义应用程序内键盘的实现方法
2022/06/14 Java/Android