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获取当前计算机cpu数量的方法
Apr 18 Python
python代码 if not x: 和 if x is not None: 和 if not x is None:使用介绍
Sep 21 Python
Python基于Flask框架配置依赖包信息的项目迁移部署
Mar 02 Python
python3实现随机数
Jun 25 Python
Python 多维List创建的问题小结
Jan 18 Python
numpy concatenate数组拼接方法示例介绍
May 27 Python
解决Django中多条件查询的问题
Jul 18 Python
Django实现发送邮件功能
Jul 18 Python
python3 selenium自动化测试 强大的CSS定位方法
Aug 23 Python
Python正则表达式如何匹配中文
May 27 Python
keras使用Sequence类调用大规模数据集进行训练的实现
Jun 22 Python
Python爬虫破解登陆哔哩哔哩的方法
Nov 17 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
如何使用Linux的Crontab定时执行PHP脚本的方法
2011/12/19 PHP
php Xdebug的安装与使用详解
2013/06/20 PHP
ThinkPHP3.1新特性之字段合法性检测详解
2014/06/19 PHP
php基于表单密码验证与HTTP验证用法实例
2015/01/06 PHP
php之static静态属性与静态方法实例分析
2015/07/30 PHP
PHP的Yii框架中过滤器相关的使用总结
2016/03/29 PHP
详解Yii2 定制表单输入字段的标签和样式
2017/01/04 PHP
PHP实现的分解质因数操作示例
2018/08/01 PHP
jQuery阻止冒泡和HTML默认操作
2010/11/17 Javascript
jquery.boxy弹出框(后隔N秒后自动隐藏/自动跳转)
2013/01/15 Javascript
js处理自己不能定义二维数组的方法详解
2014/03/03 Javascript
使用Jquery获取带特殊符号的ID 标签的方法
2014/04/30 Javascript
javascript基于prototype实现类似OOP继承的方法
2015/12/16 Javascript
jQuery实现放大镜效果实例代码
2016/03/17 Javascript
js的form表单提交url传参数(包含+等特殊字符)的两种解决方法
2016/05/25 Javascript
jQuery Ajax Post 回调函数不执行问题的解决方法
2016/08/15 Javascript
Bootstrap面板使用方法
2017/01/16 Javascript
利用js判断手机是否安装某个app的多种方案
2017/02/13 Javascript
Bootstrap的aria-label和aria-labelledby属性实例详解
2018/11/02 Javascript
JavaScript 处理树数据结构的方法示例
2019/06/16 Javascript
js实现简单五子棋游戏
2020/05/28 Javascript
python通过opencv实现批量剪切图片
2017/11/13 Python
Python 3.6 读取并操作文件内容的实例
2018/04/23 Python
python+tkinter实现学生管理系统
2019/08/20 Python
Python爬取豆瓣数据实现过程解析
2020/10/27 Python
美国汽配连锁巨头Pep Boys官网:轮胎更换、汽车维修服务和汽车零部件
2017/01/14 全球购物
SQL语言面试题
2013/08/27 面试题
维德科技C#面试题笔试题
2015/12/09 面试题
关于圣诞节的广播稿
2014/01/26 职场文书
我们的节日春节活动方案
2014/08/22 职场文书
结婚保证书
2015/01/16 职场文书
刮痧观后感
2015/06/05 职场文书
证婚人致辞精选
2015/07/28 职场文书
MySQL约束超详解
2021/09/04 MySQL
解决MySQL添加新用户-ERROR 1045 (28000)的问题
2022/03/03 MySQL
sql注入报错之注入原理实例解析
2022/06/10 MySQL