Introduction to Game Programming

Similar documents
Introduction to Game Programming

Programming challenges in C++

Functional programming in C#

Quick Start Referee Guide Use this Quick Start Guide if you are new to coachfx and are looking to find out how to use the various referee functions

[ the academy_of_code] Senior Beginners

Eastside Literacy Tutor Support Word Processing Tutorial

Session 4 Starting the Air Raid Game

Pointers, Arrays and Parameters

Pong in Unity a basic Intro

Making use of other Applications

Creating the Tilt Game with Blender 2.49b

Fruit Snake SECTION 1

Learning the Pro/ENGINEER Interface

The Stack, Free Store, and Global Namespace

Instructions for Crossword Assignment CS130

NetLogo Tutorial Series: Langton's Ant. Nicholas Bennett Grass Roots Consulting

Installing macos High Sierra

How to Program a Primitive Twin-Stick Shooter in Monogame 3.4

AN INTRODUCTION TO SCRATCH (2) PROGRAMMING

Pacman. you want to see how the maze was created, open the file named unity_pacman_create_maze.

Programming Exercise

Introduction to the Turing Programming Language program commands run Exercise #1 not var put get put Programming Names File/Save As..

Arduino IDE Friday, 26 October 2018

Google SketchUp Design Exercise 1

= 3 + (5*4) + (1/2)*(4/2)^2.

Tutorial 3: Constructive Editing (2D-CAD)

Linux Tutorial #1. Introduction. Login to a remote Linux machine. Using vim to create and edit C++ programs

Basic Computer Programming (Processing)

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

LAB 7 Writing Assembly Code

Using Inheritance to Share Implementations

Engr 123 Spring 2018 Notes on Visual Studio

Breakout YEAH hours. Brahm Capoor & Jared Wolens

CS12020 for CGVG. Practical 2. Jim Finnis

Table of Contents 1.1. Introduction Installation Quick Start Documentation Asynchronous Configuration 1.4.

CSci 1113, Fall 2018 Lab Exercise 11 (Week 13): Graphics. Warm-up

Warmup : Name that tune!

Art, Nature, and Patterns Introduction

OneNote 2016 Tutorial

IDE: Integrated Development Environment

Introduction to Computers By Jennifer King, YA and Marketing Librarian, Great Bend Public Library

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Laboratory Exercise 5

Introduction to Unreal Engine Blueprints for Beginners. By Chaven R Yenketswamy

D - Tic Tac Toe. Let's use our 9 sparkles to build a tic tac toe game! 2017 courses.techcamp.org.uk/ Page 1 of 9

Part 1 Simple Arithmetic

Tutorial 5. Website - Create a folder on the desktop called tutorial 5. Editor Brackets. Goals. Create a website showcasing the following techniques

ISY00245 Principles of Programming. Module 7

APA-STYLE TABLES in MICROSOFT WORD 2007/2010

COMP : Practical 6 Buttons and First Script Instructions

Windows XP. A Quick Tour of Windows XP Features

Vision Pointer Tools

Demo Scene Quick Start

CISC 1600 Lecture 3.1 Introduction to Processing

Address Bar. Application. The space provided on a web browser that shows the addresses of websites.

Exercise NMCGJ: Animated Graphics

Adobe Flash CS3 Reference Flash CS3 Application Window

ACTIVE PROCESSING Summary Learning Objectives How to Proceed Check your Understanding Learning Objectives 412

Lesson 18: Animation. Computer Programming is Fun!

VBA Excel 2013/2016. VBA Visual Basic for Applications. Learner Guide

We will start our journey into Processing with creating static images using commands available in Processing:

Minecraft Due: Mar. 1, 2015

Scripting Tutorial - Lesson 11: Advanced: Introducing Classes

Creating a prezi. Setting up and choosing a template. Go to Sign up if you do not already have an account.

Section 5: Editing Cells

Animation Workshop. Support Files. Advanced CODING CHALLENGE - ADVANCED

Session 8.2. Finding Winners Using Arrays

WAYLAND FREE PUBLIC LIBRARY 3D Design and Printing Tutorial: Create a Keychain

Creating Breakout - Part 2

The StarPanel. Introduction

View the full TurnItIn report. Submit the file to TurnItIn for originality checking

CS 134 Programming Exercise 9:

Binary Search. CS 5010 Program Design Paradigms Bootcamp Lesson 8.2

