pygame游戲之旅 添加碰撞效果的方法
本文為大家分享了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í)打開特定網(wǎng)頁
今天小編就為大家分享一篇運(yùn)用Python的webbrowser實(shí)現(xiàn)定時(shí)打開特定網(wǎng)頁,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-02-02
python實(shí)現(xiàn)批量處理將圖片粘貼到另一張圖片上并保存
今天小編就為大家分享一篇python實(shí)現(xiàn)批量處理將圖片粘貼到另一張圖片上并保存,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12
利用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,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
Django使用list對(duì)單個(gè)或者多個(gè)字段求values值實(shí)例
這篇文章主要介紹了Django使用list對(duì)單個(gè)或者多個(gè)字段求values值實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
python實(shí)現(xiàn)簡(jiǎn)單俄羅斯方塊游戲
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)單俄羅斯方塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Python偏函數(shù)實(shí)現(xiàn)原理及應(yīng)用
這篇文章主要介紹了Python偏函數(shù)實(shí)現(xiàn)原理及應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11

