Bài giảng 6: Lập trình nhân vật cơ bản
Tiết thứ: 13 - 16 Tuần thứ: 5+6.
- Mục đích, yêu cầu:
• Nắm được cách xây dựng hình ảnh nhân vật với các thao tác di chuyển nhân vật theo các hướng.
• Nắm được hệ thống thước đo và tỉ lệ.
- Hình thức tổ chức dạy học: Lý thuyết, , thực hành, tự học, tự nghiên cứu
- Thời gian: Lý thuyết: 3t; Thực hành: 3t Tự học, tự nghiên cứu: 6t
- Địa điểm: Giảng đường do P2 phân công.
- Nội dung chính:
Lý thuyết:
A sprite is a small image that is moved around on the screen. Many programmers misuse the term to describe any small graphic image in a game. Static, unmoving objects on the screen are not sprites because by definition a sprite is something that moves around and does something on the screen, usually in direct relation to something the player is doing within the game.
Major topics in this chapter:
Drawing regular and scaled sprites
Drawing flipped sprites
Drawing rotated and pivoted sprites
Enhancing Tank War
Drawing Regular Sprites
The first and most important function to learn is draw sprite.
void draw sprite(BITMAP *bmp, BITMAP *sprite, int x, int y)
Figure 6.1 A high-color sprite drawn to the screen with a default 8-bit color depth. Sprite image courtesy of Ari Feldman.
6.2 Drawing with Transparency
#include
#include
#define WHITE makecol(255,255,255)
int main(void)
{
BITMAP *dragon;
int x, y;
//initialize the program
allegro init();
install keyboard();
set color depth(16);
set gfx mode(GFX AUTODETECT FULLSCREEN, 640, 480, 0, 0);
//print some status information
textprintf ex(screen,font,0,0,WHITE,0,"Resolution = %ix%i",
SCREEN W, SCREEN H);
Figure 6.2
The high-color sprite is drawn to the screen with 16-bit color. Sprite image courtesy of Ari Feldman.
textprintf ex(screen, font, 0, 10, WHITE,0,"Color depth = %i",
bitmap color depth(screen));
//load the bitmap
dragon = load bitmap("spacedragon1.bmp", NULL);
x = SCREEN W/2 - dragon->w/2;
y = SCREEN H/2 - dragon->h/2;
//main loop
while (!key[KEY ESC])
{
//erase the sprite
rectfill(screen, x, y, xþdragon->w, yþdragon->h, 0);
//move the sprite
if (x-- < 2)
x = SCREEN W - dragon->w;
//draw the sprite
draw sprite(screen, dragon, x, y);
textprintf ex(screen,font,0,20,WHITE,0,"Location = %ix%i", x, y);
rest(10);
}
//delete the bitmap
destroy bitmap(dragon);
allegro exit();
return 0;
}
END OF MAIN()
Figure 6.3
The difference between a sprite drawn with and without transparency. Sprite image courtesy of Ari Feldman.
Drawing Scaled Sprites
Scaling is the process of zooming in or out of an image, or in another context, shrinking or enlarging an image.
The function is called stretch sprite and it looks like this:
void stretch sprite(BITMAP *bmp,BITMAP *sprite,int x,int y,int w,int h)
#include
#include
#define WHITE makecol(255,255,255)
Figure 6.4
A high-resolution sprite image scales quite well. Sprite image courtesy of Ari Feldman.
int main(void)
{
BITMAP *cowboy;
int x, y, n;
float size = 8;
//initialize the program
allegro init();
install keyboard();
set color depth(16);
set gfx mode(GFX AUTODETECT FULLSCREEN, 640, 480, 0, 0);
//print some status information
textprintf ex(screen,font,0,0,WHITE,0,
"Resolution = %ix%i", SCREEN W, SCREEN H);
textprintf ex(screen,font,0,10,WHITE,0,
"Color depth = %i", bitmap color depth(screen));
//load the bitmap
cowboy = load bitmap("spacecowboy1.bmp", NULL);
//draw the sprite
for (n = 0; n < 11; nþ þ) {
y = 30 þ size;
stretch sprite(screen, cowboy, size, y, size, size);
textprintf ex(screen,font,sizeþsizeþ10,y,WHITE,0,"%ix%i",
(int)size,(int)size);
size *= 1.4;
}
//delete the bitmap
destroy bitmap(cowboy);
while(!keypressed());
allegro exit();
return 0;
}
END OF MAIN()
6.4 Drawing Flipped Sprites
Now imagine eliminating the east-, west-, and south-facing
bitmaps from the game by simply drawing the north-facing bitmap in one of the
four directions using a special version of draw sprite for each one. In addition to
the standard draw sprite, you now have the use of three more functions to flip
the sprite three ways:
void draw sprite v flip(BITMAP *bmp, BITMAP *sprite, int x, int y)
void draw sprite h flip(BITMAP *bmp, BITMAP *sprite, int x, int y)
void draw sprite vh flip(BITMAP *bmp, BITMAP *sprite, int x, int y)
Chia sẻ với bạn bè của bạn: |