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é)果圖:



以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python3 圖片referer防盜鏈的實(shí)現(xiàn)方法
本篇文章主要介紹了python3 圖片referer防盜鏈的實(shí)現(xiàn)方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
Python?Pandas讀取Excel日期數(shù)據(jù)的異常處理方法
Excel文件是傳統(tǒng)的數(shù)據(jù)格式,但面對(duì)海量數(shù)據(jù)時(shí),用編程的方法來處理數(shù)據(jù)更有優(yōu)勢(shì),下面這篇文章主要給大家介紹了關(guān)于Python?Pandas讀取Excel日期數(shù)據(jù)的異常處理方法,需要的朋友可以參考下2022-02-02
使用python如何實(shí)現(xiàn)泛型函數(shù)
這篇文章主要介紹了使用python如何實(shí)現(xiàn)泛型函數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
從列表或字典創(chuàng)建Pandas的DataFrame對(duì)象的方法
這篇文章主要介紹了從列表或字典創(chuàng)建Pandas的DataFrame對(duì)象的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
keras實(shí)現(xiàn)theano和tensorflow訓(xùn)練的模型相互轉(zhuǎn)換
這篇文章主要介紹了keras實(shí)現(xiàn)theano和tensorflow訓(xùn)練的模型相互轉(zhuǎn)換,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-06-06
如何利用python實(shí)現(xiàn)圖片轉(zhuǎn)化字符畫
這篇文章主要介紹了如何利用python實(shí)現(xiàn)圖片轉(zhuǎn)化字符畫,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
VS2019+python3.7+opencv4.1+tensorflow1.13配置詳解
這篇文章主要介紹了VS2019+python3.7+opencv4.1+tensorflow1.13配置詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04
django實(shí)現(xiàn)用戶注冊(cè)實(shí)例講解
在本篇文章里小編給大家整理的是關(guān)于django用戶注冊(cè)的相關(guān)實(shí)例內(nèi)容,有興趣的朋友們學(xué)習(xí)下。2019-10-10

