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 相关文章推荐
python 中文乱码问题深入分析
Mar 13 Python
python实现zencart产品数据导入到magento(python导入数据)
Apr 03 Python
Python实现Const详解
Jan 27 Python
Python实现PS图像抽象画风效果的方法
Jan 23 Python
Python3之文件读写操作的实例讲解
Jan 23 Python
python使用phoenixdb操作hbase的方法示例
Feb 28 Python
python 的 scapy库,实现网卡收发包的例子
Jul 23 Python
Python类super()及私有属性原理解析
Jun 15 Python
浅谈Python 函数式编程
Jun 20 Python
Python 绘制可视化折线图
Jul 22 Python
python 如何对logging日志封装
Dec 02 Python
Python+Tkinter制作专属图形化界面
Apr 01 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
咖啡的植物学知识
2021/03/03 咖啡文化
通过ODBC连接的SQL SERVER实例
2006/10/09 PHP
深入php 正则表达式的学习探讨
2013/06/06 PHP
php中socket的用法详解
2014/10/24 PHP
smarty模板引擎中变量及变量修饰器用法实例
2015/01/22 PHP
PHP针对中英文混合字符串长度判断及截取方法示例
2017/03/31 PHP
基于Laravel 5.2 regex验证的正确写法
2019/09/29 PHP
js操作iframe兼容各种主流浏览器示例代码
2013/07/22 Javascript
JS求平均值的小例子
2013/11/29 Javascript
js获取当前时间显示在页面上并每秒刷新
2014/12/24 Javascript
使用RequireJS优化JavaScript引用代码的方法
2015/07/01 Javascript
详解react-native WebView 返回处理(非回调方法可解决)
2018/02/27 Javascript
vue中的模态对话框组件实现过程
2018/05/01 Javascript
AngularJS标签页tab选项卡切换功能经典实例详解
2018/05/16 Javascript
JavaScript创建对象的四种常用模式实例分析
2019/01/11 Javascript
layer.js open 隐藏滚动条的例子
2019/09/05 Javascript
vue中用 async/await 来处理异步操作
2020/07/18 Javascript
[45:44]完美世界DOTA2联赛PWL S2 FTD vs PXG 第一场 11.27
2020/12/01 DOTA
初步理解Python进程的信号通讯
2015/04/09 Python
python3实现全角和半角字符转换的方法示例
2017/09/21 Python
Python实现计算圆周率π的值到任意位的方法示例
2018/05/08 Python
如何使用python把ppt转换成pdf
2019/06/29 Python
12个步骤教你理解Python装饰器
2019/07/01 Python
python_matplotlib改变横坐标和纵坐标上的刻度(ticks)方式
2020/05/16 Python
python名片管理系统开发
2020/06/18 Python
Python中的With语句的使用及原理
2020/07/29 Python
世界上最大的字体市场:MyFonts
2020/01/10 全球购物
解释DataSet(ds) 和 ds as DataSet 的含义
2014/07/27 面试题
Linux机考试题
2015/07/17 面试题
EJB timer的种类
2014/10/28 面试题
教育局长自荐信范文
2013/12/22 职场文书
幼儿园新学期寄语
2014/01/18 职场文书
餐厅楼面主管岗位职责范本
2014/02/16 职场文书
审计专业自荐信范文
2014/04/21 职场文书
工程技术员岗位职责
2015/04/11 职场文书
HTML5页面打开微信小程序功能实现
2022/09/23 HTML / CSS