SDLでアクションゲーム

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
いーめー
記事: 4
登録日時: 8年前

SDLでアクションゲーム

#1

投稿記事 by いーめー » 8年前

以前も質問させてもらいました。
いろいろとサンプルを参考にしながら、アクションゲームを作っています。
今、とりあえず下記のようなプログラムなのですが、(1)主人公を画像として表示する方法(2)敵をマップに出現させる方法がわからず悩んでいます。
どなたかご教授願います。

コード:

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <stdio.h>
#include <stdlib.h>

#define RANDOM(N)  ((int)(((double)rand()/RAND_MAX)*(N)))
#define Win_X  1280
#define Win_Y  960
#define ColorDepth 8
#define RECTSIZE   100

#define CHARSIZ 8
int charx, chary;
int jumpcnt, jumpflg;
#define JUMPHIGH 6
#define JUMPSPD 8

#define BLOCKSIZ 32
#define MAPWID (Win_X/BLOCKSIZ)
#define MAPHIG (Win_Y/BLOCKSIZ)
char map[MAPWID][MAPHIG];
void init_map()
{
  int i, j;
  for (i = 0; i < MAPWID; i++) {
    map[i][MAPHIG-1] = 1;
  }
}

int hit_map(int ax, int ay){
  int i, j;
  int x, y, x2, y2;
  x = ax / BLOCKSIZ;
  if (x< 0 || x >= MAPWID) return 0;
  y = ay / BLOCKSIZ;
  if (y< 0 || y >= MAPHIG) return 0;
  x2 = (ax+ CHARSIZ-2) / BLOCKSIZ;
  if (x2< 0 || x2 >= MAPWID) return 0;
  y2 = (ay+CHARSIZ-2) / BLOCKSIZ;
  if (y2< 0 || y2 >= MAPHIG) return 0;
  
  for (i = 0; i < CHARSIZ / BLOCKSIZ; i++) {
    for (j = 0; j < CHARSIZ / BLOCKSIZ; j++) {
      if (map[x+i][y+j]) return 1;
    }
  }
  
  for (i = 0; i < CHARSIZ / BLOCKSIZ; i++) {
    if (map[x2][y+i]) return 1;
  }
  
  for (i = 0; i < CHARSIZ / BLOCKSIZ; i++) {
    if (map[x+i][y2]) return 1;
  }
  
  if (map[x2][y2]) {
    return 1;
  }
  
  return 0;
}

void move(int xv, int yv)
{
  int maxv, i;
  jumpflg = 0;
  if (abs(xv) > abs(yv)){
    maxv = abs(xv);
  } else {
    maxv = abs(yv);
  }
  for (i = 0; i < maxv; i++) {
    if ( abs(xv) > i) {
      if (!hit_map(charx + xv, chary) ){
	charx += xv > 0 ? 3: -3;
      }
    }
    if ( abs(yv) > i) {
      if (!hit_map(charx, chary + yv) ){
	chary += yv > 0 ?  1:  -1;
      } else if (yv > 0) {
	jumpflg = 1;
      }
    }
  }
}

void put_box(SDL_Surface *screen, int x, int y, int w, int h, Uint32 color)
{
  SDL_Rect dest;
  dest.w = w;
  dest.h = h;
  dest.x = x;
  dest.y = y;
  
  SDL_FillRect(screen,&dest,color);
}

void disp_map(SDL_Surface *screen)
{
  int i, j;
  for (i = 0; i < MAPWID; i++) {
    for (j = 0; j < MAPHIG; j++) {
      if (map[i][j]) {
	put_box(screen, i*BLOCKSIZ, j*BLOCKSIZ, BLOCKSIZ, BLOCKSIZ, SDL_MapRGB(screen->format, 0, 255, 0));
      }
    }
  }
}

void ctrl(SDL_Event e){
  static int charxvel, charyvel;
  
  switch(e.type){
  case SDL_KEYDOWN:
    switch(e.key.keysym.sym){
    case SDLK_LEFT:
      charxvel  = -1;
      break;
    case SDLK_RIGHT:
      charxvel = 1;
      break;
    case SDLK_SPACE:
      if (jumpflg) {
	jumpcnt = JUMPHIGH;
      }
      break;
    case SDLK_z:
      break;
    case SDLK_x:
      break;
    default:
      break;
    }
    break;
  case SDL_KEYUP:
    switch(e.key.keysym.sym){
    case SDLK_LEFT:
      charxvel  = 0;
      break;
    case SDLK_RIGHT:
      charxvel = 0;
      break;
    default:
      break;
    }
  }
  if ((charxvel  == -1 && charx <= 0) || (charxvel  == 1 &&charx >= WinXSize - CHARSIZ)){
    charxvel = 0;
  }
  if (jumpcnt > 0) {
    charyvel = -JUMPSPD;
    jumpcnt --;
  } else {
    charyvel = JUMPSPD;
  }
  move(charxvel, charyvel);
}

void gmain(SDL_Surface *screen){
  put_box(screen, 0, 0, Win_X, Win_Y, SDL_MapRGB(screen->format, 0, 0, 0));
  disp_map(screen);
  put_box(screen, charx, chary, CHARSIZ, 10, SDL_MapRGB(screen->format, 255, 0, 0));
  SDL_UpdateRect(screen,0,0,0,0);
}

int main(int argc,char *argv[])
{
  SDL_Surface *screen;
  SDL_Event e;
  int done = 0;

  SDL_Surface *image;
  SDL_Rect rect, scr_rect;

  if(SDL_Init(SDL_INIT_VIDEO)<0)
    exit(1);

  screen = SDL_SetVideoMode(Win_X,Win_Y,ColorDepth,SDL_HWSURFACE);
  if(screen == NULL){
    SDL_Quit();
    exit(2);
  }

  init_map();
  do{
    if (SDL_PollEvent(&e) != 0){
      switch(e.type){
      case SDL_QUIT:
	done = 1;
	break;
      }
    }
    ctrl(e);
    gmain(screen);
    SDL_Delay(16);
  }while(!done);

  image = SDL_LoadBMP("sample.bmp");
  
  rect.x = 0;
  rect.y = 0;
  rect.w = image->w;
  rect.h = image->h;
  
    scr_rect.x = 0;
  scr_rect.y = 0;
  

  SDL_BlitSurface(image, &rect, SDL_GetVideoSurface(), &scr_rect);
  
  SDL_Flip(SDL_GetVideoSurface());
  
  SDL_FreeSurface(image);
    
  SDL_Quit();

  return 0;
}

“C言語何でも質問掲示板” へ戻る