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编写web API的教程
Apr 30 Python
Python制作爬虫抓取美女图
Jan 20 Python
Python设计模式中单例模式的实现及在Tornado中的应用
Mar 02 Python
python字典键值对的添加和遍历方法
Sep 11 Python
Python实现的简单模板引擎功能示例
Sep 02 Python
Django项目之Elasticsearch搜索引擎的实例
Aug 21 Python
详解mac python+selenium+Chrome 简单案例
Nov 08 Python
Python中logging日志库实例详解
Feb 19 Python
浅谈Python线程的同步互斥与死锁
Mar 22 Python
python3.6.5基于kerberos认证的hive和hdfs连接调用方式
Jun 06 Python
Python实现对word文档添加密码去除密码的示例代码
Dec 29 Python
Python使用Turtle模块绘制国旗的方法示例
Feb 28 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
使用array mutisort 实现按某字段对数据排序
2013/06/18 PHP
测试PHP连接MYSQL成功与否的代码
2013/08/16 PHP
PHP获取当前URL路径的处理方法(适用于多条件筛选列表)
2017/02/10 PHP
PHP自定义序列化接口Serializable用法分析
2017/12/29 PHP
一个不错的用JavaScript实现的UBB编码函数
2007/03/09 Javascript
慎用 somefunction.prototype 分析
2009/06/02 Javascript
prettify 代码高亮着色器google出品
2010/12/28 Javascript
js对象之JS入门之Array对象操作小结
2011/01/09 Javascript
JavaScript面向对象知识串结(读JavaScript高级程序设计(第三版))
2012/07/17 Javascript
Extjs4 类的定义和扩展实例
2013/06/28 Javascript
基于jquery实现发送文章到手机的代码
2014/12/26 Javascript
JS限制文本框只能输入数字和字母方法
2015/02/28 Javascript
JS实现霓虹灯文字效果的方法
2015/08/06 Javascript
js实现类似MSN提示的页面效果代码分享
2015/08/24 Javascript
Jquery插件easyUi实现表单验证示例
2015/12/15 Javascript
AngularJS手动表单验证
2016/02/01 Javascript
AngularJS出现$http异步后台无法获取请求参数问题的解决方法
2016/11/03 Javascript
详解使用fetch发送post请求时的参数处理
2017/04/05 Javascript
nodejs通过钉钉群机器人推送消息的实现代码
2019/05/05 NodeJs
详解vue中$nextTick和$forceUpdate的用法
2019/12/11 Javascript
python多进程共享变量
2016/04/06 Python
Python编程实现蚁群算法详解
2017/11/13 Python
Python+selenium实现截图图片并保存截取的图片
2018/01/05 Python
用Python将mysql数据导出成json的方法
2018/08/21 Python
Python实现App自动签到领取积分功能
2018/09/29 Python
django的auth认证,authenticate和装饰器功能详解
2019/07/25 Python
Python 列表的清空方式
2020/01/13 Python
阿根廷票务网站:StubHub阿根廷
2018/04/13 全球购物
编程实现去掉XML的重复结点
2014/05/28 面试题
法律专业个人实习自我鉴定
2013/09/23 职场文书
质检部部长职责
2013/12/16 职场文书
暑期实践思想汇报
2014/01/06 职场文书
教师节宣传方案
2014/05/23 职场文书
挂职个人工作总结
2015/03/05 职场文书
虎兄虎弟观后感
2015/06/12 职场文书
使用Python通过企业微信应用给企业成员发消息
2022/04/18 Python