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随机生成指定长度密码的方法
Apr 04 Python
Python的Flask框架中集成CKeditor富文本编辑器的教程
Jun 13 Python
python使用itchat实现手机控制电脑
Feb 22 Python
基于numpy.random.randn()与rand()的区别详解
Apr 17 Python
python3使用flask编写注册post接口的方法
Dec 28 Python
在Python 中同一个类两个函数间变量的调用方法
Jan 31 Python
python基础知识(一)变量与简单数据类型详解
Apr 17 Python
Pandas的read_csv函数参数分析详解
Jul 02 Python
Python Request爬取seo.chinaz.com百度权重网站的查询结果过程解析
Aug 13 Python
使用python的pyplot绘制函数实例
Feb 13 Python
Python如何定义有默认参数的函数
Aug 10 Python
通过实例了解python__slots__使用方法
Sep 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
PHP4实际应用经验篇(5)
2006/10/09 PHP
在PHP中使用Sockets 从Usenet中获取文件
2008/01/10 PHP
php中判断文件存在是用file_exists还是is_file的整理
2012/09/12 PHP
访问编码后的中文URL返回404错误的解决方法
2014/08/20 PHP
php上传大文件失败的原因及应对策略
2015/10/20 PHP
php实现微信公众平台发红包功能
2018/06/14 PHP
显示、隐藏密码
2006/07/01 Javascript
基于jquery的让页面控件不可用的实现代码
2010/04/27 Javascript
jQuery的运行机制和设计理念分析
2011/04/05 Javascript
JQuery 自定义CircleAnimation,Animate方法学习笔记
2011/07/10 Javascript
js获取php变量的实现代码
2013/08/10 Javascript
实现51Map地图接口(示例代码)
2013/11/22 Javascript
jQuery删除节点用法示例(remove方法)
2016/09/08 Javascript
JS添加或修改控件的样式(Class)实现方法
2016/10/15 Javascript
JS表单数据验证的正则表达式(常用)
2017/02/18 Javascript
vue.js异步上传文件前后端实现代码
2017/08/22 Javascript
基于vue实现分页效果
2017/11/06 Javascript
小程序图片长按识别功能的实现方法
2018/08/30 Javascript
vue中过滤器filter的讲解
2019/01/21 Javascript
js获取图片的base64编码并压缩
2020/12/05 Javascript
wxPython中文教程入门实例
2014/06/09 Python
Python实现的使用telnet登陆聊天室实例
2015/06/17 Python
Python脚本实现自动将数据库备份到 Dropbox
2017/02/06 Python
使用Python快速搭建HTTP服务和文件共享服务的实例讲解
2018/06/04 Python
python“静态”变量、实例变量与本地变量的声明示例
2020/11/13 Python
详解移动端h5页面根据屏幕适配的四种方案
2020/04/15 HTML / CSS
日本整理专家Marie Kondo的官方在线商店:KonMari
2020/06/29 全球购物
工商管理实习自我鉴定
2013/09/28 职场文书
银行财务部实习生的自我鉴定
2013/11/27 职场文书
争论的故事教学反思
2014/02/06 职场文书
《陶罐和铁罐》教学反思
2014/02/19 职场文书
庆祝国庆节演讲稿2014
2014/09/19 职场文书
初中军训感言
2015/08/01 职场文书
Python趣味挑战之实现简易版音乐播放器
2021/05/28 Python
MySQL系列之十三 MySQL的复制
2021/07/02 MySQL
python人工智能human learn绘图可创建机器学习模型
2021/11/23 Python