最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pygame游戲之旅 添加碰撞效果的方法

 更新時(shí)間:2018年11月20日 15:09:01   作者:觀月執(zhí)白  
這篇文章主要為大家詳細(xì)介紹了pygame游戲之旅的第7篇,教大家如何添加碰撞的效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家分享了pygame游戲之旅的第7篇,供大家參考,具體內(nèi)容如下

對(duì)car和障礙的寬高進(jìn)行比較然后打印即可:

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()

結(jié)果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 運(yùn)用Python的webbrowser實(shí)現(xiàn)定時(shí)打開(kāi)特定網(wǎng)頁(yè)

    運(yùn)用Python的webbrowser實(shí)現(xiàn)定時(shí)打開(kāi)特定網(wǎng)頁(yè)

    今天小編就為大家分享一篇運(yùn)用Python的webbrowser實(shí)現(xiàn)定時(shí)打開(kāi)特定網(wǎng)頁(yè),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • 利用python獲取Ping結(jié)果示例代碼

    利用python獲取Ping結(jié)果示例代碼

    這篇文章主要給大家介紹了關(guān)于利用python獲取Ping結(jié)果的相關(guān)資料,文中給出了詳細(xì)的示例代碼供大家參考學(xué)習(xí),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。
    2017-07-07
  • python實(shí)現(xiàn)批量處理將圖片粘貼到另一張圖片上并保存

    python實(shí)現(xiàn)批量處理將圖片粘貼到另一張圖片上并保存

    今天小編就為大家分享一篇python實(shí)現(xiàn)批量處理將圖片粘貼到另一張圖片上并保存,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • 利用Python實(shí)現(xiàn)讀取Word文檔里的Excel附件

    利用Python實(shí)現(xiàn)讀取Word文檔里的Excel附件

    這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)讀取Word文檔里的Excel附件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2022-12-12
  • Pycharm中如何關(guān)掉python console

    Pycharm中如何關(guān)掉python console

    這篇文章主要介紹了Pycharm中如何關(guān)掉python console,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • Django使用list對(duì)單個(gè)或者多個(gè)字段求values值實(shí)例

    Django使用list對(duì)單個(gè)或者多個(gè)字段求values值實(shí)例

    這篇文章主要介紹了Django使用list對(duì)單個(gè)或者多個(gè)字段求values值實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • 一文詳解Python中subprocess模塊的用法

    一文詳解Python中subprocess模塊的用法

    Python的subprocess模塊是一個(gè)非常強(qiáng)大的工具,用于啟動(dòng)和與外部進(jìn)程進(jìn)行交互,本文將為大家詳細(xì)介紹?subprocess模塊的各個(gè)方面,希望對(duì)大家有所幫助
    2023-11-11
  • python實(shí)現(xiàn)簡(jiǎn)單俄羅斯方塊游戲

    python實(shí)現(xiàn)簡(jiǎn)單俄羅斯方塊游戲

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單俄羅斯方塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 如何基于Python深度圖生成3D點(diǎn)云詳解

    如何基于Python深度圖生成3D點(diǎn)云詳解

    通常使用TOF等3d攝像頭采集的格式一般只是深度圖,下面這篇文章主要給大家介紹了關(guān)于如何基于Python深度圖生成3D點(diǎn)云的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • Python偏函數(shù)實(shí)現(xiàn)原理及應(yīng)用

    Python偏函數(shù)實(shí)現(xiàn)原理及應(yīng)用

    這篇文章主要介紹了Python偏函數(shù)實(shí)現(xiàn)原理及應(yīng)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11

最新評(píng)論

潍坊市| 桂平市| 壶关县| 南乐县| 台江县| 沁源县| 侯马市| 大同市| 贡觉县| 伊吾县| 景东| 留坝县| 阳原县| 潞西市| 阳江市| 石狮市| 仙居县| 酒泉市| 阆中市| 乐安县| 达尔| 古田县| 普兰县| 平潭县| 盐源县| 抚顺市| 禹城市| 滨海县| 株洲县| 麦盖提县| 平南县| 黔南| 睢宁县| 岳阳市| 云林县| 竹溪县| 绥宁县| 蛟河市| 土默特左旗| 福建省| 鸡西市|