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)文章
selenium+python自動(dòng)化測(cè)試之多窗口切換
這篇文章主要介紹了selenium+python自動(dòng)化測(cè)試之多窗口切換,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01Python漏洞驗(yàn)證程序Poc利用入門到實(shí)戰(zhàn)編寫
這篇文章主要為大家介紹了Python?Poc利用入門到實(shí)戰(zhàn)編寫實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02Python入門教程1. 基本運(yùn)算【四則運(yùn)算、變量、math模塊等】
這篇文章主要介紹了Python教程的基本運(yùn)算,包括四則運(yùn)算、變量的使用與類型檢測(cè)、math模塊等,并附帶了相關(guān)說(shuō)明,代碼備有較為詳盡的說(shuō)明,便于理解,需要的朋友可以參考下2018-10-10使用 Python 寫一個(gè)簡(jiǎn)易的抽獎(jiǎng)程序
這篇文章主要介紹了使用 Python 寫一個(gè)簡(jiǎn)易的抽獎(jiǎng)程序,本文通過(guò)實(shí)例代碼,思路講解的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12使用python制作游戲下載進(jìn)度條的代碼(程序說(shuō)明見(jiàn)注釋)
這篇文章主要介紹了用python制作游戲下載進(jìn)度條的代碼(程序說(shuō)明見(jiàn)注釋),代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10