PC01 Term 3 Project Snake. PC01 Term 3 Project Snake.

Slides:



Advertisements
Similar presentations
Introduction to Macromedia Director 8.5 – Lingo
Advertisements

Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
RAPTOR Syntax and Semantics By Lt Col Schorsch
Cosc 5/4730 Game Design. A short game design primer. A game or animation is built on an animation loop. – Instance variables of “objects” are updated.
Introduction to Computing Science and Programming I
Code Club Session 3 Shark Eats Fish. Picture of finished product here.
Mrs. Chapman. Tabs (Block Categories) Commands Available to use Script Area where you type your code Sprite Stage All sprites in this project.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
Introduction to TouchDevelop
Making a Boat Racing Game in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University, July 2010.
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
KeyListener and Keyboard Events Another type of listener listens for keyboard entry – the KeyListener which generates KeyEvents –to implement KeyListener,
11 Adding Tomato Targets Session Session Overview  We now have a game which lets a player bounce a piece of cheese on a bread bat  Now we have.
Game Maker Terminology
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Lesson 3: Arrays and Loops. Arrays Arrays are like collections of variables Picture mailboxes all lined up in a row, or storage holes in a shelf – You.
Tutorial for Arrays and Lists. Description This presentation will cover the basics of using Arrays and Lists in an Alice world It uses a set of chickens.
CHAPTER 2 The Game Loop - Variables, Types, Classes and Objects in XNA XNA Game Studio 4.0.
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Scratch Programming Cards
UNIT - IV SORTING By B.Venkateswarlu Dept of CSE.
By Melissa Dalis Professor Susan Rodger Duke University June 2011
Sound and more Animations
Create a Halloween Computer Game in Scratch
MOM! Phineas and Ferb are … Aims:
Pixels, Colors and Shapes
User-Written Functions
Scratch for Interactivity
Repetition Structures
FLOWCHARTS Part 1.
PYGAME.
Loops BIS1523 – Lecture 10.
Background Shapes & Collision Resolution (Top-down and Side-scrolling)
Simple Sorting Algorithms
Let's Race! Typing on the Home Row
Introduction to Events
Engineering Innovation Center
Computation as an Expressive Medium
Project Snake. Project Snake Snake For this project, we’re going to make Snake This includes Making the overall game logic (using a 2D array) Handling.
Unit 7 – Excel Graphs.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Functions BIS1523 – Lecture 17.
Unreal Engine and C++ We’re finally at a point where we can start working in C++ with Unreal Engine To get everything set up for this properly, we’re going.
Fundamentals of Programming
Coding Concepts (Data Structures)
Go to =>
Topic 1: Problem Solving
Fundamentals of Data Structures
Unit 11 – PowerPoint Interaction
We’re moving on to more recap from other programming languages
Coding Concepts (Basics)
Introduction to TouchDevelop
Fundamentals of Data Representation
Game Loop Update & Draw.
Go to =>
Problem Solving Designing Algorithms.
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Data Structures & Algorithms
creating a ecosystems model in net logo
Creating a Simple Game in Scratch
Software Development Techniques
Lec 21 More Fun with Arrays: For Loops
Presentation transcript:

PC01 Term 3 Project Snake

For the project this term, we’ll be looking at Snake Snake is a short, skill-based game Involves ‘driving’ a snake around area Picking up ‘power-up’s Trying not to hit A wall The snake itself

Snake

We’ve not looked at GUI elements However, we can do this Snake We’ve not looked at GUI elements However, we can do this In the Console We will be ‘drawing’ the grid to the console every ‘frame’ Means WriteLining text to match the grid Then Clearing the console to draw another ‘frame’

The grid itself will be a 2D array Snake The grid itself will be a 2D array Each ‘space’ is a space in the grid Filled with Integers 0: nothing there -1: a power-up >0: the snake We can then use some loops and if-statements to draw something for each cell

Snake (Setup) Exercise Create a new C# project Give it a Main method At start of Main Create a 2D Integer array Give it 10 rows, and 10 columns Create a Random number generator

We’re going to use different colours for Console Colours We’re going to use different colours for The snake The power-ups This will make them stand out a bit So far we’ve not changed the colour of the console It is something we can do though

