Tile-based game Part 2: Collisions and Tilemap

KidsCanCode · Beginner ·🌐 Frontend Engineering ·10y ago

Key Takeaways

The video demonstrates how to create a tile-based game with collisions and a tilemap, using practical coding examples to make designing levels easier.

Full Transcript

welcome back to game development with pame this is part two of our tile based game project and in this video we'll be talking about uh wall collisions and tile maps in our last video we created a simple grid of tiles with a Sprite that we could move around on that grid with by pressing the arrow keys in the four directions and then we created a wall which is just a object that occupies one square so I made a line of squares here that that's going to represent the wall and now what we need to do is we make need to make it so that our Sprite cannot move through those wall tiles we're going to do that by going over to our Sprite and we're going to make it so that right here when we move this tells us what Square we're going to move into well we're only going to allow the Sprite to move into that square if it's empty so so we need to check to see if something is in this square at dxdy that we want to move into so we're going to define a command called collide with walls and that's going to we're going to pass it the DX and the Dy of where we want to check and I'm going to make give those default values of zero so that we can check any direction we want and if we we want we can only pass it one one of those values and it'll use the the zero for the other value so this just means we need to do a little Loop and we need to go through uh each of the walls in The Walls Group okay for each of those walls we're going to check and see if the walls X and Y match the x of the and Y of the player Plus the move that we want to make so if the space we're going to move into is equal to the walls X and the walls y right if both of those things are true then we must have tried to move into a square that has a wall in it so we're not going to allow that square we're going to return true and say we did Collide and then if not then we will return false so collide with walls is going to tell us true if we tried to move into a square that where a wall is false if we did not so here this move we're not going to allow the player to CH to change their X and Y unless Collide walls is false so we're going to say if not collide with walls and just use the DX and Dy that we're using and we'll indent these and that should do it so we'll go over to our main and we will run it and now I am no longer able to move through this wall from any direction I'm holding the arrow key down just not letting me move into that space so the the move command doesn't even happen because it sees that that space is occupied okay so now we have walls that work and we can spawn walls at any location we want um and and they'll all work as long as you know we are in The Walls Group which all the walls are are it's going to not allow us to move into spaces that are occupied by these objects but what if we want to lay out a level a maze or a dungeon or some other kind of world for us to walk around in adding all these little squares and figuring out the coordinates of each Square um is going to be really tedious and cover some and so the solution for that is to use something called a map file and there's a lot of different ways to do this uh to begin with we're going to use kind of the simplest version possible uh for doing this which is just a little text file that's going to be a map of our level and then our game is going to load that map and create walls wherever the map says wall should be okay so if we remember from last time this is our width and height and our tile size so we know that we have 32 tiles across and 24 tiles down so if I make a new file and I'm going to save this and I'm going to call this I'm going to call this file map. txt this is not going to be a Python program this is just going to be a text file and and I'm going to up the font a little bit so I know I need this to be 32 across so I'm going to hold down the dot until I see myself get to column number 32 okay so now I know that's 32 dots across and then I'm going to duplicate that line until I have 24 of them there we go let's make my window a little bit taller so we can see them all one more I'm pressing command shift and D on a on Windows you would press control shift d that duplicates a line so now I have a grid here of 32 by 24 uh little dots I just used a period these are going to represent the squares in our grid so what I could do is anywhere I wanted there to be a wall like for example let's say I wanted a wall all the way across the top right I'm going to type ones here anywhere that I'm going to type ones anywhere I want there to be a wall and then I'll do the same thing I will just copy that and make that the last line too so now I have I'll have walls along the top and bottom of my screen and we could even add walls along the side if we just put a one there and I will delete all these blank ones and we will just duplicate that instead okay so this is going to be my map right now I just have oh let's throw a few in the middle just so we have something okay so here I've got some walls going all the way around the edges of my screen and then a little line of walls in the middle so this is my map file so now I want my program to load this file look through it and anywhere any box where it finds a one it's going to generate a wall at that X and Y so we're going to do that in our load data here and we need to import the path command to handle all of our file location stuff depending on whether we're on Windows or Os 10 or whatever and make sure we can locate where this file is when we run our program so we're going to put that our game folder is path. durame file that's the location where our game this main.py where our game is running from so we know what our folder is so now we can open up the map. text and we talked about opening files and reading from them uh back in our schmu game project when we were talking about saving and loading the high score so I'll link to that below if you want a refresher of how the open command works and how you get it to read from files so we're just going to say path. jooin game folder map. dxt and we need to say that we're doing that in a read we're opening that file to read okay and all we're going to do is we're just going to say for each line in F we want to stick that in a list so before our Loop let's make a variable called map data and it's just going to be a list an empty list and for each line we're just going to say map data. append the line so at the end of this loop I will have read in line by line each line of this file one after another and each line will just get appended to that list so we'll have a list of 24 lines and each line will be a string containing 32 characters and we have our map data in our program now so now we need to read or or sorry we need to Loop through that map data and any location we find A1 we're going to spawn a wall and I suppose technically we should do that in our new right where we spawn walls so let's take this map data variable and let's rename or let's call that self. map data so it's one of our games properties and down here in New we're going to do the wall spawning so I'm going to erase this because we don't want to do it this man manual way anymore and now we're going to Loop through our map data list so I'm going to say for row so each of the items in the list is a row row comma tiles in enumerate map data and so real quick let's take a step to the side and talk about what enumerate does so if we go over here and open a python shell we've worked with lists plenty of times right all the time we work with lists and let's just make a quick list here that has four items in it a b c and d and we know those will be indexed 0 1 2 and three right and we know that we can refer to index number zero is the a we know that for item in L you can say print item and that just Loops through the list and looks at each item at a time and that's all stuff you've seen before but sometimes in your loop as you go through you want the item and you want its index number and so that's what enumerate is for so if I say for item or sorry for index item in a new numerate l so what this is going to do is each time through the list it's going to give me both variables it's going to give me the index this first one will be the index of the item and this will be the value so I will get zero and a and then here I'll will get one and B so I'll just print them out real quick so you can see print the index print the item and you see I get zero and a zero and b or sorry one and B and so on and that's what we want to do here because each row in this map file this is row this will be row zero so that's the that's going to be the Y value of all of these is because they're going to be on row zero these are going to be on Row one or y equals 1 and so on so I want all this data but I also want to know what index it is so I know what row that is so row is going to be equal to the index value and tiles is going to be equal to that string of all the characters well now we need to do the same thing through that string and we're going to say enumerate tiles so what we're going to do is we're going to do an enumerate on this string and so index number zero will be a one one will be a one but that index number will be the x value of the tile so when I get to this tile I'll have a row of zero and a column of 14 and so this will be the X and the Y of where to put that wall so now I'll have the value and I'll have the row and the column so all I need to do now is just see if the tile is equal to A1 right if I found a one and not a dot in that location then I can spawn a wall and the wall is going to be at the correct column and the correct row okay and that is all except I left out the word in here that red dot telling me I have an error message and let's run our program and see what happens so there we go there are my walls and you can see I can't go through them but it looks like I was one off on the size yep it turns out I went back and I counted I had counted 31 uh dots across not 32 so I was off by one so I've added one more column here on each row and now I have the right number of tiles and you see I'm spawning a wall everywhere I wanted there to be a wall now you can see the easy thing now to change things is if I want to go in here and make a change because I want a wall I just have to change the map file and I run my program again it reads that mop file now I have a a wall where I wanted there to be a wall and here I've pasted in a map I made before that's just a little bit fancier it looks a little nicer that I'm going to use for my kind of default map here that's got some walls that you got to walk around okay and so that's a good start one problem we have now though is when we run this my player spawns right here right well what if there was a wall there we don't want our player to spawn inside a wall so that means we can add to our map a player spawn location so I can say like for example if I want my player to spawn in the lower left I can add a p and now in my code I can just say when I find instead of spawning the player at 1010 I'm going to say if the tile was a p then I'm going to paste in that player spawning command and I'm just going to change this to use the column and the row where we just found the player and there we go now the player spawns at the spot I want him to spawn in so that'll do it for this video uh we've gone a little bit long this time around um go ahead and play around with the map create your create your own uh lay it out the way you like it and in the next video we'll talk about um some other ways we could make our Player move more smoothly around the map and also what kind of game this could turn into as always please press the like button and subscribe for the next video thanks for watching