2.2 - Layouts. Bforartists Reference Manual - Copyright - This page is Public Domain

Damaging, Attacking and Interaction

Your First Windows Form

Exercise 2-1. Programming, Using RoboCIM EXERCISE OBJECTIVE

Exercise 2-2. Programming, Using the Teach Pendant EXERCISE OBJECTIVE

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

SIMPLE TEXT LAYOUT FOR COREL DRAW. When you start Corel Draw, you will see the following welcome screen.

Simulator. Chapter 4 Tutorial: The SDL

Session 8 Finishing Touches to the Air Raid Game

Using Microsoft Excel for Recording and Analyzing Data Noah Segall

Get started with SketchUp!

COS 126 Exam Review. Exams overview Example programming exam Example written exam questions (part 1)

Word Processing. 2 Monroe County Library System

Beijer Electronics AB 2000, MA00453,

Frame Editor 2 Manual

[ 8 marks ] Demonstration of Programming Concepts

GRAPHICS PROGRAMMING. LAB #3 Starting a Simple Vector Animation

Notes 3: Actionscript to control symbol locations

RAM JAYAM VIDYAA MANDIR MATRIC HR SEC SCHOOL. Cumbum. Theni district. 1 D.EASWARAN M.Sc B.Ed Computer Instructor RJMS_SCHOOL.

The Maze Runner. Alexander Kirillov

Introduction to C. Sami Ilvonen Petri Nikunen. Oct 6 8, CSC IT Center for Science Ltd, Espoo. int **b1, **b2;

Exam duration: 3 hours Number of pages: 13 Number of points: 20

That means circular linked list is similar to the single linked list except that the last node points to the first node in the list.

Start Visual Studio, create a new project called Helicopter Game and press OK

Using Flash Animation Basics

Class Test 10. Question 1. Create a console application using visual studio 2012 ultimate.

Procedure: Determine the polarity of the LED. Use the following image to help:

Transcription:

Introduction to Game Programming Console video game Session 2 The game loop and user input Nacho Iborra IES San Vicente This work is licensed under the Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/

Index of contents 1.Introduction... 3 2.The game loop...4 3.Getting user input... 6 3.1.Improving the user input...7 4.Partial redrawings... 9 4.1.Erasing old positions...10 Programming Introduction to Game Programming 2.

1. Introduction In this session we will cover two different concepts. Firstly, we will see the general structure of (almost) every game loop, and the different operations that must be performed during each iteration. We will apply this loop to our video game in its current state. Then, we will see how to take information from the user (this is, movement orders), and apply them to an object of the screen. Finally, we will see how to improve these concepts so that the behavior of the video game will be much more fluid. Programming Introduction to Game Programming 3.

2. The game loop The main part of a game is a loop that repeats a list of actions. These actions are (normally): 1. Draw elements on the screen. In some games, you will need to "delete" previous screen to draw the new content. In C# console applications, this can be done with the instruction: Console.Clear(); 2. Check if the user has introduced any input (key pressed, gamepad moved, mouse clicked...) 3. Move enemies and any other object of the scene that needs to be moved 4. Check collisions between objects and characters, to check if we have collided with an enemy, a shot, a wall... or if we have collected an item, for instance. With these tests, we can update points, lives and any other information about the game state. 5. Apply a pause after each iteration, so that the videogame does not run very fast on newer computers These steps repeat while the game is running (i.e. the number of lives is greater than 0, we have not reached the end of the game yet and we have not exited the game in any way). Let's implement a first approach to this game loop. To simplify it, we will repeat the loop indefinitely, and we will leave most of the tasks empty, to be completed later when we get more knowledge about C# programming. using System; public class MyGame public static void Main() // Declare variables here // Loop repeats indefinitely while (1 == 1) // 1. Draw elements. In this place we will put the code // written in previous session for now. Console.Clear(); Console.SetCursorPosition(...); // 2. Read input and calculate player's new position Programming Introduction to Game Programming 4.

// 3. Move enemies and other objects // 4. Check collisions and update game state Exercise 1 // 5. Pause Update the class MyGame from previous sessions with a loop like the one seen in previous example. Put the code that you have already written previously in the step 1 of the loop (declare all the variables needed before the game loop). NOTE: if you run the game with these changes, it will never end. You will need to close the console manually to finish it. We will see later how to set another end conditions. NOTE 2: you will also note a blinking when running the program. It is normal, since we clear and redraw the whole console after each iteration. We will see later in this session how to avoid this blinking in console games. Programming Introduction to Game Programming 5.

