pygame游戏之旅 添加碰撞效果的方法
作者:观月执白 发布时间:2022-11-04 06:45:46
标签:pygame,碰撞效果
本文为大家分享了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()
结果图:
来源:https://blog.csdn.net/pianzang5201/article/details/78306910
![](https://www.aspxhome.com/images/zang.png)
![](https://www.aspxhome.com/images/jiucuo.png)
猜你喜欢
- 下拉框连动JavaScript代码,市区二级联动多级联动下拉选择框,动态获取下一级琥珀无限级联动菜单-JavaScript版 <htm
- 本文实例讲述了Python Web框架之Django框架cookie和session用法。分享给大家供大家参考,具体如下:part 1 概念
- 1、str.split不支持正则及多个切割符号,不感知空格的数量,比如用空格切割,会出现下面情况。>>> s1="
- 首先要注册一个账号密码,通过账号密码登录,并且滑块验证,自动输入搜索关键词,进行跳转翻页爬取数据,并保存到Excel文件中。代码运行时,滑块
- #-*- coding: utf-8 -*-import win32api,win32gui, win32conimport osimpor
- 顾名思义,本期内容肯定是涉及编程时间,那在操作python要怎么用time这个方法呢?一起来看下吧~时间模块的定义与使用:时间模块time是
- 目前SQL INJECTION的攻击测试愈演愈烈,很多大型的网站和论坛都相继被注入。这些网站一般使用的多为SQL SERVER数据库,正因为
- 参数Parameters解析响应时间resolveTimeout 数据类型:长整型。简单地说就是程序对目标主机的名字解析解析的一个过程时间。
- 删除字符串最后一个字符的方法1.使用strip()方法删除最后一个字符Python strip() 方法用于移除字符串头尾指定的字符(默认为
- 为什么很多站长开始做英文网站,我想主要是原因是良好的互联网环境让大家更容易赚到钱,中小站长做英文网站大致为两类,一是电子商务的外贸网站,二是
- 1.单列运算在Pandas中,DataFrame的一列就是一个Series, 可以通过map来对一列进行操作:df['col2
- 这几年来,我们这个行业一直都在谈用户体验,以用户为中心的设计。“用户体验”是指用户访问网站的界面、功能、相关信息的可读性、操作的方便性,交互
- 中文字体设计发展到现在,风格越来越多样化,特别是在广告(美术)字体方面,因为字数少 局限小,优秀的作品层出不穷,比较突出的应用在标志设计唱片
- 我就废话不多说了,大家还是直接看代码吧~#!/usr/bin/env python# encoding: utf-8''
- 本文讲述了python开发之IDEL(Python GUI)的使用方法。分享给大家供大家参考,具体如下:在安装完Python后,我们希望能够
- PyQt5信号与槽高级自定义信号与槽所谓高级自定义信号与槽,指的就是我们可以以自己喜欢的方式定义信号与槽函数,并传递参数,自定义信号的一般流
- 最近要做数据库同步,如果网上找了例子,成功,记录下来,下回再看。这个是网上找的一编文章。以下配置在本机上已经成功:实现功能:A为主服务器,B
- 分区视图联接来自一组成员的水平分区数据,使数据看起来象来自同一张表。SQL Server 2000 区分本地分区视图和分布式分区视图。在本地
- 一、简介time模块提供各种操作时间的函数说明:一般有两种表示时间的方式:第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒
- 看看下面的w3 upload组件例子,很简单: upload.asp<html> <head>