Thực hành:
Sinh viên tự thiết kết xây dựng một nhân vật động và cho nhân vật di chuyển dọc theo màn hình
Thực hành:
Thực hành thử nghiệm xây dựng một đối tượng hoạt hình chuyển động đơn giản.
- Yêu cầu SV chuẩn bị:
Đọc các phần tương ứng trong chương 9 của [1]
Đọc c ác phần tương ứng trong tài liệu [5].
Chú ý nghe giảng.
Tích cực tham gia phát biểu ý kiến
Kiểm tra giữa kỳ.
Tiết thứ: 21 Tuần thứ: 7
Bài giảng 8: Lập trình khung nền
Tiết thứ: 22 - 25 Tuần thứ: 8+9
- Mục đích, yêu cầu:
• Giúp sinh viên nắm được cách xây dựng khung nền theo dạng xếp tầng.
• Nắm được cách làm việc với thanh cuộn.
- Hình thức tổ chức dạy học: Lý thuyết, tự học, tự nghiên cứu
- Thời gian: Lý thuyết: 4t; Tự học, tự nghiên cứu: 8t
- Địa điểm: Giảng đường do P2 phân công.
- Nội dung chính:
8.1 Introduction to Scrolling
Scrolling is the process of displaying a small window of a larger virtual game world.
Figure 8.1
The scroll window shows a small part of a larger game world.
Figure 8.2
Some games use a smaller scroll window on the game screen.
The key to scrolling is actually having something in the virtual game world to
display in the scroll window.
8.2 A Limited View of the World
#include
#include "allegro.h"
//define some convenient constants
#define MODE GFX AUTODETECT FULLSCREEN
#define WIDTH 640
#define HEIGHT 480
#define STEP 8
//virtual buffer variable
BITMAP *scroll;
//position variables
int x=0, y=0;
//main function
int main(void)
{
//initialize allegro
allegro init();
install keyboard();
install timer();
set color depth(16);
set gfx mode(MODE, WIDTH, HEIGHT, 0, 0);
//load the large bitmap image from disk
scroll = load bitmap("bigbg.bmp", NULL);
//main loop
while (!key[KEY ESC])
{
//check right arrow
if (key[KEY RIGHT])
{
x += STEP;
if (x > scroll->w - WIDTH)
x = scroll->w - WIDTH;
}
//check left arrow
if (key[KEY LEFT])
{
x -= STEP;
if (x < 0)
x = 0;
}
//check down arrow
if (key[KEY DOWN])
{
y += STEP;
if (y > scroll->h - HEIGHT)
y = scroll->h - HEIGHT;
}
//check up arrow
if (key[KEY UP])
{
y -= STEP;
if (y < 0)
y = 0;
}
//draw the scroll window portion of the virtual buffer
blit(scroll, screen, x, y, 0, 0, WIDTH-1, HEIGHT-1);
//slow it down
rest(20);
}
destroy bitmap(scroll);
allegro exit();
return 0;
}
END OF MAIN()
The first thing I would do to enhance this program is create two variables, lastx
and lasty, and set them to equal x and y, respectively, at the end of the main loop. Then, before blitting the window, check to see whether x or y has changed since the last frame and skip the blit function. There is no need to keep blitting the same portion of the virtual background if it hasn’t moved.
8.3 Introduction to Tile-Based Backgrounds
A high-speed scrolling arcade game would automatically scroll horizontally or vertically, displaying a ground-, air-, or space-based terrain below the player (usually represented by an airplane or a spaceship). The point of these games is to keep the action moving so fast that the player doesn’t have a chance to rest from one wave of enemies to the next.
The real power of a scrolling background comes from a technique called tiling. Tiling is a process in which there really is no background, just an array of tiles that make up the background as it is displayed.
8.4 Tile-Based Scrolling
The TileScroll program uses tiles to fill the large background bitmap when the program starts. Other than that initial change, the program functions exactly like the ScrollScreen program. Take a look at Figure 8.3.
Figure 8.3
The TileScroll program demonstrates how to perform tile-based background scrolling.
The actual bitmap containing the tiles is shown in Figure 12.7.
Figure 8.4
The source file containing the tiles used in the TileScroll program.
#define WIDTH 640
#define HEIGHT 480
#define STEP 8
#define TILEW 32
#define TILEH 32
#define TILES 39
#define COLS 10
//temp bitmap
Chia sẻ với bạn bè của bạn: |