3. Getting user input Now, we are going to get the user input and move the main character according to what user has just typed. We will use the keys A and D to move the character to the left and right respectively. First of all, we need to specify the coordinates of our main character, and draw it in the console. To do this, we are going to define two "int" variables called "characterx" and "charactery", and place the bar in X = 18, Y = 23: int characterx = 19, charactery = 23; Then, we print the main character in the specified coordinates. As it is going to be a bar, we need to print some whitespaces (five, for instance). Do this in step #1 of the game loop. while (1 == 1) // 1. Draw elements. In this place we will put the code // written in previous session for now. // Print main character Console.SetCursorPosition(characterX, charactery); Console.BackgroundColor = ConsoleColor.White; Console.Write(" "); Console.ResetColor(); Now let's move to step number 2 of the game loop (get user input), and fill it with something like this: using System; public class MyGame public static void Main() int characterx = 19, charactery = 21; string key; // Other variables declared previously // Loop repeats indefinitely while (1 == 1) // 1. Draw elements. In this place we will put the code // written in previous session for now. Programming Introduction to Game Programming 6.

// 2. Read input and calculate player's new position key = Console.ReadLine(); if (key == "a") characterx--; if (key == "d") characterx++; // 3. Move enemies and other objects // 4. Check collisions and update game state // 5. Pause We have also defined a variable called key to store the text written by the user, and two integer variables (characterx and charactery) to store the current position of the main character. Exercise 2 Update your game program with these instructions to move the main character. Remember to link the character position with the characterx and charactery variables, so that, if you change the value of these variables, the position of the character will be automatically updated. NOTE: to read the user input in every iteration, set the cursor position to X = 0, Y = 24 so that you will not type anything within the game board. 3.1. Improving the user input Having to press Enter after each input is not very friendly for the user. We can improve it if we access the keyboard directly, with ConsoleKeyInfo object. Just replace the key variable with a new variable of type ConsoleKeyInfo, and the new lines written in step 2 in previous example with these lines below. Besides, we will use the arrow keys instead of A-D keys to move the character: using System; public class MyGame Programming Introduction to Game Programming 7.

public static void Main() int x, y; string key; // Delete this variable ConsoleKeyInfo key; // Other variables declared previously // Loop repeats indefinitely while (1 == 1) // 1. Draw elements. In this place we will put the code // written in session 01 for now. // 2. Read input and calculate player's new position key = Console.ReadLine(); // Delete this line key = Console.ReadKey(false); if (key == "a") characterx--; // Delete these lines if (key == "d") characterx++; if (key.key == ConsoleKey.RightArrow) characterx++; if (key.key == ConsoleKey.LeftArrow) characterx--; // 3. Move enemies and other objects // 4. Check collisions and update game state Exercise 3 // 5. Pause Update the game by changing the code to move the character typed in exercise 2 with the new code shown in previous example. Programming Introduction to Game Programming 8.

4. Partial redrawings In graphical environments, the graphics engine automatically clears the screen and redraws the components in it, but we don't see any blinking because of the graphics capabilities. Unfortunately, we can't say the same when working with console video games, since console is not designed for quick redrawing, and everytime we clean it to redraw the elements, we see how it blinks. But we have a solution to avoid this blinking. We must pay attention at which elements of our video game are changing at each iteration, and redraw only these elements. In our case, only the main character (and the ball, later) change their position after each iteration, so we can take the rest of the code outside the game loop. We will get something like this: using System; public class MyGame public static void Main() // Variable declarations // Draw right half and texts while (1 == 1) // 1. Draw movable elements (character and ball) // 2. Read input and calculate player's new position // 3. Move enemies and other objects // 4. Check collisions and update game state // 5. Pause Programming Introduction to Game Programming 9.

Exercise 4 Update the code with these changes and test it. You will see that the screen no longer blinks. However, old positions of the main character are not erased. 4.1. Erasing old positions The final step is to erase the character's old position before moving it to the new one. To do this, we have to enter these instructions BEFORE changing the coordinates of the character: 1. Set cursor position to character's current X and Y 2. Print as many white spaces as character s length (with black background, in our case) Then, we can change the corresponding coordinates, and in the next iteration the character or enemy will be placed in its new position. Exercise 5 Add these instructions before updating the position of the character, and see how it moves without leaving a trail behind it. Programming Introduction to Game Programming 10.