用c编程的游戏 [英] game programing with c

查看:86
本文介绍了用c编程的游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我们有一个由我们的讲师给出的项目,我们不会继续这个项目。首先是我们的游戏:www.miniclip.com/games/fat-slice/tr/

我们的主要编程语言是C.我们编写了球功能,但存在一些错误。我们没有解决它并实现削减功能的想法。我们使用alleg.lib并在Visual C ++ 2010上工作

Express.We在切割图像后我们对于球的移动并不了解。球移动 - 没有垂直没有水平 - 交叉。他们在不同的地方移动

和不同的方式。他们互相穿过,他们闪烁。双重缓冲不起作用,屏幕上有两个球。你能帮助我们吗?



Hi everyone. We have a project which is given by our lecturer and we don''t move on this project. Firstly our game : www.miniclip.com/games/fat-slice/tr/
Our primary programming language is C. We codified ball function but there is a some mistakes. We don''t fix it and carry out the idea to cutting function. We use alleg.lib and work on Visual C++ 2010
Express.We don''t have an idea about ball''s moving after cutting the image. Balls move as - no vertical no horizontal - cross. They move on different places
and different way. They pass through each other and they are flicker.Double buffering doesn''t work that there is two ball on the screen. Can you help us?

#include <allegro.h>
 
void Baslat();
void Bitir();
void moveBall()
 
int main() {
Baslat();
while (!key[KEY_ESC]) {
 
int width = 220;//width of box
int height = 440;//height of box
int radius = 5;//radius of ball

int x = 110;//initial position of ball
int y = 220;//initial position of ball

int tempX;
int tempY;

//Keep track of direction of motion here.
//0= northwest 1 = southwest, 2 = northeast,
//3 = southeast
int dir;
moveBall();

}
Bitir();
return 0;
}
END_OF_MAIN()
 
void Baslat() {
int depth, res;
allegro_init();
depth = desktop_color_depth();
if (depth == 0) depth = 32;
set_color_depth(depth);
res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
if (res != 0) {
allegro_message(allegro_error);
exit(-1);
}
install_timer();
install_keyboard();
install_mouse();
 
}
void Bitir() {
clear_keybuf();
 
}
void moveBall(){
  //Save current location of ball.
  tempX = x;
  tempY = y;

  switch(dir){
    case 0:
      //Direction is northwest.
      if((x <= radius) || (y <= radius)){
        //Ball has collided with either the left wall or
        // the top wall.
        //Get a new direction. Note that if the new
        // direction is the same as the old one, control
        // will come back to here to get still another
        // direction the next time the functionis called.
        dir = rand() % 4;
      }else{
        //No collision, set new location for the ball
        --x;
        --y;
      }//end else
    break;
    case 1:
      //Direction is southwest.
      if(((x <= radius) || (y >= (height - radius)))){
        //get a new direction
        dir = rand() % 4;
      }else{
        //set new location for the ball
        --x;
        ++y;
      }//end else
    break;
    case 2:
      //Direction is northeast.
      if(((x >= (width - radius)) || (y <= radius))){
        //get a new direction
        dir = rand() % 4;
      }else{
        //set new location for the ball
        ++x;
        --y;
      }//end else
    break;
    case 3:
      //Direction is southeast
      if((((x >= (width - radius)) ||
                              (y >= (height - radius))))){
        //get a new direction
        dir = rand() % 4;
      }else{
        //set new location for the ball
        ++x;
        ++y;
      }//end else

  }//end switch 







这不完全正常。我们有一些切割形状和弹跳球的问题。我们制作弹跳球,但球是闪烁的。双缓冲不起作用。我们的另一个问题是两个形状相同的球通过彼此。我们怎么能解决这个问题?

所以我们的另一个问题 - 我认为这是一个大问题 - 我们不会削减形状。我们能做些什么呢?

















我们对切片的形状一无所知。你能帮助我们吗?并且你发送的功能比那更好

我们快点因为我们应该在6天内完成这个项目。



注意:我们正在使用Allegro v4.2.3。



是否有人用简单的代码帮助我们?



添加了代码块 - OriginalGriff [/ edit]




This is not working exactly. We have some problems that cutting shape and bouncing ball. We make bouncing ball but the ball is flicker. Double buffering is not work on it. Our another problem is that two balls ,which are in the shape, through pass each other. How can we fix that?
So our another problem - i think it is a big problem - that we don''t cut the shape. What can we do to make it?








We don''t have an idea about slicing the shape. Can you help us? And you send the function better than that
We hurry up because we should finish this project in 6 days.

Note : We are using Allegro v4.2.3.

Is there someone who help us with a simple codes?

[edit]Code block added - OriginalGriff[/edit]

推荐答案

对于2个球体之间的碰撞,这里是第一个链接 [ ^ ]我从谷歌搜索''2D球物理''。



对于球的碰撞检查,容器仅检查矩形容器。您应该将容器实现为多边形。也许是一个结构,其中包含构成多边形边缘的线条列表,并保留绕线顺序的标签 [ ^ ]。



完成此设置后,您可以使用基本 2D circle-line collision [ ^ ]。



最后,处理碰撞的代码,如果球与边缘碰撞,则采用随机方向。这似乎不正确。应该从圆线碰撞算法计算合成的运动矢量。来自谷歌的更多链接是此处 [ ^ ]和这里 [ ^ ]。



我希望这个帮助。
For collision between 2 spheres, here''s the first link[^] I got from a google search for ''2D ball Physics''.

The collision checks you have for the ball with the container checks only for a rectangular container. You should implement the container as a polygon. Maybe a structure which contains a list of lines which make up the edges of the polygon and keeps tab of the winding order[^].

Once you have this set up, you can check for collision with the edges using basic 2D circle - line collision[^].

Finally, the code to handle the collision, takes a random direction if the ball collides with an edge. This seems incorrect. The resultant motion vector should be computed from the circle-line collision algorithm. A couple more links from google are here[^] and here[^].

I hope this helps.


一个简单的物理引擎的有趣教程 [ ^ ]包括移动,碰撞球。看看它,它有很好的解释和代码。
There is an interesting tutorial for a simple physics engine[^] that includes moving, colliding balls. Check it out, it comes with great explanations and code.


这篇关于用c编程的游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