跳轉到內容

DarkBASIC 程式設計/2D 遊戲程式設計 第 1 部分

來自華夏公益教科書,開放的書籍,用於開放的世界

第 15 天

[編輯 | 編輯原始碼]

終於到了中途點,經過了所有的編碼、閱讀和日子。在本章中,我們將使用你將學習到的更多文字命令來製作一個文字冒險遊戲。此外,你將學習如何用各種顏色清除螢幕,你會喜歡這節課的。

你可能想過,天啊,文字總是必須是白色的嗎?

`NOPE
ink rgb(0,255,0),1
Print "OMG FINALLY COLOR!"

沒錯!我是一個加拿大人,但我仍然把顏色拼寫成 color 而不是 colour。RGB 是什麼意思?它代表紅色、綠色、藍色,用一個鏡頭看看你的顯示器,你會看到很多這些顏色的畫素。rgb 必須用括號分隔,然後用逗號分隔透明度值。透明度範圍從 0 到 255。RGB 值也從 0 到 255。

這裡有一些顏色要記住

255,0,0 - red
0,255,0 - green
255,255,0 - yellow
0,0,255 - blue
0,255,255 - cyan
255,255,255 - white
0,0,0 - black
255,0,255 - pink
192,192,192 - light grey

要檢視更多顏色,請在畫圖程式中檢視 rgb 值,然後隨意操作,這就是我找到的方法,所以你也應該這樣做。使用上面的值,你應該能夠建立不錯的顏色。值越低,顏色越暗;值越高,顏色越亮。

現在,在你想要一種更好的列印方式之後呢?

text x,y,string$

使用文字時,你需要使用 + 而不是分號來分隔變數。所有值都必須是字串,記住要將一個值轉換為字串,請使用 str$ 命令,如第 6 課所示。

正確的方式

a = 1
text 1,100,"This is no syntax error "+str$(a)

錯誤的方式

a = 1
text 1,100,"This is a syntax error you won't see this text until you fix "+a

我們可以輕鬆更改字型

set text font fontnameasstring$

我們也可以輕鬆地使文字不透明,或者讓我們能夠在其他文字值上書寫

set text opaque

以下是樣式更改

set text bold
set text italic
set text bolditalic

以下是更改大小的方法

set text size integernumber

好的,以下是在一個程式中展示所有這些

set text font "Times New Roman"
set text size 30
set text bold
text 1,100,"Hey"

你的作業是隨意玩弄文字值和顏色,明天回來

第 16 天

[編輯 | 編輯原始碼]

好的,現在我們已經學習了很多關於更改文字值的內容,是時候編寫你自己的文字冒險遊戲了。我們將其命名為“厄運地牢”,它將讓玩家使用各種資料命令等逃離一座城堡的潮溼地牢。

首先,讓我們做一個提綱

現在我們有了提綱,讓我們寫一個簡單的故事

邪惡的精靈把你囚禁在他們的監獄裡,現在你必須逃離厄運地牢。注意,有些轉彎會導致厄運,有些則會導致出路。

在我們遊戲中,只有兩種死法,因為這更像是一個迷宮。

現在讓我們為它做一些資料

`Dungeons of Doom
`By Dbtutor

`Room
dim room1$(10)
dim room2$(10)
dim room3$(10)
dim room4$(10)
dim room5$(10)
dim room6$(10)
dim room7$(10)
dim room8$(10)
dim room9$(10)
dim room10$(10)
dim room11$(10)
dim room12$(10)



rem -----------------
gosub startgame

startgame:
cls
gosub start
for a = 1 to 10
   Print room1$(a)
next a

do
   if inkey$() = "l" then gosub room1_b
   if inkey$() = "r" then gosub room2_b
   if inkey$() = "u" then gosub room3_b
loop

room1_b:
cls
gosub room1
for a = 1 to 10
   Print room2$(a)
next a
wait key
end

room2_b:
cls
gosub room2
for a = 1 to 10
   Print room3$(a)
next a
wait key
end

room3_b:
cls
gosub room3
for a = 1 to 10
   Print room4$(a)
next a

do
   if inkey$() = "l" then gosub room4_b
   if inkey$() = "r" then gosub room5_b
   if inkey$() = "d" then gosub startgame
   if inkey$() = "u" then gosub room6_b
loop

room4_b:
cls
gosub room4
for a = 1 to 10
   Print room5$(a)
next a
wait key
end

room5_b:
cls
gosub room5
for a = 1 to 10
   Print room6$(a)
next a

do
   if inkey$() = "b" then gosub room3_b
loop

room6_b:
cls
gosub room6
for a = 1 to 10
   Print room7$(a)
next a

