Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "PC01 Term 3 Project Snake. PC01 Term 3 Project Snake."— Presentation transcript:

1

2 PC01 Term 3 Project Snake

3 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

4 Snake

5 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’

6 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

7 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

8 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

9 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

10 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

11 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

12 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

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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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

22 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

23 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

24 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

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

26 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

27 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

28 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

29 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

30 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

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

32 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

33 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

34 Testing This is what we get when running this program

35 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

36 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’

37 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) { }

38 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) { }

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

40 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

41 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

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

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

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

45 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

46 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

47 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

48 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

49 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

50 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

51 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

52 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

53 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

54 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

55 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

56 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

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

58 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

59 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

60 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

61 Testing

62 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

63 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

64 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

65 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

66 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

67 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()

68 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


Download ppt "PC01 Term 3 Project Snake. PC01 Term 3 Project Snake."

Similar presentations


Ads by Google