We start by making a ConsoleColor variable Console Colours We start by making a ConsoleColor variable Like we make a Random variable Then we give it an identifier The value is one of the possible values of ConsoleColor We use ConsoleColor.Red to get red

ConsoleColor snakeColour = ConsoleColor.Green; Console Colours ConsoleColor snakeColour = ConsoleColor.Green; This line stores the green colour in a variable This doesn’t do anything We’re simply storing the colour We’ll use it later on

Snake (Setup) Exercise After making the Random number generator Create two ConsoleColor variables for Your snake A power-up You can use any colour you want If you type in ConsoleColor. a list of possible colours will appear

Snake (Setup) We’re going to need four more variables The Snake’s head’s current row The Snake’s head’s current column The Snake’s head’s direction The Snake’s size These will be updated during gameplay The direction will tell the snake where its moving to The positions are for the 2D array (i.e. where the Snake is at the moment) Will work with direction to work out what row/column to move to The size is for how ‘big’ the Snake is Represents number of blocks in length for the Snake

Snake (Setup) Exercise After making the colour variables Create four Integer variables for Current Row = 4 Current Column = 4 Direction = 0 Size = 1

There are four main areas we need to work on Snake (Setup) There are four main areas we need to work on Game setup (putting everything in right place) Rendering (drawing grid onto console) Logic updates (moving Snake, collecting power-ups) Reading input (getting direction from user) We’ll start with setting up the game

Game Setup We need to place two things when setting up a game A power-up The Snake The Snake is done for us That’s the current row and column variables We need to assign All we have to do is place a power-up This could be random, or we can always add the first one to the top-left Let’s make it random

Game Setup We need to place two things when setting up a game A power-up The Snake The Snake is done for us That’s the current row and column variables We just need to assign the Size to the current row and column We then need to place a power-up This could be random, or we can always add the first one to the top-left Let’s make it random

grid[currentRow, currentCol] = size; Game Setup grid[currentRow, currentCol] = size; Here we’re taking the Snake’s size and putting it into the current cell of the grid The size will change any time the Snake picks up a power-up

Game Setup As a bit of preface, the grid will look like this in the background Any number greater than 0 is the Snake The smallest number is the tail The largest number is the head

Game Setup When the snake moves, we’re moving all those greater-than-0 numbers It gives the effect of ‘moving’ Us setting the size in the current row and column puts 3 (in this example) in the right place

Game Setup For placing a power-ups, we need to choose a random row and random column We then check to make sure that the Snake isn’t already there If not, we place the power-up We’ll be doing this a lot So, let’s make it a function

Put the size in the current row and current column cell Game Setup Exercise Put the size in the current row and current column cell See a previous slide for the exact code to use Create a static void function Called AddPowerUp Has two parameters The Random number generator The 2D array

Generate two random Integers: Game Setup Exercise Generate two random Integers: randRow (uses grid.GetLength(0) as the max) randCol (uses grid.GetLength(1) as the max) Check if this space in the grid is greater than 0 If so, continue choosing a random cell Repeat this until the condition is false Put -1 in the random cell of the grid

Rendering With the game grid set up, let’s look at rendering This is where we will output the ‘game’ to the console This involves two things Clearing the console Outputting characters for each space in the grid We’re going to do this a lot, so a function would work wonders here

Create a static void function Rendering Exercise Create a static void function Called RenderGame Has three parameters The game grid The Snake’s ConsoleColor The power-up’s ConsoleColor

The first thing we do is clear the console Rendering The first thing we do is clear the console There’s an easy way of doing this This removes everything from the console Even previous ‘renders’ Console.Clear();

Rendering Motion is the rapid display of still images We’re emulating that here to make it look like the Snake is moving smoothly We’ll clear the screen Then render the grid again We shouldn’t notice the clear However, the game can flicker That’s a consequence of using this method Nothing we can do about it really

With the console cleared we can draw the grid Rendering With the console cleared we can draw the grid We need to go through every cell in the grid This’ll need a loop and a nested loop The first loop goes over every row The nested loop goes over every column in every row We can then draw the grid

Then (using a loop and nested loop) Rendering Exercise Clear the console Using Console.Clear() Then (using a loop and nested loop) Go through every cell in the grid We’ll see what to do in these loops in a bit

Here’s how this will work Rendering Here’s how this will work The nested loop will draw each individual space The outer loop will repeat this for each row After the nested loop, we need to move on to the next line Inside the nested loop, we stay on the same line