do
   if inkey$() = "l" then gosub room7_b
   if inkey$() = "r" then gosub room8_b
   if inkey$() = "d" then gosub room3_b
   if inkey$() = "u" then gosub wingame
loop

room7_b:
cls
gosub room7
for a = 1 to 10
   print room8$(a)
next a
do
   if inkey$() = "b" then gosub room6_b
loop

room8_b:
cls
gosub room8
for a = 1 to 10
   print room9$(a)
next a

do
   if inkey$() = "b" then gosub room6_b
loop

wingame:
cls
gosub win
for a = 1 to 10
   print room10$(a)
next a
wait key
end

`Data Statements
start:
data "Dungeons of Doom - Starting Area"
data "The cell door swings open, the guard lies dead in a pool of blood"
data "A large gash does through his neck, clutching your dagger you"
data "Have a decision to make."
Data "You can Go - L>eft, R>ight, or U>p"

for a = 1 to 5
   read room1$(a)
next a

return

room1:
data "Dungeons of Doom - Room 1"
data "You look around the corner and leap out at a guard"
data "Sticking a sharp spear in the air your gored by it"
data "Through the heart you die instantly and never escape"
data "The Dungeons of Doom....."
data "Press Any Key"

for a = 1 to 6
   read room2$(a)
next a

return

room2:
`Right of start
data "Dungeons of Doom - Room 2"
data "As you reach the room, you notice the walls close in!"
data "You are squished between the walls"
data "The End - Press Any Key"
for a = 1 to 4
   read room3$(a)
next a

return


room3:
`North of the start point
data "Dungeons of Doom - Room 3"
data "This room is a room that leads all ways..."
data "U>p, L>eft, R>ight or D>own"
for a = 1 to 3
   read room4$(a)
next a

return

room4:
`Left of Up one from start point
data "Dungeons of Doom - Room 4"
data "You run into a monstrous troll, who proceeds to bash you"
data "Over the head with a club, waking up inside a pie"
data "You are instantly eaten by a horde of trolls"
data "The End - Press Any Key"

for a = 1 to 5
   read room5$(a)
next a

return

room5:
`Right of the room4
data "Dungeons of Doom - Room 5"
data "You run into a dead end, you must go out the way you came"
data "B>ack"

for a = 1 to 3
   read room6$(a)
next a

return

room6:
`North of Room
data "Dungeons of Doom - Room 6"
data "This room is a dank one, to the north you feel cold air a way out?"
data "U>p, L>eft, R>ight, or D>own"

for a = 1 to 3
   read room7$(a)
next a

return

room7:
`Left of the room 6
data "Dungeons of Doom - Room 7"
data "Dead End Go Back"
data "B>ack"

for a = 1 to 3
   read room8$(a)
next a

return

room8:
`Right of Room 6
Data "Dungeons of Doom - Room 8"
data "Dead End Go Back"
data "B>ack"
for a = 1 to 3
   read room9$(a)
next a

return

win:
Data "You Won"
Data "Escaping to the Forest, now you need to find a town!"
data "Press Any Key"

for a = 1 to 3
   read room10$(a)
next a

return

這個遊戲已經完成了,如果所有東西都正常工作,如果沒有,請隨時透過以下地址給我發郵件:thenerd2468@hotmail.com

那麼,你的作業是,為了證明你瞭解發生了什麼,你需要回到程式碼中,添加註釋來描述每個部分的事件。你作業的最後一步是編輯所有程式碼和資料語句,製作一個續集,新增房間以及更多內容。這個遊戲不是一個文字角色扮演遊戲,我們將在下一課中製作文字角色扮演遊戲。我認為明天你將會突破遊戲程式設計。

第 17 天

[編輯 | 編輯原始碼]

哦,天啊!今天我們將製作一個乒乓球遊戲,一個打磚塊遊戲,你將學習關於影像、精靈等的更多內容!好的,那麼我們如何在 DarkBASIC 中載入多媒體?記住,在檔案管理器程式中,我們有一些令人困惑的檔案格式。不幸的是,我沒有新增動畫。

多媒體示例

`Image
`Supports bmps and jpgs only
id = 1
string$ = "myimage.bmp"
load image string$,id
x = 1
y = 1
paste image id,x,y

`Music
`Sound works the same way, only instead the word music is replaced with the word `sound
`Supports are: wav, mp3, and midi
musid = 1
musstring$ = "mymusic.mid"
load music musstring$,musid
play music musid
stop music musid
loop music musid

`Animation
`Supports gif, avi, may support other formats
animid = 1
animstring$ = "myanimation.gif"
load animation animstring$,animid
play animation animid

