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 30 Python
python进阶_浅谈面向对象进阶
Aug 17 Python
pandas.dataframe中根据条件获取元素所在的位置方法(索引)
Jun 07 Python
对python中两种列表元素去重函数性能的比较方法
Jun 29 Python
详解flask入门模板引擎
Jul 18 Python
python并发和异步编程实例
Nov 15 Python
关于python字符串方法分类详解
Aug 20 Python
Python算法中的时间复杂度问题
Nov 19 Python
Python3中的f-Strings增强版字符串格式化方法
Mar 04 Python
Python如何在循环内使用list.remove()
Jun 01 Python
Python使用scapy模块发包收包
May 07 Python
PyQt5爬取12306车票信息程序的实现
May 14 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 debug 安装技巧
2011/04/30 PHP
PHP实现的汉字拼音转换和公历农历转换类及使用示例
2014/07/01 PHP
PHP实现Session入库/存入redis的方法
2017/05/04 PHP
laravel框架添加数据,显示数据,返回成功值的方法
2019/10/11 PHP
jQuery 对象中的类数组操作
2009/04/27 Javascript
flash 得到自身url参数的代码
2009/11/15 Javascript
一款js和css代码压缩工具[附JAVA环境配置方法]
2010/04/16 Javascript
jQuery的each终止或跳过示例代码
2013/12/12 Javascript
js一维数组、多维数组和对象的混合使用方法
2016/04/03 Javascript
Ajax+FormData+javascript实现无刷新表单信息提交
2016/10/24 Javascript
mint-ui的search组件在键盘显示搜索按钮的实现方法
2017/10/27 Javascript
JS通过位运算实现权限加解密
2018/08/14 Javascript
js canvas实现橡皮擦效果
2018/12/20 Javascript
JS根据json数组多个字段排序及json数组常用操作
2019/06/06 Javascript
JavaScript实现4位随机验证码的生成
2021/01/28 Javascript
[44:22]完美世界DOTA2联赛循环赛 FTD vs PXG BO2第一场 11.01
2020/11/02 DOTA
python将html转成PDF的实现代码(包含中文)
2013/03/04 Python
python切换hosts文件代码示例
2013/12/31 Python
python实现自动更换ip的方法
2015/05/05 Python
python中引用与复制用法实例分析
2015/06/04 Python
Python时间的精准正则匹配方法分析
2017/08/17 Python
Python命令行解析模块详解
2018/02/01 Python
python实现微信自动回复机器人功能
2019/07/11 Python
CSS3 transforms应用于背景图像的解决方法
2019/04/16 HTML / CSS
马来西亚时装购物网站:ZALORA马来西亚
2017/03/14 全球购物
Onzie官网:美国时尚瑜伽品牌
2019/08/21 全球购物
Nike瑞士官网:Nike CH
2021/01/18 全球购物
信息技术培训感言
2014/03/06 职场文书
志愿者爱心公益活动策划方案
2014/09/15 职场文书
2014年党建工作汇报材料
2014/10/27 职场文书
土建施工员岗位职责
2015/04/11 职场文书
英语版自我评价,35句话轻松搞定
2019/10/08 职场文书
十二月早安励志心语大全
2019/12/03 职场文书
解决golang在import自己的包报错的问题
2021/04/29 Golang
Pytorch distributed 多卡并行载入模型操作
2021/06/05 Python
解决WINDOWS电脑开机后桌面没有任何图标
2022/04/09 数码科技