C++ is Fun – Part Nine at Turbine/Warner Bros.! Russell Hanson

Embed Size (px)

Citation preview

  • Slide 1
  • C++ is Fun Part Nine at Turbine/Warner Bros.! Russell Hanson
  • Slide 2
  • Project 1 For people who are still working on Project 1, good news! Extension until Project 2 (May 15)
  • Slide 3
  • Syllabus 1) First program and introduction to data types and control structures with applications for games learning how to use the programming environment Mar 25-27 2) Objects, encapsulation, abstract data types, data protection and scope April 1-3 3) Basic data structures and how to use them, opening files and performing operations on files April 8-10 4) Algorithms on data structures, algorithms for specific tasks, simple AI and planning type algorithms, game AI algorithms April 15-17 Project 1 Due April 17 5) More AI: search, heuristics, optimization, decision trees, supervised/unsupervised learning April 22-24 6) Game API and/or event-oriented programming, model view controller, map reduce filter April 29, May 1 7) Basic threads models and some simple databases SQLite May 6-8 8) Graphics programming, shaders, textures, 3D models and rotations May 13-15 Project 2 Due May 15 9) How to download an API and learn how to use functions in that API, Windows Foundation Classes May 20-22 10) Designing and implementing a simple game in C++ May 27-29 11) Selected topics Gesture recognition & depth controllers like the Microsoft Kinect, Network Programming & TCP/IP, OSC June 3-5 12) Working on student projects - June 10-12 Final project presentations Project 3/Final Project Due June 12
  • Slide 4
  • If you want to grab some media files for the following demos, link: http://wps.aw.com/aw_gaddis_games_1/114/29318/7505573.cw/index.html Media Files The media files contain graphics and audio files that can be used in student projects. MediaFiles_1.zip MediaFiles_2.zip MediaFiles_3.zip Yet another GDK DARK GDK http://www.thegamecreators.com/?m=view_product&id=2128&page=index
  • Slide 5
  • Items A and B Please fill in the paper with two things you would like more information or clarification on that weve covered already, that you want covered in the future, or that would help you with your project: items A and B.
  • Slide 6
  • Foundations of Game APIs, etc.
  • Slide 7
  • Slide 8
  • Slide 9
  • Slide 10
  • Slide 11
  • Slide 12
  • Slide 13
  • Slide 14
  • Slide 15
  • agk::Sync()
  • Slide 16
  • // Includes, namespace and prototypes #include "template.h using namespace AGK; app App; // Constants for the screen resolution const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; // Constants for the sprite indices const int HOUSE_INDEX = 1; const int GHOST_INDEX = 2; // Constants for the ghost's position const float GHOST_X = 200; const float GHOST_Y = 150; // Begin app, called once at the start void app::Begin( void ) { // Set the virtual resolution. agk::SetVirtualResolution(SCREEN_WIDTH, SCREEN_HEIGHT); // Create the haunted house sprite for the background. agk::CreateSprite(HOUSE_INDEX, "haunted_house.png"); // Create the ghost sprite. agk::CreateSprite(GHOST_INDEX, "ghost.png"); // Set the ghost's position. agk::SetSpritePosition(GHOST_INDEX, GHOST_X, GHOST_Y); } // Main loop, called every frame void app::Loop ( void ) { // Display the screen. agk::Sync(); } // Called when the app ends void app::End ( void ) { }
  • Slide 17
  • Ghost is haunting the house! :{
  • Slide 18
  • Slide 19
  • Slide 20
  • Slide 21
  • Slide 22
  • Lets make the ghost move!
  • Slide 23
  • Slide 24 GHOST_START_X) { // Not at the edge yet, so keep moving left. agk::SetSpriteX(GHOST_INDEX, ghostX - INCREMENT); } else { // The sprite is at the left edge of the screen. // Change the game state to reverse directions. g_gameState = MOVING_RIGHT; } // Display the screen. agk::Sync(); } // Called when the app ends void app::End ( void ) { }">
  • // Includes, namespace and prototypes #include "template.h" using namespace AGK; app App; // Global constants for screen resolution const int SCREEN_WIDTH = 640; // Screen width const int SCREEN_HEIGHT = 480; // Screen height // Global constants for the ghost sprite const int GHOST_INDEX = 1; // Ghost sprite index const float GHOST_START_X = 0; // Ghost's starting X const float GHOST_START_Y = 150; // Ghost's starting Y const float GHOST_END_X = 540; // Ghost's ending X const int INCREMENT = 10; // Amount to move the ghost // Global constants for the game state const int MOVING_RIGHT = 0; const int MOVING_LEFT = 1; // Global variable for game state int g_gameState = MOVING_RIGHT; // Begin app, called once at the start void app::Begin( void ) { // Set the virtual resolution. agk::SetVirtualResolution(SCREEN_WIDTH, SCREEN_HEIGHT); // Create the ghost sprite. agk::CreateSprite(GHOST_INDEX, "ghost.png"); // Set the ghost's position. agk::SetSpritePosition(GHOST_INDEX, GHOST_START_X, GHOST_START_Y); } // Main loop, called every frame void app::Loop( void ) { // Get the ghost's current X coordinate. float ghostX = agk::GetSpriteX(GHOST_INDEX); // Is the sprite moving to the right side of the screen? if (g_gameState == MOVING_RIGHT) { // The sprite is moving right. Has it reached the // edge of the screen? if (ghostX < GHOST_END_X) { // Not at the edge yet, so keep moving right. agk::SetSpriteX(GHOST_INDEX, ghostX + INCREMENT); } else { // The sprite is at the right edge of the screen. // Change the game state to reverse directions. g_gameState = MOVING_LEFT; } else { // The sprite is moving to the left. // Has it reached the edge of the screen? if (ghostX > GHOST_START_X) { // Not at the edge yet, so keep moving left. agk::SetSpriteX(GHOST_INDEX, ghostX - INCREMENT); } else { // The sprite is at the left edge of the screen. // Change the game state to reverse directions. g_gameState = MOVING_RIGHT; } // Display the screen. agk::Sync(); } // Called when the app ends void app::End ( void ) { }
  • Slide 25
  • Slide 26
  • This is for you ASCII collision guy!!
  • Slide 27
  • This is (also) for you ASCII collision guy!! 8)
  • Slide 28
  • Slide 29
  • // This program demonstrates collision detection // with a text object and the mouse pointer. // Includes, namespace and prototypes #include "template.h" using namespace AGK; app App; // Constants for the screen resolution const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; // Constant for the text object index number. const int TEXT = 1; // Constant for the text object size. const float TEXT_SIZE = 16; // Constant for the text object alignment. const int ALIGN_CENTER = 1; // Constants for the center of the screen. const float CENTER_X = SCREEN_WIDTH / 2; const float CENTER_Y = SCREEN_HEIGHT / 2; // Begin app, called once at the start void app::Begin( void ) { // Set the virtual resolution. agk::SetVirtualResolution(SCREEN_WIDTH, SCREEN_HEIGHT); // Set the window title. agk::SetWindowTitle("Text Object Collision"); // Create the text object. agk::CreateText(TEXT, ""); // Set the size of the text object. agk::SetTextSize(TEXT, TEXT_SIZE); // Set the alignment of the text object. agk::SetTextAlignment(TEXT, ALIGN_CENTER); // Set the position of the text object. agk::SetTextPosition(TEXT, CENTER_X, CENTER_Y); } // Main loop, called every frame void app::Loop ( void ) { // Get the mouse coordinates. float mouseX = agk::GetRawMouseX(); float mouseY = agk::GetRawMouseY(); // Determine if the mouse pointer has hit the text object. if (agk::GetTextHitTest(TEXT, mouseX, mouseY)) { agk::SetTextString(TEXT, "Ouch! You hit me."); } else { agk::SetTextString(TEXT, "I am a text object."); } // Refresh the screen. agk::Sync(); } // Called when the app ends void app::End ( void ) { } // Set the alignment of the text object. agk::SetTextAlignment(TEXT, ALIGN_CENTER); // Set the position of the text object. agk::SetTextPosition(TEXT, CENTER_X, CENTER_Y); } // Main loop, called every frame void app::Loop ( void ) { // Get the mouse coordinates. float mouseX = agk::GetRawMouseX(); float mouseY = agk::GetRawMouseY(); // Determine if the mouse pointer has hit the text object. if (agk::GetTextHitTest(TEXT, mouseX, mouseY)) { agk::SetTextString(TEXT, "Ouch! You hit me."); } else { agk::SetTextString(TEXT, "I am a text object."); } // Refresh the screen. agk::Sync(); } // Called when the app ends void app::End ( void ) { }
  • Slide 30
  • Slide 31
  • Slide 32
  • // This program demonstrates how sprite // collisions can be detected. // Includes, namespace and prototypes #include "template.h" using namespace AGK; app App; // Constants for the screen resolution const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; // Constant for the image index numbers. const int BALL1_IMAGE = 1; const int BALL2_IMAGE = 2; // Constant for the sprite index numbers. const int BALL1_SPRITE = 1; const int BALL2_SPRITE = 2; // Constant for ball 1's initial X position. const float BALL1_X = 0; // Constant for ball 2's initial Y position. const float BALL2_X = 511; // Constant for the Y position of both sprites. const float BALL_Y = 175; // Constant for the distance to move each frame. const float DISTANCE = 1; // Begin app, called once at the start void app::Begin( void ) { // Set the virtual resolution. agk::SetVirtualResolution(SCREEN_WIDTH, SCREEN_HEIGHT); // Set the window title. agk::SetWindowTitle("Sprite Collision"); // Load the images. agk::LoadImage(BALL1_IMAGE, "BowlingBall1.png"); agk::LoadImage(BALL2_IMAGE, "BowlingBall2.png"); // Create the sprites. agk::CreateSprite(BALL1_SPRITE, BALL1_IMAGE); agk::CreateSprite(BALL2_SPRITE, BALL2_IMAGE); // Set the position of each sprite. agk::SetSpritePosition(BALL1_SPRITE, BALL1_X, BALL_Y); agk::SetSpritePosition(BALL2_SPRITE, BALL2_X, BALL_Y); } // Main loop, called every frame void app::Loop ( void ) { // Get the X-coordinate of each sprite. float ball1x = agk::GetSpriteX(BALL1_SPRITE); float ball2x = agk::GetSpriteX(BALL2_SPRITE); // Determine if the two sprites have collided. if (agk::GetSpriteCollision(BALL1_SPRITE, BALL2_SPRITE)) { // Reset the sprites to their original locations. agk::SetSpriteX(BALL1_SPRITE, BALL1_X); agk::SetSpriteX(BALL2_SPRITE, BALL2_X); } else { // Move ball 1 to the right. agk::SetSpriteX(BALL1_SPRITE, ball1x + DISTANCE); // Move ball 2 to the left. agk::SetSpriteX(BALL2_SPRITE, ball2x - DISTANCE); } // Refresh the screen. agk::Sync(); } // Called when the app ends void app::End ( void ) { } // Create the sprites. agk::CreateSprite(BALL1_SPRITE, BALL1_IMAGE); agk::CreateSprite(BALL2_SPRITE, BALL2_IMAGE); // Set the position of each sprite. agk::SetSpritePosition(BALL1_SPRITE, BALL1_X, BALL_Y); agk::SetSpritePosition(BALL2_SPRITE, BALL2_X, BALL_Y); } // Main loop, called every frame void app::Loop ( void ) { // Get the X-coordinate of each sprite. float ball1x = agk::GetSpriteX(BALL1_SPRITE); float ball2x = agk::GetSpriteX(BALL2_SPRITE); // Determine if the two sprites have collided. if (agk::GetSpriteCollision(BALL1_SPRITE, BALL2_SPRITE)) { // Reset the sprites to their original locations. agk::SetSpriteX(BALL1_SPRITE, BALL1_X); agk::SetSpriteX(BALL2_SPRITE, BALL2_X); } else { // Move ball 1 to the right. agk::SetSpriteX(BALL1_SPRITE, ball1x + DISTANCE); // Move ball 2 to the left. agk::SetSpriteX(BALL2_SPRITE, ball2x - DISTANCE); } // Refresh the screen. agk::Sync(); } // Called when the app ends void app::End ( void ) { }
  • Slide 33
  • Preview of the future, in the future we will cover more: Polymorphism in C++
  • Slide 34
  • Exceptions
  • Slide 35
  • And, Threads
  • Slide 36
  • Overview of Standard Library headers
  • Slide 37
  • Slide 38
  • Homework Exercises For Next Monday (Pick 2) 3) 1)Download and install http://www.appgamekit.com/http://www.appgamekit.com/ (2D only, but simpler) OR http://www.ogre3d.org/ OR DARK GDK http://www.thegamecreators.com/?m=view_product&id=2128&page=indexhttp://www.thegamecreators.com/?m=view_product&id=2128&page=index. Compile, link, and run one of the sample programs. Implement a simple Tic Tac Toe AI strategy. Some sample implementations of the game are at the following two links: http://courses.ischool.berkeley.edu/i90/f11/resources/chapter06/tic-tac- toe.py http://en.literateprograms.org/Tic_Tac_Toe_(Pythonhttp://en.literateprograms.org/Tic_Tac_Toe_(Python) 2)
  • Slide 39
  • 4)
  • Slide 40
  • 5)
  • Slide 41
  • 6)Implement something cool using the App Game Kit, and sprites. Surprise me. 7)Delve into the App Game Kit internals, and describe the collision detection algorithm the AGK uses for agk::GetSpriteCollision(BALL1_SPRITE, BALL2_SPRITE)