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实现正则匹配检索远端FTP目录下的文件
Mar 25 Python
Python基于checksum计算文件是否相同的方法
Jul 09 Python
Python语言生成水仙花数代码示例
Dec 18 Python
Sanic框架配置操作分析
Jul 17 Python
Python GUI布局尺寸适配方法
Oct 11 Python
Python正则匹配判断手机号是否合法的方法
Dec 09 Python
Form表单及django的form表单的补充
Jul 25 Python
Python 项目转化为so文件实例
Dec 23 Python
Python中文分词库jieba,pkusegwg性能准确度比较
Feb 11 Python
python 解决tqdm模块不能单行显示的问题
Feb 19 Python
解决Python pip 自动更新升级失败的问题
Feb 21 Python
Python中的套接字编程是什么?
Jun 21 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
Memcache 在PHP中的使用技巧
2010/02/08 PHP
PHP为表单获取的URL 地址预设 http 字符串函数代码
2010/05/26 PHP
用PHP读取超大文件的实例代码
2012/04/01 PHP
Avengerls vs KG BO3 第二场2.18
2021/03/10 DOTA
CSDN轮换广告图片轮换效果
2007/03/27 Javascript
把textarea中字符串里含有的回车换行替换成&amp;lt;br&amp;gt;的javascript代码
2007/04/20 Javascript
javascript实现的动态文字变换
2007/07/28 Javascript
JS对select控件option选项的增删改查示例代码
2013/10/21 Javascript
基于RequireJS和JQuery的模块化编程日常问题解析
2016/04/14 Javascript
AngularJS 入门教程之事件处理器详解
2016/08/19 Javascript
js实现select选择框效果及美化
2016/08/19 Javascript
canvas红包照片实例分享
2017/02/28 Javascript
Vue.js实现移动端短信验证码功能
2017/03/29 Javascript
JQuery实现定时刷新功能代码
2017/05/09 jQuery
Vue和Bootstrap的整合思路详解
2017/06/30 Javascript
vue路由跳转时判断用户是否登录功能的实现
2017/10/26 Javascript
Node.js net模块功能及事件监听用法分析
2019/01/05 Javascript
websocket4.0+typescript 实现热更新的方法
2019/08/14 Javascript
layui-select动态选中值的例子
2019/09/23 Javascript
JS实现贪吃蛇游戏
2019/11/15 Javascript
JavaScript实现猜数字游戏
2020/05/20 Javascript
Node.js API详解之 repl模块用法实例分析
2020/05/25 Javascript
Openlayers3实现车辆轨迹回放功能
2020/09/29 Javascript
Python中将字典转换为XML以及相关的命名空间解析
2015/10/15 Python
Python判断中文字符串是否相等的实例
2018/07/06 Python
python编辑用户登入界面的实现代码
2018/07/16 Python
Python编程中类与类的关系详解
2019/08/08 Python
pygame实现成语填空游戏
2019/10/29 Python
Boston Proper官网:美国女装品牌
2017/10/30 全球购物
优秀中学生事迹材料
2014/01/31 职场文书
给面试官的感谢信
2014/02/01 职场文书
二年级评语大全
2014/04/23 职场文书
驻村工作简报
2015/07/20 职场文书
2016高考感言
2015/08/01 职场文书
自考生自我评价
2019/06/21 职场文书
php 获取音视频时长,PHP 利用getid3 获取音频文件时长等数据
2021/04/01 PHP