生活随笔
收集整理的這篇文章主要介紹了
Hotdog 热狗大战
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
HOTDOG CONFLICT
廢話介紹 背景介紹
創作背景
19號磨磨蹭蹭地在重慶考完了雅思,終于有機會打開積灰已久的CSDN記錄一下“代碼人生"了哈哈哈。未來的一個月還要同時進行AP的學習,加油啊啊啊!
項目背景
這個項目是寒假參加“發現杯”前的復習項目,覺得還不錯就想把它搬上來分享分享。
【聲明】非原創,受小學時候玩的“金山打字通”接蘋果游戲、和wsc吃熱狗的啟發
整體思路
- 準備工作(搭建環境,例如導入庫、設置標題、窗框大小坐標等等)
- 設置處理事件類型
- 通過“類”的方式創造組件,并進行調用
- 后臺處理
準備工作
導入庫大家都應該很熟練,這里我們也很熟練地操作一下~
(玄武黑,感覺比簡約白更高級炫酷欸哈哈哈,高光也很棒orz)
import pygame
from pygame
.locals import *
import time
, random
import sys
import os
接下來的一步,創建窗口和設置標題我們在Time Management里面也見到過,這里不難也比較標準,不用過多進行思維上的理解。
值得一提的是,最后一行(255,255,255)表示的是邊框的顏色,分別對應“紅黃藍”三原色的占比(此處代表白色),(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))
窗口標題的設置也很常規~
pygame
.display
.set_caption
("熱狗大戰")
(哈哈哈說一句題外話,我真的好喜歡Markdown:)
在完成外框的設置之后,我們就要開始逐步傳輸圖片了。我們事先會將圖片儲存在同級別的文件夾里,運用調動相對路徑的方式將它們傳輸上去。
(如何區分相對路徑和絕對路徑呢?
絕對路徑跟快遞收貨地址很像,就像是衛星定位一樣,需要提供確切的位置。在電腦中的絕對路徑就相當于“C盤 XXX文件夾”
而相對路徑適用于描述某個相鄰的事物,例如當你知道我的位置時,我通知你來到我的鄰居家,你也能很方便清晰地找到它。)
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")
到此為止準備任務就到此結束啦,我們接下來進行事件處理類型的設置。
下一步是對用戶操作動作的設置,例如如果按下了移動箭頭的按鍵,程序會進行怎樣的變化。用到的是比較簡單的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
這時 重點就來了!!!
一般我們想用某個庫/工具包里的功能時,我們直接用庫名.方法名() 的方法進行調用;但如果此時我們想調用的功能還沒有被創建出來,但每一次書寫都特別麻煩時,我們就可以用def的方法自己創造工具包。
例如這里,我們想創造一個“字體”方法,之后設置字體的時候就可以直接調用 。
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
)
- 第一排括號里的(text, position, view=canvas)是形式參數,相當于只是一個名字而已,沒有任何實際意義。之后在調用這個方法時,再將實際參數傳輸進去。
- 第二行代碼是進行字體的設置,前者是字體類型,后者是字體大小
- 下一行的第一個元素代表內容;第二個元素True代表“抗鋸齒效果”,如果設置為False或者忽略設置,就會看到一群馬賽克在晃動;后面的3個255我們在前面已經提到過啦~)
- 最后一行就是傳輸文字,包含內容和位置坐標。
class object
這里比較特別,是用class語句將特定功能*“封裝”*起來,也是方便之后的調用。
def括號內的內容也是形式參數,后面的傳參就是“實例化方法”。
就像是設置的一個一個工具包,把代碼分成一塊一塊的,再來調用。
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
()
最后一步,是為了保持項目的實時性,即使用while True死循環將保證所有的插件元素(熱狗、wsc、背景等一直出現在畫布上)
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)
這個項目就到此結束啦!剛剛回看了一下自己寫的blog,感覺有很多東西沒有講清楚:(
只能先這樣啦,到時候想到什么再補嘻嘻!
對了,來看看這個項目的完整版和效果視頻吧:
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
("熱狗大戰")
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)
嘿嘿,之前參加比賽,為傳承中國文化,還專門照葫蘆畫瓢了個“蘇武牧羊”:
最后的最后,小本生意碼字不易,大家能看到這里已經是我何等的榮幸了哈哈哈
如有問題,記得聯系我呀~
1773632066@qq.com
總結
以上是生活随笔為你收集整理的Hotdog 热狗大战的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。