Learn-AppGameKit
Tôi sẽ bắt đầu xem các clip trên Youtube để hiểu về ngôn ngữ BASIC, sau đó sẽ xem các code có sẵn từ examples trong Game.
1 Introduction¶
Sau khi xem các clip về Code của BASIC thì ngôn ngữ này không phù hợp với tôi. Nó không có OOP nên các code rất dài
2 Load Image¶
2.1 Tạo nhân vật¶
SetVirtualResolution(1024,768)
img_boy = LoadImage( "boy.png" )
boy_sprite = CreateSprite(img_boy)
do
sync()
loop
2.2 Tạo Background¶
// create background
img_background = LoadImage( "colourful-space-square.png" )
CreateSprite(img_background)
// Create sprite character
img_boy = LoadImage( "boy.png" )
boy_sprite = CreateSprite(img_boy)
Chú ý đến thứ tự load của các image.
3 Movement of Sprite¶
Để di chuyển nhân vật thì rất đơn giản, chỉ cần sử dụng GetJoystrick
function
// set position of sprite
sx = 512
sy = 380
// Tao Game loop
do
SetSpritePosition(boy_sprite,sx,sy)
sx = sx+GetJoystickX()*10
sy = sy+GetJoystickY()*10
sync()
loop
Chú ý hệ toạ độ của AGK
4 Simple Shooter Game¶
4.1 Set up scripts cho game¶
Thông thường thì sẽ không chỉ tạo 1 script là main.agc cho AGK mà tạo ra nhiều file tương ứng cho nhiều thành phần trong Game
Ví dụ như main, Loader, Player.
Trong file main thường sẽ tạo các variables chung cho tất cả các class, trong từng class (script file) thì sẽ update các class đó.
Ví dụ như file main
playerX as float
playerY as float
#include "Loader.agc" // only load, but nothing run, need to use Gosub -name function-
#include "PlayerMove.agc"
Gosub loader // Load function loader of script Loader.agc
// Game loop
do
Print( ScreenFPS() )
Gosub PlayerMove //load function from PlayerMove.agc
Sync()
loop
Trong class Player thì tạo script PlayerMove
// File: PlayerMove.agc
// Created: 22-04-21
PlayerMove:
playerX = playerX + GetDirectionX()*15+GetJoystickX()*12 //if use WASD to move use GetJoystick
playerY = playerY + GetDirectionY()*15+GetJoystickY()*12 // if use arrow keys, use GetDirection
//Check the player outline game
if playerX <=-GetSpriteWidth(1)
playerX = GetVirtualWidth()
endif
if playerX >= (GetVirtualWidth() + GetSpriteWidth(1))
playerX = 0
endif
//update position
SetSpritePosition(1,playerX, playerY)
Return
Tương tự trong Loader cũng như vậy. Điểm đáng chú ý khi sử dụng class trong AGK là phải #include
sau đó phải gọi tên function mà muốn sử dụng bằng cách Gosub PlayerMove
.
4.2 Keyboard Input cho game¶
Có thể sử dụng GetPointerPress hoặc GetRawPressed
để kiểm tra input của bàn phím. Các mã code của bàn phím theo website dưới đây
// File: PlayerShoot.agc
// Created: 22-04-21
PlayerShoot:
if (GetPointerPressed()=1 or GetButtonPressed(1)=1) and lazer_fined=0 // if there is a mounse press, 1, set lazer_fired to allow only one bullet/time
lazerX = GetSpriteX(1) + GetSpriteWidth(1)/2 - GetSpriteWidth(2)/2 //position of lazer start from spaceship
lazerY = getspriteY(1) - 40 // why -40?
lazer_fined = 1
endif
if lazer_fined = 1
lazerY = lazerY - 12 //-12 mean the bullet will go up of the screen, becasue the spaceship is in the bottom
endif
if lazerY < -GetSpriteHeight(2)/2 // if the bullet touch the top of the screen, stop the shoot
lazer_fined = 0
endif
SetSpritePosition(2,lazerX, lazerY)
Return
Trong code này có một nhược điểm là bullet chỉ có một. Nếu bỏ điều kiện and lazer_fined=0
thì có thể bắn bullet mới, nhưng bullet cũ sẽ biến mất.
Để tạo ra nhiều bullets thì phải có function khác
4.3 Add sound¶
Trong AGK chỉ có 2 lựa chọn cho audio là WAV và OGG. OGG là dạng file nén nên có dung lượng nhỏ hơn, tuy nhiên sẽ làm game chậm hơn 1 chút vì phải tự giải nén trong game, nhưng package game sẽ nhẹ.
Load_Sound:
explosion_sound = LoadSound( "FireworkBang.wav" )
lazer_sound = LoadSound( "LaserSimple.wav" )
Return
Load_Music:
music = LoadMusicOGG( "Sleepless-City-Synthwave-Retrowave-Music.ogg" )
PlayMusicOGG(music)
SetMusicVolumeOGG(music,10) // reduce music background
Return
Chú ý Game Sound thì không được để trong Game Loop vì nó sẽ làm nhạc bị rối loạn, phải khai báo nó từ ban đầu.
4.4 Enemy¶
Cho enemy random di chuyển
Enemy_move:
//Move enemy from top to outside of bottom
enemyY = enemyY + 1
//Move enemy left or right automatically
// Go until touch right side
if enemy_direction = 7 and enemyX > GetVirtualWidth() - GetSpriteWidth(3)
enemy_direction = -7
endif
// Go until touch left side
if enemy_direction = -7 and enemyX <0
enemy_direction = 7
endif
enemyX = enemyX + enemy_direction
SetSpritePosition(3,enemyX, enemyY)
Return
4.5 Collision¶
// File: Collision.agc
// Created: 22-04-21
collision:
if GetSpriteCollision(2,3)=1 // bullet touch enemy
PlaySound(explosion_sound,10)
score = score+10
enemyX=Random(0,500): enemyY=-50
lazer_fined = 0
lazerX = -50 : lazerY = -50
endif
Return
4.6 Text¶
Text trong AGK rất đơn giản, chỉ cần chọn text và position là được.
5 AGK for Python¶
Để sử dụng được Python và Export ra file EXE cần sử dụng pyinstaller version 4.4 đến 4.6 và upx (sử dụng để nén, giảm dung lượng file EXE) version 3.96 trong môi trường Python 3.7 (có lẽ version mới hơn cũng được nhưng chưa test).
Hiện tại đã update pyinstaller version 5.0 cho Python 3.7. Mọi thứ ổn định. Bản 5.0.1 bị lỗi với upx
Ngoài ra còn có 2 python packges cho Game là ursina engine cho 3D (từ 2019, dựa theo Panda3D) và pygame cho 2D game (từ rất lâu rồi dựa theo SDL)
6 Pong tutorial in AGK Studio¶
6.1 Tạo Type - tương đương class¶
Backlinks¶
No other pages link to this page.
Created : Apr 6, 2022
Recent Posts
- 2024-11-02: BUỔI 10 - Phân tích thị trường
- 2024-11-02: BUỔI 11 - Phân tích thị trường
- 2024-11-02: BUỔI 12 - Phân tích sóng tăng
- 2024-11-02: BUỔI 13 - Phân tích hỏi đáp
- 2024-11-02: BUỔI 14 - Yếu tố kiểm soát
- 2024-11-02: BUỔI 15 - Hỏi đáp
- 2024-11-01: BUỔI 6 - Ôn lại và bổ sung
- 2024-11-01: BUỔI 7 - Chiến thuật Trend
- 2024-11-01: BUỔI 8 - Công thức điểm vào lệnh
- 2024-11-01: K2023 - BUỔI 9 - Quy trình vào lệnh