生活随笔
收集整理的這篇文章主要介紹了
Hotdog 热狗大战
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
HOTDOG CONFLICT
廢話介紹 背景介紹
創(chuàng)作背景
19號(hào)磨磨蹭蹭地在重慶考完了雅思,終于有機(jī)會(huì)打開積灰已久的CSDN記錄一下“代碼人生"了哈哈哈。未來的一個(gè)月還要同時(shí)進(jìn)行AP的學(xué)習(xí),加油啊啊啊!
項(xiàng)目背景
這個(gè)項(xiàng)目是寒假參加“發(fā)現(xiàn)杯”前的復(fù)習(xí)項(xiàng)目,覺得還不錯(cuò)就想把它搬上來分享分享。
【聲明】非原創(chuàng),受小學(xué)時(shí)候玩的“金山打字通”接蘋果游戲、和wsc吃熱狗的啟發(fā)
整體思路
- 準(zhǔn)備工作(搭建環(huán)境,例如導(dǎo)入庫、設(shè)置標(biāo)題、窗框大小坐標(biāo)等等)
- 設(shè)置處理事件類型
- 通過“類”的方式創(chuàng)造組件,并進(jìn)行調(diào)用
- 后臺(tái)處理
準(zhǔn)備工作
導(dǎo)入庫大家都應(yīng)該很熟練,這里我們也很熟練地操作一下~
(玄武黑,感覺比簡(jiǎn)約白更高級(jí)炫酷欸哈哈哈,高光也很棒orz)
import pygame
from pygame
.locals import *
import time
, random
import sys
import os
接下來的一步,創(chuàng)建窗口和設(shè)置標(biāo)題我們?cè)赥ime Management里面也見到過,這里不難也比較標(biāo)準(zhǔn),不用過多進(jìn)行思維上的理解。
值得一提的是,最后一行(255,255,255)表示的是邊框的顏色,分別對(duì)應(yīng)“紅黃藍(lán)”三原色的占比(此處代表白色),(0, 0, 0)的配比則是黑色。
pygame
.init
()
os
.environ
['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (100, 25)
canvas
= pygame
.display
.set_mode
((1000, 625))
canvas
.fill
((255, 255, 255))
窗口標(biāo)題的設(shè)置也很常規(guī)~
pygame
.display
.set_caption
("熱狗大戰(zhàn)")
(哈哈哈說一句題外話,我真的好喜歡Markdown:)
在完成外框的設(shè)置之后,我們就要開始逐步傳輸圖片了。我們事先會(huì)將圖片儲(chǔ)存在同級(jí)別的文件夾里,運(yùn)用調(diào)動(dòng)相對(duì)路徑的方式將它們傳輸上去。
(如何區(qū)分相對(duì)路徑和絕對(duì)路徑呢?
絕對(duì)路徑跟快遞收貨地址很像,就像是衛(wèi)星定位一樣,需要提供確切的位置。在電腦中的絕對(duì)路徑就相當(dāng)于“C盤 XXX文件夾”
而相對(duì)路徑適用于描述某個(gè)相鄰的事物,例如當(dāng)你知道我的位置時(shí),我通知你來到我的鄰居家,你也能很方便清晰地找到它。)
bg
= pygame
.image
.load
("images/bg.jpg")
h
= pygame
.image
.load
("images/hotdog.png")
player
= pygame
.image
.load
("images/hero.png")
end
= pygame
.image
.load
("images/end.jpg")
bullet_tip
= pygame
.image
.load
("images/bullet_tip.png")
time_tip
= pygame
.image
.load
("images/time_tip.png")
到此為止準(zhǔn)備任務(wù)就到此結(jié)束啦,我們接下來進(jìn)行事件處理類型的設(shè)置。
下一步是對(duì)用戶操作動(dòng)作的設(shè)置,例如如果按下了移動(dòng)箭頭的按鍵,程序會(huì)進(jìn)行怎樣的變化。用到的是比較簡(jiǎn)單的if-elif-else語句,所以也很便于理解~
def handleEvent():for event
in pygame
.event
.get
(): if event
.type == QUIT
: pygame
.quit
() sys
.exit
() if event
.type == KEYDOWN
: if event
.key
== 276: Var
.wsc
.x
-= 40 if event
.key
== 275: Var
.wsc
.x
+= 40 if Var
.wsc
.x
< 0: Var
.wsc
.x
= 0 if Var
.wsc
.x
> 1000 - Var
.wsc
.width
: Var
.wsc
.x
= 1000 - Var
.wsc
.width
這時(shí) 重點(diǎn)就來了!!!
一般我們想用某個(gè)庫/工具包里的功能時(shí),我們直接用庫名.方法名() 的方法進(jìn)行調(diào)用;但如果此時(shí)我們想調(diào)用的功能還沒有被創(chuàng)建出來,但每一次書寫都特別麻煩時(shí),我們就可以用def的方法自己創(chuàng)造工具包。
例如這里,我們想創(chuàng)造一個(gè)“字體”方法,之后設(shè)置字體的時(shí)候就可以直接調(diào)用 。
def fillText(text
, position
, view
=canvas
):my_font
= pygame
.font
.Font
("my_font/font1.ttf", 30)text
= my_font
.render
(text
, True, (255, 255, 255))view
.blit
(text
, position
)
- 第一排括號(hào)里的(text, position, view=canvas)是形式參數(shù),相當(dāng)于只是一個(gè)名字而已,沒有任何實(shí)際意義。之后在調(diào)用這個(gè)方法時(shí),再將實(shí)際參數(shù)傳輸進(jìn)去。
- 第二行代碼是進(jìn)行字體的設(shè)置,前者是字體類型,后者是字體大小
- 下一行的第一個(gè)元素代表內(nèi)容;第二個(gè)元素True代表“抗鋸齒效果”,如果設(shè)置為False或者忽略設(shè)置,就會(huì)看到一群馬賽克在晃動(dòng);后面的3個(gè)255我們?cè)谇懊嬉呀?jīng)提到過啦~)
- 最后一行就是傳輸文字,包含內(nèi)容和位置坐標(biāo)。
class object
這里比較特別,是用class語句將特定功能*“封裝”*起來,也是方便之后的調(diào)用。
def括號(hào)內(nèi)的內(nèi)容也是形式參數(shù),后面的傳參就是“實(shí)例化方法”。
就像是設(shè)置的一個(gè)一個(gè)工具包,把代碼分成一塊一塊的,再來調(diào)用。
class HotDog():def __init__(self
, x
, y
, width
, height
, img
): self
.x
= x self
.y
= y self
.width
= width self
.height
= height self
.img
= img self
.canDelete
= False def paint(self
): canvas
.blit
(self
.img
, (self
.x
, self
.y
)) def step(self
): self
.y
+= 4 def hit(self
, wsc
): if self
.x
>= wsc
.x
- self
.width
+ 10 and self
.x
<= wsc
.x
+ wsc
.width
- 10 and self
.y
>= wsc
.y
- self
.height
+ 10 and self
.y
<= wsc
.y
+ wsc
.height
- 10:self
.canDelete
= True Var
.game
.score
+= 1 def out(self
): if self
.y
>= 625 - self
.height
: self
.canDelete
= True def gameOver():Var
.game
.end
()
最后一步,是為了保持項(xiàng)目的實(shí)時(shí)性,即使用while True死循環(huán)將保證所有的插件元素(熱狗、wsc、背景等一直出現(xiàn)在畫布上)
while True:if not Var
.game
.isEnd
: componentEnter
() componentPaint
() componentStep
() checkHit
() componentDelete
() isOutBounds
() gameOver
() else: canvas
.blit
(end
, (0, 0)) fillTextOver
(str(Var
.game
.score
), (460, 390)) handleEvent
()pygame
.display
.update
()pygame
.time
.delay
(10)
這個(gè)項(xiàng)目就到此結(jié)束啦!剛剛回看了一下自己寫的blog,感覺有很多東西沒有講清楚:(
只能先這樣啦,到時(shí)候想到什么再補(bǔ)嘻嘻!
對(duì)了,來看看這個(gè)項(xiàng)目的完整版和效果視頻吧:
import pygame
from pygame
.locals import *
import time
, random
import sys
import os
pygame
.init
()
os
.environ
['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (100, 25)
canvas
= pygame
.display
.set_mode
((1000, 625))
canvas
.fill
((255, 255, 255))
pygame
.display
.set_caption
("熱狗大戰(zhàn)")
bg
= pygame
.image
.load
("images/bg.jpg")
h
= pygame
.image
.load
("images/hotdog.png")
player
= pygame
.image
.load
("images/hero.png")
end
= pygame
.image
.load
("images/end.jpg")
bullet_tip
= pygame
.image
.load
("images/bullet_tip.png")
time_tip
= pygame
.image
.load
("images/time_tip.png")
def handleEvent():for event
in pygame
.event
.get
(): if event
.type == QUIT
: pygame
.quit
() sys
.exit
() if event
.type == KEYDOWN
: if event
.key
== 276: Var
.wsc
.x
-= 40 if event
.key
== 275: Var
.wsc
.x
+= 40 if Var
.wsc
.x
< 0: Var
.wsc
.x
= 0 if Var
.wsc
.x
> 1000 - Var
.wsc
.width
: Var
.wsc
.x
= 1000 - Var
.wsc
.width
def fillText(text
, position
, view
=canvas
):my_font
= pygame
.font
.Font
("my_font/font1.ttf", 30)text
= my_font
.render
(text
, True, (255, 255, 255))view
.blit
(text
, position
)def fillTextOver(text
, position
, view
=canvas
):my_font
= pygame
.font
.Font
("my_font/font1.ttf", 50)text
= my_font
.render
(text
, True, (255, 255, 255))view
.blit
(text
, position
)
class HotDog():def __init__(self
, x
, y
, width
, height
, img
): self
.x
= x self
.y
= y self
.width
= width self
.height
= height self
.img
= img self
.canDelete
= False def paint(self
): canvas
.blit
(self
.img
, (self
.x
, self
.y
)) def step(self
): self
.y
+= 4 def hit(self
, wsc
): if self
.x
>= wsc
.x
- self
.width
+ 10 and self
.x
<= wsc
.x
+ wsc
.width
- 10 and self
.y
>= wsc
.y
- self
.height
+ 10 and self
.y
<= wsc
.y
+ wsc
.height
- 10:self
.canDelete
= True Var
.game
.score
+= 1 def out(self
): if self
.y
>= 625 - self
.height
: self
.canDelete
= True
class Wsc():def __init__(self
, x
, y
, width
, height
, img
): self
.x
= x self
.y
= y self
.width
= width self
.height
= height self
.img
= img
def paint(self
): canvas
.blit
(self
.img
, (self
.x
, self
.y
))
class Game():def __init__(self
, t
): self
.score
= 0 self
.t
= t self
.lastTime
= time
.time
() self
.interval
= 0.6 self
.time0
= 0 self
.isEnd
= False def end(self
): if self
.t
> 0: t
= time
.time
() if t
- self
.lastTime
>= 1: self
.t
-= 1 self
.lastTime
= t
else: self
.isEnd
= True def isTime(self
): if not self
.time0
: self
.time0
= time
.time
() return True t
= time
.time
() if t
- self
.time0
>= self
.interval
:self
.time0
= t
return True pass
class Var():hotDogs
= [] wsc
= Wsc
(480, 625 - 153, 80, 133, player
) game
= Game
(30) pass
def componentEnter():if Var
.game
.isTime
(): x
= random
.randint
(0, 1000 - 32) Var
.hotDogs
.append
(HotDog
(x
, 0, 32, 60, h
)) pass
def componentDelete():for hotDog
in Var
.hotDogs
: if hotDog
.canDelete
: Var
.hotDogs
.remove
(hotDog
) pass
def componentPaint():canvas
.blit
(bg
, (0, 0)) for hotDog
in Var
.hotDogs
: hotDog
.paint
() Var
.wsc
.paint
() canvas
.blit
(bullet_tip
, (800, 5)) canvas
.blit
(time_tip
, (25, 25)) fillText
(str(Var
.game
.t
), (40, 40)) fillText
(str(Var
.game
.score
), (880, 40)) pass
def componentStep():for hotDog
in Var
.hotDogs
: hotDog
.step
() pass
def checkHit():for hotDog
in Var
.hotDogs
: hotDog
.hit
(Var
.wsc
) pass
def isOutBounds():for hotDog
in Var
.hotDogs
: hotDog
.out
() pass
def gameOver():Var
.game
.end
() while True:if not Var
.game
.isEnd
: componentEnter
() componentPaint
() componentStep
() checkHit
() componentDelete
() isOutBounds
() gameOver
() else: canvas
.blit
(end
, (0, 0)) fillTextOver
(str(Var
.game
.score
), (460, 390)) handleEvent
()pygame
.display
.update
()pygame
.time
.delay
(10)
嘿嘿,之前參加比賽,為傳承中國(guó)文化,還專門照葫蘆畫瓢了個(gè)“蘇武牧羊”:
最后的最后,小本生意碼字不易,大家能看到這里已經(jīng)是我何等的榮幸了哈哈哈
如有問題,記得聯(lián)系我呀~
1773632066@qq.com
總結(jié)
以上是生活随笔為你收集整理的Hotdog 热狗大战的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。