`Bitmap
`Works same as image only supports bitmaps, you can load only 32
bmpid = 1
bmp$ = "mybmp.bmp"
load bitmap bmp$
`This pastes the bitmap, but the id can be used to create bitmaps at certain `locations 
`see bmps under the command reference for those details

end

檢視 DarkBASIC 中更多多媒體示例,還有一些是 3D 音訊,你也可以播放 CD 上的音樂。我們將在後面的章節中使用 DarkBASIC 中更多多媒體示例,現在我們將專注於示例中提到的內容。

你的主要問題可能是什麼是精靈?精靈是一個在遊戲中使用的二維角色。基本上,精靈是一個可以做更多事情的影像,在 DarkBASIC 中,精靈是系統中的核心。

精靈的工作方式像

sprite spriteid,x,y,imageid

你不能使用 load bitmap 並將其用作精靈的影像,你只能使用 load image 命令載入的影像。


以前,你在 DarkBASIC 中無法操作影像的 x,y 座標,但現在要改變了。在我們開始之前,如果我們讓計算機來處理遊戲的幀速率,那麼我們的遊戲執行速度會很慢,並且在不同速度的計算機上執行方式也會不同。使用 sync on 將幀速率交給你自己,而不是計算機。每次使用 sync 命令時,螢幕都會不斷更新。使用 sync rate 命令來設定幀重新整理率,對於 DarkBASIC 中的 3D 遊戲來說,30 通常很好,對於 2D 遊戲來說,40 也很好,但最好使用 30。DarkBASIC 中有一些命令可以繪製粗略的圖形,我們將使用的命令是:line(line startingx,toendingx,startingy,toendingy)、circle(x,y,radius) 和 dot(dot x,y)。它們使用預設墨水繪製這些圖形,如果墨水未設定,則將使用青色作為預設值。還有更多命令可以繪製不同的基本二維形狀,但我們不會使用它們。如果你好奇,請在 DarkBASIC 命令參考中的 BASIC2d 中檢視它們。


以下是如何移動一個圓的示例

`Moving Circle
`By Dbtutor

`Hide the mouse
hide mouse

`Setup sync rate
sync on
sync rate 30

`Make the screen grey
cls rgb(192,192,192) 

`Select an ink color for us, make it all transparent
ink rgb(0,155,0),0

`Draw the circle at coordinates provided
xpos = 100
ypos = 200
circle xpos,ypos,10

`Start the main loop
do
   `Clean up mess left over, and re-draw circle
   cls rgb(192,192,192)
   circle xpos,ypos,10

   `User control
   if upkey() = 1 then ypos = ypos - 7
   if downkey() = 1 then ypos = ypos + 7
   if rightkey() = 1 then xpos = xpos + 7
   if leftkey() = 1 then xpos = xpos - 7
   `Allow no leaving the screen
   if xpos > 600 then xpos = xpos - 7
   if xpos < 1 then xpos = xpos + 7
   if ypos > 400 then ypos = ypos - 7
   if ypos < 1 then ypos = ypos + 7
   `Refresh the screen
   sync
loop
   

哇,效果很好,對吧?現在讓我們睡一會兒吧。

第 18 天

[編輯 | 編輯原始碼]

在我們開始之前,我想要感謝所有訪問本華夏公益教科書的人,感謝您抽出時間閱讀它,感謝您知道當我搜索“DarkBASIC 程式設計”時,我的書出現在第一頁。這是一種很棒的感覺!我希望你學到一些東西,我將盡力盡快完成這本書,但我一直在學習新事物和處理學業,所以很忙。我一直在學習彙編、C++、Pascal 和 Visual Basic,我也在考慮購買一個控制檯構建套件“The XGameStation”,由 Lamothe 先生開發,或者你可能知道他叫 Andre' Lamothe 或 Lord Necron。除了 Lee Bamber(DarkBASIC 背後的有遠見的人)之外,他是我的程式設計靈感來源之一。好的,讓我們從乒乓球遊戲開始,我必須說編寫這個乒乓球遊戲是一個挑戰,我遇到過很多錯誤。你必須使用你自己的影像,只包含原始碼。

理解材料在我繼續進行乒乓球遊戲之前,有一些事情我必須、不,需要向你解釋。首先,你需要理解什麼是“精靈”,我知道我之前提到過它,但沒有像現在這樣深入解釋,那只是一個簡單的例子。基本上,精靈只是一個活動影像,它比標準點陣圖允許更多的操作。用我們的術語來說,精靈在大多數情況下只是一個二維角色。在 DarkBASIC 中,我們可以對精靈進行比普通影像更多的操作。

你已經知道如何設定精靈。但你不知道如何刪除精靈,如何檢測精靈的碰撞,而且你還沒有太多使用它們。刪除精靈的語法很簡單:delete sprite spriteid。如果精靈不存在,顯然你會得到一個執行時錯誤,指出“bob”或精靈不存在。對精靈碰撞也是一樣的:最精確、最好的方法是使用這個命令:sprite collision(spriteid1,spriteid2)。同樣,它們必須存在,正如我所說,你必須有兩個,我再次陳述顯而易見的事實,但有些人確實很笨。

另一種方法與之相同,只是沒有那麼精確,像這樣:sprite hit(spriteid1,spriteid2)。很簡單!閱讀下面的程式碼後,可能不太容易理解的是隨機數的用途。簡單地說,這些隨機數是為了讓計算機生成一個 0 到 1 之間的數字,表示球應該向哪個方向移動。它們用於 X 軸和 Y 軸,你應該已經知道它們是什麼了。

乒乓球遊戲 -

如果你不理解這些材料,請複習之前的課程或查閱 DarkBASIC 幫助檔案。

`Pong example code
`By Dbtutor
`You must use your own multimedia!

