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的Django框架中manage命令的使用与扩展
Apr 11 Python
python下调用pytesseract识别某网站验证码的实现方法
Jun 06 Python
python 定义给定初值或长度的list方法
Jun 23 Python
python+pyqt5实现KFC点餐收银系统
Jan 24 Python
Django应用程序入口WSGIHandler源码解析
Aug 05 Python
Python django框架输入汉字,数字,字符生成二维码实现详解
Sep 24 Python
keras K.function获取某层的输出操作
Jun 29 Python
Python如何给函数库增加日志功能
Aug 04 Python
PyCharm上安装Package的实现(以pandas为例)
Sep 18 Python
python实现三种随机请求头方式
Jan 05 Python
移除Selenium中window.navigator.webdriver值
Jun 10 Python
Django框架中视图的用法
Jun 10 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 miniBB中文乱码问题解决方法
2008/11/25 PHP
一个简单php扩展介绍与开发教程
2010/08/19 PHP
PHP网页游戏学习之Xnova(ogame)源码解读(一)
2014/06/23 PHP
php获取一个变量的名字的方法
2014/09/05 PHP
使用URL传输SESSION信息
2015/07/14 PHP
thinkphp5 + ajax 使用formdata提交数据(包括文件上传) 后台返回json完整实例
2020/03/02 PHP
分享别人写的一个小型js框架
2007/08/13 Javascript
JS保存、读取、换行、转Json报错处理方法
2013/06/14 Javascript
JS下拉框内容左右移动效果的具体实现
2013/07/10 Javascript
jQuery循环遍历子节点并获取值的方法
2016/04/14 Javascript
js文件中直接alert()中文出来的是乱码的解决方法
2016/11/01 Javascript
详解js中Number()、parseInt()和parseFloat()的区别
2016/12/20 Javascript
JavaScript实现鼠标点击导航栏变色特效
2017/02/08 Javascript
详解ES6中的三种异步解决方案
2018/06/28 Javascript
用webpack4开发小程序的实现方法
2019/06/04 Javascript
在webstorm中配置less的方法详解
2020/09/25 Javascript
[07:37]DOTA2-DPC中国联赛2月2日Recap集锦
2021/03/11 DOTA
Python中使用Beautiful Soup库的超详细教程
2015/04/30 Python
PyCharm设置每行最大长度限制的方法
2019/01/16 Python
python mysql断开重连的实现方法
2019/07/26 Python
Django单元测试工具test client使用详解
2019/08/02 Python
基于pytorch的保存和加载模型参数的方法
2019/08/17 Python
Django项目后台不挂断运行的方法
2019/08/31 Python
对Pytorch中Tensor的各种池化操作解析
2020/01/03 Python
Python 解析库json及jsonpath pickle的实现
2020/08/17 Python
HTML5拖拽文件上传的示例代码
2021/03/04 HTML / CSS
全球领先的各类汽车配件零售商:Advance Auto Parts
2016/08/26 全球购物
植村秀美国官网:Shu Uemura美国
2019/03/19 全球购物
final, finally, finalize的区别
2012/03/01 面试题
结婚喜宴主持词
2014/03/14 职场文书
英文演讲稿
2014/05/15 职场文书
工作目标责任书
2014/07/23 职场文书
群众路线批评与自我批评发言稿
2014/10/16 职场文书
教代会开幕词
2015/01/28 职场文书
分家协议书范本
2016/03/22 职场文书
Python合并多张图片成PDF
2021/06/09 Python