AGK-Python
Trong tutorial này sẽ học cách sử dụng các class trong Python của AGK.
1 Using Python Class¶
1.1 Khởi tạo Game¶
from appgamekit import * # load AGK
# Tạo Application theo các thông số ban đầu
with Application(width=1024, height=768, fullscreen=False):
use_new_default_fonts(True)
set_window_title("Learn Using Python Class")
set_virtual_resolution(1024,768)
set_scissor(0, 0, 0, 0)
# Game loop
while True:
print_value(screen_fps())
sync()
1.2 Tạo class Ball¶
Để có thể tạo nhiều nhân vật có đặc tính của Ball thì sử dụng class trong Python.
Một template của class như sau:
class Ball:
"""Tạo Type Ball có khả năng tạo các sprite có thể tựdi chuyển"""
def __init__(self) -> None:
pass
Trong đó ký hiệu ->
để mô tả type
trả về. Nó cũng có thể thuộc dạng None
Sau game như sau
from appgamekit import * # load AGK
# Tạo class Ball
class Ball:
"""Tạo Type Ball có khả năng tạo các sprite có thể tựdi chuyển"""
def __init__(self,image_id): #Input của Ball sẽ là image ID
sprite = create_sprite(image_id)
set_sprite_position(sprite, random(320, 3200), random(10, 760)) # cho random vị trí trong màn hình theo y, nhưng x thì có thể xa hơn từ bên ngoài màn hình
set_sprite_color(sprite,random(0,255),random(0,255),random(0,255),random(0,255)) # Tạo random màu
# Gán các thuộc tính của sprite vào init self
self.sprite = sprite
self.speed= random(10,40)/10
self.move = 0
self.angle = 0
def animate(self):
# Gọi các local variables trong self init để sử dụng trong function này
sprite = self.sprite
speed = self.speed
x, y = get_sprite_x(sprite), get_sprite_y(sprite)
x -= speed # cho ball di chuyến từ phải sang trái dựa theo random speed
y += cos(self.move)
self.move += speed
set_sprite_angle(sprite, get_sprite_angle(sprite)+speed)
# Nếu ball ra khỏi phía trái màn hình thì reset lại vị trí
if x < -100:
x, y = random(320,2000), random(10,760)
set_sprite_position(sprite, x, y)
# Tạo Application theo các thông số ban đầu
with Application(width=1024, height=768, fullscreen=False):
use_new_default_fonts(True)
set_window_title("Learn Using Python Class")
set_virtual_resolution(1024,768)
set_scissor(0, 0, 0, 0)
# Get screen width and height
screenWidth = get_device_width()
screenHeight = get_device_height()
# Add background cho game, fit to game screen
background_sprite = load_sprite("background1.jpg")
set_sprite_size(background_sprite,screenWidth,screenHeight)
set_sprite_depth(background_sprite,100) # cho đứng ở vị trí 100
fix_sprite_to_screen(background_sprite,True) # fix position background
# Load image of ball
ball_image = load_image("ball1.png")
# Tạo một list của Ball để chứa nhiều nhân vật ball random
ball_sprites = []
# Sử dụng loop để add từng Ball type vào list
for each in range (5000):
ball_sprites.append(Ball(ball_image))
# Game loop
while True:
print_value(screen_fps())
# cho Ball di chuyển dựa theo function animate() đã tạo trong class
for ball in ball_sprites:
ball.animate()
sync()
# Sử dụng phím Escape để thoát
if get_raw_key_pressed(KEY_ESCAPE):
break
Kết quả tạo Random 5000 Ball như sau với FPS gần 60, lúc chụp hình còn khoảng 40 FPS. Test cho 50.000 ball thì FPS khoảng 5, thấp hơn 3 lần so với Tier 1 của AGK, chắc cũng thấp hơn nhiều so với C++
Backlinks¶
No other pages link to this page.
Created : Apr 27, 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