Original Description

In this video we'll make our walls solid, and learn how to make designing our level a lot easier by creating a tilemap. In each video in this series, we'll add another feature to our game until we have a full game experience with graphics, animations, sound, and much more! Code for this part: https://github.com/kidscancode/pygame_tutorials/tree/master/tilemap/part%2002 If you like these videos please consider supporting me on Patreon: https://www.patreon.com/kidscancode Other helpful links: * Installing Python: http://kidscancode.org/python-install.html * Installing Pygame: http://kidscancode.org/blog/2015/09/pygame_install/ * Setting up Atom: https://www.youtube.com/watch?v=uve1tjVIQ6c&ab_channel=KidsCanCode
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from KidsCanCode · KidsCanCode · 57 of 60

1 Learning to Code with Python: Lesson 1.1 - What is Programming?
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
2 Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
3 Learning to Code with Python: Lesson 1.3 - Variables
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
4 Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
5 Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
6 Learning to Code with Python: Lesson 1.6 - Functions
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
7 Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
8 Learning to Code with Python: Lesson 1.8 - Number Guessing Game
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
9 KidsCanCode - Patreon Intro Video
KidsCanCode - Patreon Intro Video
KidsCanCode
10 Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
11 Learning to Code with Python: Lesson 1.10 - Secret Codes
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
12 Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
13 Learning to Code with Python: Lesson 2.2 Simple Animation
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
14 Learning to Code with Python: Lesson 2.3: Animating More Objects
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
15 Learning to Code with Python: Lesson 2.4: More Fun with Animation
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
16 Extra: Setting up the Atom Editor for Python
Extra: Setting up the Atom Editor for Python
KidsCanCode
17 Game Development 1-1: Getting Started with Pygame
Game Development 1-1: Getting Started with Pygame
KidsCanCode
18 Game Development 1-2: Working with Sprites
Game Development 1-2: Working with Sprites
KidsCanCode
19 Game Development 1-3: More About Sprites
Game Development 1-3: More About Sprites
KidsCanCode
20 Pygame Shmup Part 1: Player Sprite and Controls
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
21 Pygame Shmup Part 2: Enemy Sprites
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
22 Pygame Shmup Part 3: Collisions (and Bullets!)
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
23 Pygame Shmup Part 4: Adding Graphics
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
24 Pygame Shmup Part 5: Improved Collisions
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
25 Pygame Shmup Part 6: Sprite Animation
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
26 Pygame Shmup Part 7: Score (and Drawing Text)
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
27 Pygame Shmup Part 8: Sound and Music
Pygame Shmup Part 8: Sound and Music
KidsCanCode
28 Pygame Shmup Part 9: Shields
Pygame Shmup Part 9: Shields
KidsCanCode
29 Pygame Shmup Part 10: Explosions
Pygame Shmup Part 10: Explosions
KidsCanCode
30 Pygame Shmup Part 11: Player Lives
Pygame Shmup Part 11: Player Lives
KidsCanCode
31 Pygame Shmup Part 12: Powerups
Pygame Shmup Part 12: Powerups
KidsCanCode
32 Pygame Shmup Part 13: Powerups (part 2)
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
33 Pygame Shmup Part 14: Game Over Screen
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
34 Pygame Platformer Part 1: Setting Up
Pygame Platformer Part 1: Setting Up
KidsCanCode
35 Pygame Platformer Part 2: Player Movement
Pygame Platformer Part 2: Player Movement
KidsCanCode
36 Pygame Platformer Part 3: Gravity and Platforms
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
37 Pygame Platformer Part 4: Jumping
Pygame Platformer Part 4: Jumping
KidsCanCode
38 Pygame Platformer Part 5: Scrolling the Window
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
39 Pygame Platformer Part 6: Game Over
Pygame Platformer Part 6: Game Over
KidsCanCode
40 Pygame Platformer Part 7: Splash & End Screens
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
41 Pygame Platformer Part 8: Saving High Score
Pygame Platformer Part 8: Saving High Score
KidsCanCode
42 Pygame Platformer Part 9: Using Spritesheets
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
43 Pygame Platformer Part 10: Character Animation (part 1)
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
44 Pygame Platformer Part 11: Character Animation (part 2)
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
45 Pygame Platformer Part 12: Platform Graphics
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
46 Pygame Platformer Part 13: Improved Jumping
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
47 Pygame Platformer Part 14: Sound and Music
Pygame Platformer Part 14: Sound and Music
KidsCanCode
48 Pygame Platformer Part 15: Powerups
Pygame Platformer Part 15: Powerups
KidsCanCode
49 Pygame Platformer Part 16: Enemies
Pygame Platformer Part 16: Enemies
KidsCanCode
50 Pygame Platformer Part 17: Using Collision Masks
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
51 Pygame Platformer Part 18: Scrolling Background
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
52 Pygame Platformer Part 19: Wrapping Up
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
53 Gamedev In-depth Topics: 4-way vs. 8-way Movement
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
54 Gamedev In-depth Topics: Time-based vs. Frame-based Movement
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
55 Gamedev In-depth Topics: Non-integer Movement
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
56 Tile-based game Part 1: Setting up
Tile-based game Part 1: Setting up
KidsCanCode
Tile-based game Part 2: Collisions and Tilemap
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
58 Tile-based game Part 3: Smooth Movement
Tile-based game Part 3: Smooth Movement
KidsCanCode
59 Tile-based game Part 4: Scrolling Map / Camera
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
60 Tile-based game Part 5: Player Graphics
Tile-based game Part 5: Player Graphics
KidsCanCode

This video teaches how to create a tile-based game with collisions and a tilemap, making level design easier. It covers practical coding examples and techniques for game development.

Key Takeaways
  1. Create a new game project
  2. Set up a tilemap
  3. Implement collisions
  4. Design a level with the tilemap
💡 Using a tilemap can make designing levels for a tile-based game much easier and more efficient.

Related Reads

📰
HTML, CSS, and JavaScript Roadmap: Where Every Developer Actually Starts
Learn the fundamentals of HTML, CSS, and JavaScript before moving to frameworks like React to build a strong foundation for a career in web development
Medium · JavaScript
📰
Your React App Works. Until the API Doesn’t.
Learn to handle API failures in React apps to ensure a seamless user experience
Medium · JavaScript
📰
We Hit the Browser's Scroll Limit at 550K Rows. Here's How We Reached 1 Million.
Learn how to overcome browser scroll limits when rendering large datasets in React, a crucial skill for frontend engineers
Dev.to · JANG KIYOUNG
📰
I accidentally reinvented CSS progress() and then I published it anyway.
Learn how a developer accidentally recreated CSS progress() and what you can learn from their experience in CSS framework development
Dev.to · Petros Papatheodorou
Up next
Adapting to dynamic content using modern CSS
Kevin Powell
Watch →