Rendering We’ll output characters depending on the cell Nothing there (0)? Add a hyphen Power-up (-1)? Add a cross Snake (>0)? Add a circle We will also change the colours of the Console for each one Snake and power-up colours are being passed in Empty space colour should be White

Console.ForegroundColor = ConsoleColor.White; Rendering Can change text colour in console using this code Foreground is colour of text Also have a BackgroundColor Simply assign it any ConsoleColor From list Or from parameters Console.ForegroundColor = ConsoleColor.White;

In the nested loop, check the value in the current space of the grid Rendering Exercise In the nested loop, check the value in the current space of the grid If its 0 Change ForegoundColor to ConsoleColor.White Write a hyphen (-) to console (no new line) If its -1 Change ForegroundColor to power-up colour parameter Write a cross (X) to console (no new line) If its >0 Change ForegroundColor to Snake colour parameter Write a circle (O) to console (no new line) After the nested loop, write a new line to the console

Testing We have two of the main functions in place Let’s test them Near end of Main method, we’ll use these functions First AddPowerUp Then RenderGame We can see what the result looks like

Testing This is what we get when running this program

Finally, add a Console.ReadLine() Testing Exercise Near end of Main method Run AddPowerUp Pass in random number generator and grid Then run RenderGame Pass in grid, snake colour, and power-up colour Finally, add a Console.ReadLine() To stop console from closing Run the program and see what it looks like

Logic We have the base set up Now we need logic This will handle Setting up a game Rendering a game Now we need logic This will handle Getting input Moving snake Checking for ‘collisions’

Logic We should do this in a separate function We’ll make one Called Update Will have parameters for Random number generator Grid Position Direction Size static void Update(Random rng, int[,] grid, ref int currentRow, ref int currentCol, ref int direction, ref int size) { }

Logic However, position/direction/size need to be adjusted I.e. permanently changed Change must effect variables In Main method So, we use ref keyword Passes data in by reference I.e. we’re passing in the variable itself, not its data static void Update(Random rng, int[,] grid, ref int currentRow, ref int currentCol, ref int direction, ref int size) { }

Create a function Give it 6 parameters Called Update Logic Exercise Create a function Called Update Give it 6 parameters The random number generator The grid The current row (reference) The current column (reference) The direction (reference) The size (reference)

We can now get to work on a single ‘update’ Each update will include Logic We can now get to work on a single ‘update’ Each update will include Reading input from user Moving snake in that direction Looking for collisions That’s it

Logic (Getting Input) If we think about controls Move snake using arrow keys However, we’re using a console User input involves text We could get user to type in “Left” to go left etc. However, would feel clunky

ConsoleKey keyPressed = Console.ReadKey().Key; Logic (Getting Input) There is a way of getting the key pressed Using Console.ReadKey() This is what the code looks like ConsoleKey keyPressed = Console.ReadKey().Key;

ConsoleKey keyPressed = Console.ReadKey().Key; Logic (Getting Input) When we use this function Program locks waiting for user to press a key It reads what key they press Then stores in a variable We can use if/switch to work out what they pressed Match against ConsoleKey.LeftArrow etc. If keyPressed equals ConsoleKey.LeftArrow, then user wants to go left ConsoleKey keyPressed = Console.ReadKey().Key;

Logic (Getting Input) Exercise In Update Get a key press from the user Use this code to do so Afterwards, check if keyPressed equals (and do) ConsoleKey.UpArrow: set direction to 0 ConsoleKey.RightArrow: set direction to 1 ConsoleKey.DownArrow: set direction to 2 ConsoleKey.LeftArrow: set direction to 3 ConsoleKey keyPressed = Console.ReadKey().Key;

Logic (Getting Input) Exercise After changing direction, change Snake’s head’s position If direction is 0 (moving up) Take away from current row If direction is 1 (moving right) Add to current column Work out the rest for direction being 2 or 3 If row or column ever get < 0 or > grid length Wrap them around I.e. if gone off left side, make snake appear on right instead

We now need to move the Snake We have their next position Logic (Moving Snake) We now need to move the Snake We have their next position Current row and current column So we know where the Snake is going

