pygame游戲之旅 如何制作游戲障礙
更新時間:2018年11月20日 14:39:05 作者:觀月執(zhí)白
這篇文章主要為大家詳細介紹了pygame游戲之旅的第6篇,教大家如何制作游戲障礙,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文為大家分享了pygame游戲之旅的第6篇,供大家參考,具體內容如下
定義一個障礙模型函數(shù):
def things(thingx, thingy, thingw, thingh, color): pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
在游戲循環(huán)中調用:
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()
結果圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
python多線程semaphore實現(xiàn)線程數(shù)控制的示例
這篇文章主要介紹了python多線程semaphore實現(xiàn)線程數(shù)控制的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-08-08Python3利用Dlib19.7實現(xiàn)攝像頭人臉識別的方法
這篇文章主要介紹了Python 3 利用 Dlib 19.7 實現(xiàn)攝像頭人臉識別 ,利用python開發(fā),借助Dlib庫捕獲攝像頭中的人臉,提取人臉特征,通過計算歐氏距離來和預存的人臉特征進行對比,達到人臉識別的目的,感興趣的小伙伴們可以參考一下2018-05-05使用Python實現(xiàn)給企業(yè)微信發(fā)送消息功能
本文將介紹如何使用python3給企業(yè)微信發(fā)送消息,文中有詳細的圖文解說及代碼示例,對正在學習python的小伙伴很有幫助,需要的朋友可以參考下2021-12-12python pandas dataframe 去重函數(shù)的具體使用
這篇文章主要介紹了python pandas dataframe 去重函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07python pandas.DataFrame.loc函數(shù)使用詳解
這篇文章主要介紹了python pandas.DataFrame.loc函數(shù)使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03Python基于callable函數(shù)檢測對象是否可被調用
這篇文章主要介紹了Python基于callable函數(shù)檢測對象是否可被調用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10