sync on
sync rate 40
hide mouse

`Load the images
load image "paddle1.bmp",1
load image "paddle2.bmp",2
load image "ball.bmp",3

` Load the music
load music "Dodge This.mid",1

` Load the sound
load sound "thump.wav",1

` Play the music 
play music 1

` Start the main game loop
` Setup some variables first
oneypos = 1
onexpos = 1
twoypos = 1
twoxpos = 620
bally = 150
ballx = 150
movey = rnd(1)
movex = rnd(1)
onegoal = 0
twogoal = 0

do

 `Display Scores & Whatnot
 set text opaque
 text 1,1,"Goals: "+str$(onegoal)
 text 585,1,"Goals: "+str$(twogoal)

 `Player Control for 1
 if upkey() = 1 then oneypos = oneypos - 7
 if downkey() = 1 then oneypos = oneypos + 7
 if oneypos < 1 then oneypos = oneypos + 7
 if oneypos > 432 then oneypos = oneypos - 7

 `The paddle sprite
 sprite 1,onexpos,oneypos,1

 `Player 2 Paddle
 if inkey$() = "w" then twoypos = twoypos - 7
 if inkey$() = "s" then twoypos = twoypos + 7
 sprite 2,twoxpos,twoypos,2
 if twoypos < 1 then twoypos = twoypos + 7
 if twoypos > 432 then twoypos = twoypos - 7

 `The Ball
 if movey = 1 then bally = bally + 5
 if movey = 0 then bally = bally - 5
 if movex = 1 then ballx = ballx + 5
 if movex = 0 then ballx = ballx - 5
 sprite 3,ballx,bally,3

 `Collision Detection
 if sprite hit(1,3) = 1 then movex = 1:play sound 1
 if sprite hit(2,3) = 1 then movex = 0:play sound 1
 if ballx < 1 then movex = 1:twogoal = twogoal + 1:play sound 1
 if ballx > 620 then movex = 0:onegoal = onegoal + 1:play sound 1
 if bally < 1 then movey = 1:play sound 1
 if bally > 432 then movey = 0:play sound 1

 `Wins
 if onegoal = 10
 text 100,150,"Player 1 Has Won!"
 wait 300
 end
 endif

 if twogoal = 10
 text 100,150,"Player 2 Has Won!"
 wait 300
 end
 endif

 `Refresh the screen
 sync

loop

好的,這就是光榮的乒乓球。現在是進行長時間測驗的時候了。

1 在遊戲設計中,二維角色叫什麼?

A) 馬里奧
B) 查拉
C) 剪貼畫
D) 精靈

2 對還是錯:30 是任何計算機上游戲的良好同步率

T)rue
F)alse

3 哪個是手動設定計算機遊戲中幀速率的最佳方法

A) 設定 sync on,將其設定為平均速率,例如 30,並在需要時使用 sync 重新整理
B) 讓計算機處理所有操作
C) 將同步率設定為 0
D) 只需開啟同步

4 你可以怎樣宣告資料語句?

A) Data 資料型別
B) dat 資料型別
C) godarkbasic data 資料型別
D) data.db 資料型別

5 資料語句儲存哪些型別的資料?

A) 僅字串
B) 陣列
C) 字串、整數和實數(浮點小數或浮點數)
D) 整數

6 在我們的文字冒險遊戲中,你必須逃離

A) 城堡
B) 監獄
C) 森林
D) 地牢

好了,現在來說答案。如果你作弊,那是你自己的事,因為這本書完成後,你就不能再作弊了,所以不要膽小,不要選擇懦夫的路。

答案

  1. D
  2. 正確
  3. A
  4. A
  5. C
  6. D

最後,我們需要結束這漫長而艱苦的編碼日,因為新的編碼屬性將會帶來新的挑戰。

華夏公益教科書