pygame游戲之旅 如何制作游戲障礙
本文為大家分享了pygame游戲之旅的第6篇,供大家參考,具體內(nèi)容如下
定義一個(gè)障礙模型函數(shù):
def things(thingx, thingy, thingw, thingh, color): pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
在游戲循環(huán)中調(diào)用:
things(thing_startx, thing_starty, thing_width, thing_height, black) thing_starty += thing_speed
障礙消失之后修改x值:
if thing_starty > display_height: thing_starty = 0 - thing_height thing_startx = random.randrange(0, display_width)
全部代碼:
import pygame
import time
import random
pygame.init()
white = (255,255,255)
black = (0,0,0)
gray = (128,128,128)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
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, white)
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:
gameExit = True
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)
pygame.display.update()
clock.tick(60)
crash()
#game_loop()
pygame.quit()
quit()
結(jié)果圖:



以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python多線程semaphore實(shí)現(xiàn)線程數(shù)控制的示例
這篇文章主要介紹了python多線程semaphore實(shí)現(xiàn)線程數(shù)控制的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
TensorFlow Autodiff自動(dòng)微分詳解
這篇文章主要介紹了TensorFlow Autodiff自動(dòng)微分詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
Python3利用Dlib19.7實(shí)現(xiàn)攝像頭人臉識(shí)別的方法
這篇文章主要介紹了Python 3 利用 Dlib 19.7 實(shí)現(xiàn)攝像頭人臉識(shí)別 ,利用python開(kāi)發(fā),借助Dlib庫(kù)捕獲攝像頭中的人臉,提取人臉特征,通過(guò)計(jì)算歐氏距離來(lái)和預(yù)存的人臉特征進(jìn)行對(duì)比,達(dá)到人臉識(shí)別的目的,感興趣的小伙伴們可以參考一下2018-05-05
使用Python實(shí)現(xiàn)給企業(yè)微信發(fā)送消息功能
本文將介紹如何使用python3給企業(yè)微信發(fā)送消息,文中有詳細(xì)的圖文解說(shuō)及代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴很有幫助,需要的朋友可以參考下2021-12-12
使用Python實(shí)現(xiàn)批量發(fā)送個(gè)性化郵件
在現(xiàn)代工作環(huán)境中,我們經(jīng)常需要向多個(gè)收件人發(fā)送個(gè)性化的郵件,因此本文小編為大家整理了Python實(shí)現(xiàn)批量發(fā)送個(gè)性化郵件的示例代碼,希望對(duì)大家有所幫助2023-11-11
基于Python編寫(xiě)一個(gè)自動(dòng)關(guān)機(jī)程序
這篇文章主要介紹了基于Python編寫(xiě)的一個(gè)自動(dòng)關(guān)機(jī)程序,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定的幫助,感興趣的同學(xué)可以學(xué)習(xí)一下2022-01-01
python pandas dataframe 去重函數(shù)的具體使用
這篇文章主要介紹了python pandas dataframe 去重函數(shù)的具體使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
python pandas.DataFrame.loc函數(shù)使用詳解
這篇文章主要介紹了python pandas.DataFrame.loc函數(shù)使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Python基于callable函數(shù)檢測(cè)對(duì)象是否可被調(diào)用
這篇文章主要介紹了Python基于callable函數(shù)檢測(cè)對(duì)象是否可被調(diào)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10

