with c/c++ and allegro programming video games. class agenda we will: discuss video games and how...

40
Programming Video Games

Upload: mary-cunningham

Post on 20-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

Programming Video Games

Page 2: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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

Page 3: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

What is a video game?How do they work?

Video Games

Page 4: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

Interactive Tight

communication between player and game

Player has control over the video game

Imaginative Entertaining

Page 5: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

LoadingLoading ControlControl

LogicLogic

FeedbackFeedbackUnloadingUnloading

Page 6: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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

C/C++ and Allegro

Page 7: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

Programming Language

High level, but lower than BASIC

More complicated, but more versatile

Universal Strongly-typed

Page 8: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

#include<stdio.h>

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

}

Page 9: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

Library Cross-Platform Very easy to learn

and use Has powerful

features Has 3D capability

with OpenGL

Page 10: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

Windows is simple install

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

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

Page 11: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

So, how are we going to make it?

Breaking Down Pong

Page 12: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

Two paddles and a ball – simple, effective.

Pong

Page 13: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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

Page 14: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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

Page 15: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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

Page 16: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

Drawing (Feedback)

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

Page 17: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

Initializing Allegro

Programming Pong

Page 18: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

Initializing Allegro

#include “allegro.h”

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

}

Page 19: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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)

Page 20: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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);

}

Page 21: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

Drawing to the ScreenDouble-bufferingTransparency

Programming Pong

Page 22: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

Adding a Main Loop

A main loop is the most important part of a game

while (!key[KEY_ESC]) {

// action happens here

}

Page 23: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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);

Page 24: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

Moving the Paddle

int paddle1y = 240;

if(key[KEY_DOWN]) {

paddle1y = paddle1y + 3;

}

draw_sprite(screen, paddle, 20, paddle1y);

clear_bitmap(screen);

Page 25: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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

Page 26: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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);

}

Page 27: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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

Page 28: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

Ball PhysicsBouncingFrame-rate

Programming Pong

Page 29: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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

Page 30: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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;

Page 31: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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?

Page 32: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

Ball Bouncing

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

} if(ball_y <= 0) {

ball_y_vel = -ball_y_vel; }

Page 33: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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);

Page 34: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

Keeping ScoreCollisionAI

Programming Pong

Page 35: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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

Page 36: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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;

}

Page 37: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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

Page 38: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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

Page 39: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

Artificial Intelligence

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

if(ball_y < paddle2y) {

paddle2y -= 5; }

Page 40: WITH C/C++ AND ALLEGRO Programming Video Games. Class Agenda We will:  Discuss video games and how they work  Overview our tools, and install them

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