gpt4 book ai didi

c++ - 动画问题?

转载 作者:行者123 更新时间:2023-11-30 03:06:01 25 4
gpt4 key购买 nike

现在,当我开始我的游戏时,我正在用 C++ 制作我向左向右向上或向下行走。但是角色只是滑动,看起来不像他在走路。我已经将所有图片加载到我的游戏中,它们正在工作..但我不知道如何解决它..我想不通的是当你按住按钮时如何改变图片..

顺便说一句,这是快板..

这是我的绘图播放器代码:

void Player::Draw(BITMAP *Buffer){
draw_sprite(Buffer, Oskar[picNumber], x, y);
}

Oskar[] 是包含所有图片的数组的名称..

这是当您按下按钮时角色图片发生变化的原因:

void Player::Controls(){

if(key[KEY_RIGHT]){
velocityX = speed;
picNumber = 6;
}

else if(key [KEY_LEFT]){
velocityX = -speed;
picNumber = 9;
}

else{
velocityX = 0;
}

if(key [KEY_UP]){
velocityY = -speed;
picNumber = 3;
}
else if(key [KEY_DOWN]){
velocityY = speed;
picNumber = 0;
}
else{
velocityY = 0;
}

x += velocityX;
y += velocityY;
}

这都是关于我创建的变量 picNumber.. 我拥有的所有图片都在一个数组中,picNumber 代表要绘制的图片..很高兴在这方面得到一些帮助。我整天都在想这个问题。

编辑

#include "Player.h"
#include "Global.h"
#include <allegro.h>


Player::Player(){

}


Player::~Player(){

}

void Player::Init(){
x = 10;
y = 10;
velocityX = 0;
velocityY = 0;
speed = 1;
picNumber = x % MAXPICS;

OskarFront[0] = load_bitmap("Character\\OskarFront.bmp", NULL);
OskarFront[1] = load_bitmap("Character\\OskarStanding.bmp", NULL);
OskarFront[2] = load_bitmap("Character\\OskarFront2.bmp", NULL);

OskarBack[0] = load_bitmap("Character\\OskarBack.bmp", NULL);
OskarBack[1] = load_bitmap("Character\\OskarStandingBack.bmp", NULL);
OskarBack[2] = load_bitmap("Character\\OskarBack2.bmp", NULL);

OskarRight[0] = load_bitmap("Character\\Oskar1.bmp", NULL);
OskarRight[1] = load_bitmap("Character\\Oskar.bmp", NULL);
OskarRight[2] = load_bitmap("Character\\Oskar2.bmp", NULL);

OskarLeft[0] = load_bitmap("Character\\OskarLeft.bmp", NULL);
OskarLeft[1] = load_bitmap("Character\\OskarLeftStand.bmp", NULL);
OskarLeft[2] = load_bitmap("Character\\OskarLeft2.bmp", NULL);
}

void Player::Update(){
Player::Controls();
}

void Player::Draw(BITMAP *Buffer){

if(walkingRight == true){
draw_sprite(Buffer, OskarRight[picNumber], x, y);
}

else if(walkingLeft == true){
draw_sprite(Buffer, OskarLeft[picNumber], x, y);
}

else if(walkingFront == true){
draw_sprite(Buffer, OskarFront[picNumber], x, y);
}

else if(walkingBack == true){
draw_sprite(Buffer, OskarBack[picNumber], x, y);
}

else{
draw_sprite(Buffer, OskarFront[1], x, y);
}
}

void Player::Controls(){

if(key[KEY_RIGHT]){
velocityX = speed;
walkingRight = true;
}

else if(key [KEY_LEFT]){
velocityX = -speed;
walkingLeft = true;
}

else{
walkingRight = false;
walkingLeft = false;
velocityX = 0;
}

if(key [KEY_UP]){
velocityY = -speed;
walkingFront = true;
}
else if(key [KEY_DOWN]){
velocityY = speed;
walkingBack = true;
}
else{
velocityY = 0;
walkingFront = false;
walkingBack = false;
}

x += velocityX;
y += velocityY;
}

现在这里是我在这里获得帮助后输入的新的完整代码。当我走上去显示前面的图片和走下上面的图片时,它现在不工作了。但是左右工作。也它不会像动画一样改变图片..

最佳答案

每个运动方向只有一个索引!

假设您绘制了大约 3 个(左腿向前、重叠、右腿向前),您需要随着行进的进行随时间或运动增加索引。

有趣的是 Allegro 取得了多远的进步 - 十年前它主要是 DOS 图形(我将它与 DJGPP 一起使用)并且有来自住在伦敦公寓的作者的笔记。 我的想法多么简朴,直到现在我才明白我们外省人的价格与伦敦的价格相差多少。

在回答您的评论时,尝试这样的事情:

#define MAXPICS 3
BITMAP *OskarWalksLeft[MAXPICS];
BITMAP *OskarWalksRight[MAXPICS];
picNumber = x % MAXPICS;
if (facingLeft) {
draw_sprite(Buffer, OskarWalksLeft[picNumber], x, y);
} else //facingRight
draw_sprite(Buffer, OskarWalksRight[picNumber], x, y);

或者如果您不希望每个像素都有一个新帧,但每 3 个左右尝试更换一次

picNumber = x % MAXPICS;

picNumber = (x / 3)  % MAXPICS;

我试过稍微整理一下你的代码并想出这个,我没有快板所以无法测试它:

#include "Player.h"
#include "Global.h"
#include <allegro.h>

Player::Player(){}

Player::~Player(){}

enum playerDir {left, right, front, back, stationary} direction;
BITMAP *Oskar[stationary][3];

void Player::Init(){
x = 10;
y = 10;
velocityX = 0;
velocityY = 0;
speed = 1;
picNumber = x % MAXPICS;

char filename[256];
const char *dirStrs[] = {"left","right","front","back"};
for (playerDir i = left; i < stationary; (int)i = (int)i + 1)
for (j = 0; j < 3; ++j) {
strcpy(filename, "Character\\Oskar");
strcat(filename, dirStrs[i]);
strcat(filename, itoa(j));
strcat(filename, ".bmp");
Oskar[i][j] = load_bitmap(filename, NULL);
}
}

void Player::Update(){Player::Controls();}
void Player::Draw(BITMAP *Buffer){
switch (direction) {
case left :
draw_sprite(Buffer, OskarLeft[picNumber], x, y);
break;
case right :
draw_sprite(Buffer, OskarRight[picNumber], x, y);
break;
case front :
draw_sprite(Buffer, OskarFront[picNumber], x, y);
break;
case back :
draw_sprite(Buffer, OskarBack[picNumber], x, y);
break;
default:
draw_sprite(Buffer, OskarFront[1], x, y);
}
}

void Player::Controls(){
direction = stationary;
if(key[KEY_RIGHT]){
velocityX = speed;
direction = right;
} else if(key [KEY_LEFT]){
velocityX = -speed;
direction = left;
} else velocityX = 0;

if(key [KEY_UP]){
velocityY = -speed;
direction = front;
} else if(key [KEY_DOWN]){
velocityY = speed;
direction = back;
} else velocityY = 0;

x += velocityX;
y += velocityY;
}

关于c++ - 动画问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7163425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com