WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will: Discuss video games and how...

Post on 20-Jan-2016

214 views 0 download

Transcript of WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will: Discuss video games and how...

Programming Video Games

Class Agenda

We will: Discuss video games and how they work Overview our tools, and install them Develop a plan for creating Pong Write a basic Pong game Elaborate on the game, time permitting

What is a video game?How do they work?

Video Games

Interactive Tight

communication between player and game

Player has control over the video game

Imaginative Entertaining

LoadingLoading ControlControl

LogicLogic

FeedbackFeedbackUnloadingUnloading

Why should we use C/C++?Why is Allegro good to start with?

C/C++ and Allegro

Programming Language

High level, but lower than BASIC

More complicated, but more versatile

Universal Strongly-typed

#include<stdio.h>

main() { printf("Hello World");

}

Library Cross-Platform Very easy to learn

and use Has powerful

features Has 3D capability

with OpenGL

Windows is simple install

Linux: You need X11 development package You need gcc You need Allegro

Standard ./configure + make depend + make + make install

So, how are we going to make it?

Breaking Down Pong

Two paddles and a ball – simple, effective.

Pong

Initialize Allegro Load images into memory Make variables to hold temporary info

Location of paddles, and the ball The player score The speed of the ball

Make sure everything worked Start the game loop

Check if keys are up or down Optionally, use the mouse to move

Processing (Logic)

Adjust the paddle location (based on input) Adjust the ball location, using the ball

speed Check to see if the ball hit the paddle

If it did, bounce it off Check to see if the ball hit the wall

If it did, bounce it off Check to see if anyone scored

Update the score if they did, restart

Drawing (Feedback)

Delete what was on the screen before Draw the paddles Draw the ball

Initializing Allegro

Programming Pong

Initializing Allegro

#include “allegro.h”

main () {allegro_init();install_keyboard();install_mouse()allegro_message(“Hello!”);allegro_exit();

}

Setting Bit-Depth

Bit-depth is a measurement of the amount of color that a computer can process 8 bit == 256 colors, 32 bit == millions of

colors Allegro was designed for 8-bit systems In order for it to work, we need to set it to

32-bitint depth = desktop_color_depth();if (depth == 0) depth = 32;set_color_depth(depth)

Selecting a Graphics Mode

set_gfx_mode(type of window, width, height, 0, 0);

int res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);

if (res != 0) {

allegro_message(allegro_error);

exit(-1);

}

Drawing to the ScreenDouble-bufferingTransparency

Programming Pong

Adding a Main Loop

A main loop is the most important part of a game

while (!key[KEY_ESC]) {

// action happens here

}

Drawing to the Screen

BITMAP *paddle;

paddle = load_bitmap(“paddle1.bmp”, NULL)

draw_sprite(where to draw the image to, the image itself, x location, y location)

draw_sprite(screen, paddle, 20, 240);

Moving the Paddle

int paddle1y = 240;

if(key[KEY_DOWN]) {

paddle1y = paddle1y + 3;

}

draw_sprite(screen, paddle, 20, paddle1y);

clear_bitmap(screen);

Double Buffering

Monitors refresh (update) ~70 times a second

Drawing a sprite to a screen is SLOW

Copying memory <-> memory is FAST

Solution? Have two copies of the

screen Draw on the copy that

is NOT on the real screen

Swap the copies

Double Buffering

BITMAP *buffer;buffer = create_image(SCREEN_W,

SCREEN_H);

while(!key[KEY_ESC]) {// draw to BUFFERdraw_sprite(buffer, paddle, 20, paddle1y);

draw_sprite(screen, buffer, 0, 0);clear_bitmap(buffer);

}

Transparency

draw_sprite() will NEVER draw hot pink

The value for hot pink in RGB is (255,0,255)

We use hot pink for transparency

Ball PhysicsBouncingFrame-rate

Programming Pong

Ball Physics

A ball will have an (x,y) location

A ball also has a velocity, or speed in (x,y) format

To find the next location of the ball, we ADD the velocity to the current location

Ball Physics

int ball_x = 320;int ball_y = 240;

int ball_vel_x = 1;int ball_vel_y = 0;

ball_x = ball_x + ball_vel_x;ball_y = ball_y + ball_vel_y;

Ball Bouncing

Imagine a basketball falling towards the floor

It is falling down What is its velocity?

It has hit the ground What is its velocity, now?

It is coming back up What is this finally velocity?

Ball Bouncing

if(ball_y >= SCREEN_H) { ball_y_vel = -ball_y_vel;

} if(ball_y <= 0) {

ball_y_vel = -ball_y_vel; }

Frame-rate (FPS)

Frame-rate is how fast the game runs It is usually measured in FPS (Frames Per

Second) The number of cycles the game

completes in one second is the frame-rate

A good frame rate to run at is ~30 fps

rest(5);

Keeping ScoreCollisionAI

Programming Pong

Keeping Score

When is a point scored? When it passes the right edge (x = 640) Or the left edge (x = 0)

We need to check the ball to determine if a point is scored.

The players’ score is stored in an integer value

Keeping Score

int player_1_score = 0;int player_2_score = 0;

if(ball_x >= 640) {player_1_score = player_1_score + 1;ball_x = 0;ball_y = 0;

}if(ball_x <= 0) {

player_2_score = player_2_score + 1;ball_x = 0;ball_y = 0;

}

Collision

A simple method of collision is box-box

An imaginary box is drawn over a sprite

If two boxes intersect, there is a collision

We can check box collision by checking corners

Demonstration

Artificial Intelligence

We will be using a finite-state machine If the ball is above the paddle, the

paddle will move up If the ball is below the paddle, the

paddle will move down Very simple “if” statements

Artificial Intelligence

if(ball_y > paddle2y) { paddle2y += 5;}

if(ball_y < paddle2y) {

paddle2y -= 5; }

Conclusion

Where do we go next? Make the game 2 player Add a menu to the game Make the game end after a score of 10 Add power-ups to the game

Resources Other game ideas