The Snake’s body will be made up of integers Logic (Moving Snake) The Snake’s body will be made up of integers Greater than 0 If we subtract 1 from all the body parts The tip of the tail will disappear As it goes from 1 to 0 We can then put the size in the current position This gives the effect of movement

That means we need to do two things Logic (Moving Snake) That means we need to do two things Subtract 1 from all body parts of the snake Add the size to the current position We’ll set the size afterwards When we check for collisions For now, we need to loop through whole grid If a space is greater than 0 We subtract 1 from it

Logic (Moving Snake) Exercise After changing current row/column Loop through all the cells in the grid If the cell’s value is greater than 0 Subtract 1 from it

Logic (Moving Snake) We’ve moved the body of the Snake Now we need to move its head First, we need to check for collisions We need to check two things If the head’s new position is -1 The Snake picks up a power-up If the head’s new position is > 0 Run into Snake’s body

Logic (Moving Snake) Exercise After subtracting 1 from the Snake’s body Check if the Snake’s new position Is -1 Then after (in an else-if chain) check if the Snake’s new position Is greater than 0

Logic (Snake Collisions) Let’s deal with the -1 outcome Snake has picked up a power-up Two things should happen The size should increase by 1 A new power-up should be added to the grid We’ve already made a function for adding a power-up Called AddPowerUp We simply call it with the number generator and the grid

Logic (Snake Collision) Exercise If the cell is -1 Increase the size by 1 Run the AddPowerUp function, passing in The random number generator The grid

Logic (Snake Collisions) If the cell is greater than 0 The Snake has run into itself The game should end There’s a quick way of doing this Using Environment.Exit(0) Closes the command window for us However, we should output a lose message first Otherwise the window would close unexpectedly for the player

Logic (Snake Collision) Exercise If the cell is greater than 0 Output to the console “You run into yourself. You lose…” Then use Console.ReadLine() to pause the console Then use Environment.Exit(0) to close the program

We’ve dealt with the collisions Now we can move the Snake Logic (Moving Snake) We’ve dealt with the collisions Now we can move the Snake As simple as putting size into the current position in the grid

Logic (Moving Snake) Exercise After checking for collisions Put size into the current cell in the grid That’s using Current row Current column

Testing We need to run this Update function continuously As well as RenderGame Let’s add a while loop to the Main function After running AddPowerUp As that’s setting up the grid This while loop will do two things RenderGame Update That’s it

After running AddPowerUp in the Main function Testing Exercise After running AddPowerUp in the Main function Add an infinite while-loop Inside the while-loop Run RenderGame Then run Update

Testing You should be able to press the arrow keys To move the Snake The Snake should ‘wrap’ around the grid It should increase in size when picking up a power-up The game should end when the Snake collides with itself

Testing

Constant Movement Right now, we need to press a direction to move the Snake That’s not how the game should work The Snake should always move We just choose the direction

Constant Movement This requires only a simple change We only read a key from the console if a key is available There’s a property for this: Console.KeyAvailable Returns true if the user has a key pressed We can then read that key

Constant Movement Exercise In the Update() function Wrap the Console.ReadKey().key part (and the switch below it) in an if-statement The condition of this statement should be Console.KeyAvailable Run your program

Slower Movement If we run this program, the Update() function will run as fast as possible This leads to some interesting effects on the game Mostly it not even finishing drawing a single update before moving on to the next one

Slower Movement We need to slow this down considerably We’re going to slow down the main game loop That’s in the Main() function We’ll start by adding using System.Threading; to the top A library that includes the Thread class Then we’ll run Thread.Sleep() after updating This will ‘pause’ the game for a bit

Slower Movement Exercise Add using System.Threading; to the top of your C# file In the Main() function, above the game’s while loop, create an integer variable millisecondsPerUpdate Assign it a value of 500 Inside the Main() function, use Thread.Sleep(millisecondsPerUpdate); Run the game It should run slower now If you’re getting any input ‘lag’ (the change in direction doesn’t take affect until the next update) Move Update() before RenderGame()

Extra Content You now have a fully-functioning game Try these optional bits, if you have time Stop the Snake moving ‘back’ on itself If the Snake is moving left, it shouldn’t be able to move right Display the score (size) of the Snake underneath the grid Decrease the millisecondsPerUpdate whenever the Snake picks up a power-up This speeds up the Snake over time