⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objects.c

📁 乒乓球游戏程序
💻 C
字号:
/*----------------------------------------------------------------
 * objects.c -- object manipulation routines
 *----------------------------------------------------------------
 *  These routines coordinate the movement of game objects.
 *  Just about anything that need coordinating counts as a
 *  game object.  The actual processing of the objects is 
 *  done by separate modules, one for each object.  This
 *  module just keeps track of the objects.
 */

#include "error.h"
#include "objects.h"
#include "objdisp.h"
#include "gamevars.h"
#include "layout.h"
#include "input.h"
#include "inptyps.h"
#include "bat.h"
#include "ball.h"

/* bat: (local)
 *  struct bat_t is defined in bat.h -- it holds information
 *  about the bat.  This variable is local (static), not global,
 *  because not many modules need to know about it.  When we ask
 *  the `bat' module to deal with the bat we'll pass the variable
 *  to the routine -- this will have more advantages later on.
 */
static struct bat_t bat;

/* ball: (local)
 *  See the description above, for `bat'.
 */
static struct ball_t ball;


/* objects_init:
 *  This function initialises all the objects in the game, with the
 *  help of some subsidiary modules.
 */
void objects_init() {
	struct input_t *input;

	/* You can change the input type here, for now.  Later we'll
	 * let the user choose at run-time, of course.  Put exactly
	 * one of these outside the #if 0 ... #endif. */

	input = input_create_keyboard (KEY_LEFT, KEY_RIGHT, 0, 0, 0, 0);
#if 0
	input = input_create_mouse (0);
	input = input_create_joystick (1);
#endif

	if (!input) error ("Unable to create input device", NULL);
	bat_setup (&bat, arena_width/2, arena_height - bat_height, bat_length, bat_colour, input);
	ball_setup (&ball, arena_width/2, arena_height/2, 1, -1, ball_colour);
}


/* objects_shutdown:
 *  Yep, you guessed it -- this undoes what objects_init does.
 */
void objects_shutdown() {
	ball_unsetup (&ball);
	bat_unsetup (&bat);
}


/* objects_update:
 *  Called once every game cycle to update all the objects.
 */
void objects_update() {
	bat_update (&bat);
	ball_update (&ball);

	/* Check whether the ball should bounce off the bat */
	if ((((ball.y >= bat.y) && (ball.oy < bat.y))
	  || ((ball.y <= bat.y) && (ball.oy > bat.y)))
	  && (ball.x >= bat.x) && (ball.x <= bat.x + bat.l))
	{
		ball_bounce_y (&ball);
		game_score++;
	}

	/* Check whether the player has missed the ball */
	if (ball.y > arena_height) {
		game_end_flag = 1;
	}
}


/* objects_draw:
 *  Called by the display module to draw all the objects.
 */
void objects_draw (BITMAP *bmp) {
	bat_draw (bmp, &bat);
	ball_draw (bmp, &ball);
}


/* objects_erase:
 *  Called by the display module to erase all the objects.
 */
void objects_erase (BITMAP *bmp, BITMAP *bkgnd) {
	bat_erase (bmp, bkgnd, &bat);
	ball_erase (bmp, bkgnd, &ball);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -