Game Programming - Episode 43 - Player Input

The Cherno · Intermediate ·🧒 Coding for Kids ·13y ago

Key Takeaways

The video demonstrates game programming concepts, specifically player input, using Java, and covers the creation of a player class, handling keyboard input, and updating entity movement.

Full Transcript

guys my name is aeno welcome back to episode 43 of game programming so we are back finally after that break that I took for Christmas New Year's whatever and first of all I want to wish you guys a Happy New Year I hope you guys um have some big plans for 2013 I know I do and uh hopefully you know it's going to be the best year of my life basically and you know I wish the same onto you all as well um now this show is going to resume being daily now so um so yeah that's something to look forward to I guess now today is actually an interesting day um it's 41° right now in Melbourne let's just say that okay and if you guys are from America or whatever you'll find that it's actually 106° F so I mean it's it's kind of hot right now so if if I you know I didn't sleep that well either so if I get something wrong if I take a little longer explaining something just you know go easy on me and um and yeah but either way guys we' got some exciting stuff coming up in this series I was actually thinking about all the stuff that we actually need to do um going on in this year and I'm hopefully you know going to be wrapping up the Series this year as well um so yeah first of all though um if we launch our game that's always what I like doing first so we can see where we're up to currently you can see that we've got this uh this nice thing and we actually talked about making a player Sprite and we did make a player uh sorry not a player Sprite a player um class and we ended up making a player class and you can see that all of this stuff if we actually like um this update method I'm fairly sure is actually being used if I go into references worksspace uh oh it's not being used huh oh well it's probably being overridden and then used I think it should be used if we go into game all right all right cool so um you can see that right now what we've got going on here is um these controls they're actually being done in the update method now that's incorrect because what it's doing is something that it shouldn't be doing we've got two integers over here and what the C what what is what is going on here is it's adjusting these integers right based on um what we hit I guess a better way to explain this would actually be if I go down into this render method right here I'm just going to pull up um right under right after the images drawn G do draw string and in the string all I'm going to draw is like the X um and then know plus X and then plus uh also the Y so in other words I'm just going to draw I'm just going to draw those two inte integers on the screen um I'll also set the color to white and I'll you guys don't really need to take this down I'm just trying to prove a point and I'll set the font to like I don't know vidana and we'll give it like oops like a 50 size or something um whoops what am I doing all right 50 size and this obviously will pop um probably around size of our screen okay about 900 um probably pop it it around 450 by 400 or something all right import font so if I okay that's not really the middle but anyway uh you can see that as I move the as I move the the keyboard I'm not really moving the keyboard as I as I hit the different keys to move our player or you know what seems like our player you can see that what's happening is it's basically keeping track of where the player is in that sense right when we start we're at 0 0 and then as we move it's going to increase and decrease this variable X variable if we're moving horizontally if we move vertically it's going to increase and decrease the Y variable so that's kind of what's going on right now so we just get rid of all this stuff um I'll keep this here though just in case I want to draw some other font things all right so that's what's going on right now now that's not technically correct okay because all it is doing if we actually check out this x variable is being plugged into this level. random method and what it's actually doing is it's offsetting the the level based on the scroll that is technically correct right but it's it's not really like it's working as you guys can see but it's we're going to run into tons of problems including the fact that you know when we have multiple players on the streen on the screen where we've got multiple mobs to keep track of this is a very bad way of doing it so what I'm going to do right now is just delete that that's gone and we'll also delete these X and Y variables now this stuff goes away and this stuff sort of ends up throwing an error that's all right um we'll deal with that in a minute first of all we need to actually create the player so I'm going to go private player player and what we've done here is we've created a player object all right we've officially created a player object in the game class let's import the player um one thing we need to do is instantiated obviously currently it's equal it's equal to null that won't work um I'm sure you guys pretty comfortable with object oriented programming we'll create it just before we create the level actually we'll probably create it after we create the level so player equals new player and the only parameters that we we don't actually have like we've got this spawn parameter here which we won't deal with now but what we don't have in the player class is the actual um an input sort of parameter right sorry I'm just adjusting my microphone so if you can hear that I am sorry um we're not actually in Ting the input right what we need to do over here is we need to give the player a control method so I'm going to try I'm going to type input Handler input right and I actually didn't call it input handle I called it keyboard so we'll call it keyboard input right and and this input and the keyboard is basically going to be an input method right here now we'll also use the mouse in the future but for now it's just keyboard um so let's import the keyboard and now we need to also create a private variable that's called keyboard again we'll call it input and we'll just say that when the class does get created let's make input equal to ke uh this. input equals input right and what's that what that's going to do is set this parameter equal to this um and or rather set that equal to the parameter and we'll also do the same thing for over here right so we'll also put the keyboard input into here as well so we've got um so that um no matter how the players created it's always going to have an input method so and this input will actually be used over here to adjust the X and Y variables which we'll deal with in a minute but over here we just need to type in I think it's called K and now you'll find that we've we've created we've created a new player and we've got the key parameter in there so that so that we get the um the keyboard basically so that we can use the keyboard in this player class um so now we've done that let's actually change the inputs of the player so in other words when we press the up Arrow when we press the down arrow left right whatever when we move our player when we press the keys that we want to move our player let's actually move our player if we go all the way down to entity right so in other words if we go to extends mob here and then we click on entity control click on entity um we'll get we'll get into entity class right and we have two variables here X and Y and the X and Y variables are the variables that actually keep track of where our mob or entity any entity actually of where our entity is located on the map so um you can probably tell that if we want to you know if if basically if input dot you know up is pressed if input. up is pressed right we sort of want to we want to subtract from y right to make it to make it sort of go up um if input um do down right we want to add to Y and exact same thing for X so left and right we want to do exactly the same thing except for X so what we're doing here is if we if we if we hold the left button the left uh Arrow key or or the a button we're going to subract subtract uh from X so we basically Rewritten all that we did in this update method but we done we've done it in the player method and then what's what it's actually doing is instead of affecting the int X and in y variables that we just created in game it's actually going to affect if we control click on this it's actually going to affect the entities X and Y um coordinates it's it's actual location so that in in other words in multiplayer we'll be able to keep track of all those locations and uh and it'll work out great um so yeah that's what we've done right now we've added all these things into the update method now few things that we actually need to do first of all is run this update method because if this method is in being ran then it's not going to be able to nothing's going to happen as we press the keys it's not going to do anything so we actually need to go player. update right now few things like straight away you can see that the mob has an update method and the entity has an uh entity has an update method the reason for now we're not going to deal with that all right player I like to sort of update the player by itself just in case anything else goes wrong the play is still able to update itself so for now we are going to do that that way but otherwise you know we don't even have like an entity system in our level class yet so for now player. update is going to work we might fix that in the future depending on if I feel like it or not um and the last thing that we'll do is probably arguably one of the most important to make this work again is this X and Y we're going to change it to player dox and player doy all right just like that and if we launch our game you'll find that look at that our game is working perfectly but what it's actually doing here is instead of accessing um this microphone have to get a new microphone stand this is ridiculous hang on oh I'm tightening it all right it's better no it's still falling I'm going to order a new microphone stand this is getting really annoying maybe like one of those desk clip one anyway um so j. draw string um again you know this is like the X now plus X oh sorry not plus X Plus player dox now right um plus y plus player doy and then we'll draw draw it at like 350 300 cuz 450 400 not the mail um and if we draw this right now then you'll see that what we've got is um it's updating like it was before but now player is actually handling its own movement player and MOB and entity whatever ever is handling its own movement now this is one way to do it and tomorrow we're going to cover the other way to do it all right I sort of want to take this take this one step at a time um so you guys understand it but that that is one way to do it and tomorrow we're going to take a look at another way to do it so I hope you guys enjoyed this episode of game programming it was quite a long one and uh yeah hit the like button if you did and I'll see you guys next time goodbye [Music]

Original Description

Welcome to Game Programming, a series in which we take an in depth look at how to make a game from scratch, in Java. ---------------------------------------------------------------------------------------------------------------- Website: http://www.thecherno.com Twitter: http://www.twitter.com/thecherno Facebook: http://www.facebook.com/thecherno Subreddit: http://www.reddit.com/r/thecherno/ Steam Group: http://www.steamcommunity.com/groups/thecherno ---------------------------------------------------------------------------------------------------------------- Outro music is by Approaching Nirvana: http://www.youtube.com/approachingnirvana
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from The Cherno · The Cherno · 0 of 60

← Previous Next →
1 3D Game Programming - Episode 1 - Window
3D Game Programming - Episode 1 - Window
The Cherno
2 3D Game Programming - Episode 2 - Game Loop
3D Game Programming - Episode 2 - Game Loop
The Cherno
3 3D Game Programming - Episode 3 - Arrays
3D Game Programming - Episode 3 - Arrays
The Cherno
4 3D Game Programming - Episode 4 - Drawing Pixels!
3D Game Programming - Episode 4 - Drawing Pixels!
The Cherno
5 3D Game Programming - Episode 4.5 - How Rendering Works
3D Game Programming - Episode 4.5 - How Rendering Works
The Cherno
6 3D Game Programming - Episode 5 - Playing with Pixels!
3D Game Programming - Episode 5 - Playing with Pixels!
The Cherno
7 3D Game Programming - Episode 6 - Performance Boosting
3D Game Programming - Episode 6 - Performance Boosting
The Cherno
8 3D Game Programming - Episode 7 - FPS Counter
3D Game Programming - Episode 7 - FPS Counter
The Cherno
9 3D Game Programming - Episode 8 - Alpha Support and More
3D Game Programming - Episode 8 - Alpha Support and More
The Cherno
10 3D Game Programming - Episode 9 - Beginning 3D
3D Game Programming - Episode 9 - Beginning 3D
The Cherno
11 3D Game Programming - Episode 10 - Floors and Animation
3D Game Programming - Episode 10 - Floors and Animation
The Cherno
12 3D Game Programming - Episode 11 - Rotation
3D Game Programming - Episode 11 - Rotation
The Cherno
13 3D Game Programming - Episode 12 - User Input
3D Game Programming - Episode 12 - User Input
The Cherno
14 3D Game Programming - Episode 13 - Render Distance Limiter!
3D Game Programming - Episode 13 - Render Distance Limiter!
The Cherno
15 3D Game Programming - Episode 14 - Basic Mouse Movement
3D Game Programming - Episode 14 - Basic Mouse Movement
The Cherno
16 3D Game Programming - Episode 15 - Textures + More!
3D Game Programming - Episode 15 - Textures + More!
The Cherno
17 3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
3D Game Programming - Episode 16 - Walking, Crouching, Sprinting + More
The Cherno
18 3D Game Programming - Episode 16.5 - Exporting Runnable Jars
3D Game Programming - Episode 16.5 - Exporting Runnable Jars
The Cherno
19 3D Game Programming - Episode 17 - Small Adjustments + Birthday!
3D Game Programming - Episode 17 - Small Adjustments + Birthday!
The Cherno
20 3D Game Programming - Episode 17.5 - Creating an Applet
3D Game Programming - Episode 17.5 - Creating an Applet
The Cherno
21 3D Game Programming - Episode 18 - The Beginning of Walls
3D Game Programming - Episode 18 - The Beginning of Walls
The Cherno
22 3D Game Programming - Episode 18.1 - A Few More Things
3D Game Programming - Episode 18.1 - A Few More Things
The Cherno
23 Episode 18.5 - Creating an EXE File in Java
Episode 18.5 - Creating an EXE File in Java
The Cherno
24 3D Game Programming - Episode 19 - Rendering Walls
3D Game Programming - Episode 19 - Rendering Walls
The Cherno
25 3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
3D Game Programming - Episode 20 - Continuing Walls, Fixing Bugs, and Managing Crashes
The Cherno
26 3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
3D Game Programming - Episode 21 - Texturing Walls, Fixing Clipping, and Fixing the Mouse
The Cherno
27 3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
3D Game Programming - Episode 22 - Random Level Generator + Properly Fixing Clipping
The Cherno
28 3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
3D Game Programming - Episode 23 - Graphical User Interface (GUI) Launcher
The Cherno
29 3D Game Programming - Episode 24 - Making Our Launcher Work
3D Game Programming - Episode 24 - Making Our Launcher Work
The Cherno
30 3D Game Programming - Episode 25 - Writing and Reading Files
3D Game Programming - Episode 25 - Writing and Reading Files
The Cherno
31 3D Game Programming - Episode 26 - Custom Resolutions
3D Game Programming - Episode 26 - Custom Resolutions
The Cherno
32 3D Game Programming - Episode 27 - Decorating the Launcher
3D Game Programming - Episode 27 - Decorating the Launcher
The Cherno
33 3D Game Programming - Episode 28 - Continuing our Custom Launcher!
3D Game Programming - Episode 28 - Continuing our Custom Launcher!
The Cherno
34 3D Game Programming - Episode 29 - Launching The Game
3D Game Programming - Episode 29 - Launching The Game
The Cherno
35 3D Game Programming - Episode 30 - Colour Processing In-Depth
3D Game Programming - Episode 30 - Colour Processing In-Depth
The Cherno
36 3D Game Programming - Episode 31 - Sprites!
3D Game Programming - Episode 31 - Sprites!
The Cherno
37 3D Game Programming - Episode 32 - Sprite Mapping
3D Game Programming - Episode 32 - Sprite Mapping
The Cherno
38 3D Game Programming - Episode 33 - High Resolution Rendering
3D Game Programming - Episode 33 - High Resolution Rendering
The Cherno
39 3D Game Programming - Episode 34 - Entities
3D Game Programming - Episode 34 - Entities
The Cherno
40 Genesis - My Game for Ludum Dare 24
Genesis - My Game for Ludum Dare 24
The Cherno
41 Vlog + Ludum Dare Results
Vlog + Ludum Dare Results
The Cherno
42 Game Programming - Episode 1 - Resolution
Game Programming - Episode 1 - Resolution
The Cherno
43 Game Programming - Episode 2 - Threads
Game Programming - Episode 2 - Threads
The Cherno
44 Game Programming - Episode 3 - Game Loop
Game Programming - Episode 3 - Game Loop
The Cherno
45 Game Programming - Episode 4 - Window
Game Programming - Episode 4 - Window
The Cherno
46 Episode 5 - Buffer Strategy
Episode 5 - Buffer Strategy
The Cherno
47 Game Programming - Episode 6 - Graphics Initialized
Game Programming - Episode 6 - Graphics Initialized
The Cherno
48 Game Programming - Episode 7 - Buffered Image and Rasters
Game Programming - Episode 7 - Buffered Image and Rasters
The Cherno
49 Game Programming - Episode 8 - The Screen Class
Game Programming - Episode 8 - The Screen Class
The Cherno
50 Game Programming - Episode 9 - Rendering Pixels
Game Programming - Episode 9 - Rendering Pixels
The Cherno
51 Game Programming - Episode 10 - Clearing the Screen
Game Programming - Episode 10 - Clearing the Screen
The Cherno
52 Game Programming - Episode 11 - "Out of Bounds, Baby!"
Game Programming - Episode 11 - "Out of Bounds, Baby!"
The Cherno
53 Game Programming - Episode 12 - Negative Bounds
Game Programming - Episode 12 - Negative Bounds
The Cherno
54 Game Programming - Episode 13 - Timer
Game Programming - Episode 13 - Timer
The Cherno
55 Game Programming - Episode 14 - FPS Counter
Game Programming - Episode 14 - FPS Counter
The Cherno
56 Episode 15 - Tiles
Episode 15 - Tiles
The Cherno
57 Game Programming - Episode 16 - The Map
Game Programming - Episode 16 - The Map
The Cherno
58 The Walls 2 - Minecraft PvP Survival Map
The Walls 2 - Minecraft PvP Survival Map
The Cherno
59 Game Programming - Episode 17 - Key Input
Game Programming - Episode 17 - Key Input
The Cherno
60 Game Programming - Episode 18 - Controlling The Map
Game Programming - Episode 18 - Controlling The Map
The Cherno

This video teaches game programming concepts, specifically player input, using Java, and covers the creation of a player class, handling keyboard input, and updating entity movement. The video provides a comprehensive overview of game programming and is suitable for intermediate learners. By following the steps outlined in the video, learners can create their own game with player input functionality.

Key Takeaways
  1. Make a player class
  2. Use update method to adjust integers for controls
  3. Draw player position on screen using render method
  4. Use font to display player position on screen
  5. Track player position using X and Y variables
  6. Deleted X and Y variables
  7. Created a player object in the game class
  8. Instantiated the player object
  9. Added a keyboard input method to the player class
  10. Used the keyboard input method to adjust the X and Y variables
💡 The video highlights the importance of separating game logic into different classes, such as the player class and entity class, to handle player input and entity movement effectively.

Related Reads

📰
How Creative Characters Like Sprunki Inspire Kids to Learn Coding
Learn how creative characters like Sprunki inspire kids to learn coding through interactive games and animations
Medium · Programming
📰
Computer Applications for Primary School Children: A Fun + Safe Guide for Ages 6-12
Learn how to introduce primary school children to computer applications in a fun and safe way, teaching them essential skills for the future.
Dev.to · Ogunkola Adeola
📰
From 0 to 20 Chapters: My Story‑Driven Rust Book for Kids Now Has a Bilingual Interactive Demo
Create an interactive coding book for kids using Rust, with a focus on storytelling and bilingual support, to teach programming concepts in an engaging way
Dev.to · born1987-ir
📰
How Kids Can Build Fighting Games Stickman in Scratch
Kids can learn coding by building a stickman fighting game in Scratch, developing skills like movement control and collision detection.
Medium · Programming
Up next
Man Builds a Backyard Tiny House and Turns It into a Home Office | Start to Finish by @Elseweyr
AKLA GELEN
Watch →