Terraria-like World Generation - Pygame Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'll be covering the implementation of a terraria-like world generation using one-dimensional purlin noise if you're unfamiliar with pro and noise it's what generates these noise patterns you've probably seen this type of image somewhere before for our purposes just imagine it as a function that you pass in coordinates and it gives you a height value this image i'm showing right here is two dimensional noise and in our case we'll be doing one dimensional noise so what you can imagine here is that we're just taking all of the points along only one axis in our case just imagine as the x-axis so we're just taking one line straight through this image and then the white parts will be the high points in the train and the black point of parts will be the low points in the terrain you can also layer this effect to get even more complex features the library i'll be using actually has stuff built in for doing this but we'll be using a very simple system this time i'll be building this into my platformer that i made in my tutorial series i'm going specifically off of the code from episode 7. if you haven't seen that yet i'd recommend you to watch it but what you need to know for the purposes of this video is that i do have a system where it generates just flat terrain as needed i also have a built-in system for rendering that optimizes things so i can have infinite worlds anyways the library i'll be using here is not actually built into python you do need to install it which is why i've got a terminal open right now to install it you'll need to do pip 3 install noise and then you'll have to wait for it to install if that command doesn't work for you you might be using an old version of python where pip isn't included with python or you may have just had the pit executable not linked properly in which case you'll have to go hunting for it and then run it manually you can look up how to use pip on your own it's not too hard to figure out anyways now i've got this noise library installed let me show you what the game was as of the end of the tutorial that the code i'm working with is based on so i've just got a platformer here what can run around basically and jump that's pretty much it there's also a little bit of a parallax background but this train's infinite and it's generated as needed once it appears on the screen and it'll call a command and try to generate some terrain so all i'm doing here is replacing that code to add just height levels so that i can have terraria electron using noise so let's take a look at the code the first thing i'm going to need to do here since i'm using the noise library is import the noise library so there is a lot in here but for the purposes of this video we only need one section which is this generate chunk function which is called whenever a new section of terrain needs to be generated and conveniently i have these target x and target y variables which indicate the x and y positions of the terrain i'm generating so to use the noise library and i'll be using one dimensional noise here i put in an x value and it'll give me a value from zero to one for that location and it's smooth so let's say height equals noise dot p noise one which is the function in that library that does one-dimensional like there's also p noise two which is two-dimensional p-noise three which is three dimensional you can actually just do it just in the shell help on the noise library and i'll give you a lot of information anyways so let's pass in our x coordinate and something interesting here is that it's on its own scale so you can spread out the noise or make it denser by modifying the x value you put in so if i multiply it by 10 it'll be crossing that noise pattern very quickly and you'll get very sporadic patterns whereas if i do times 0.1 it'll be slower in crossing that pattern and this is what i'm going to go with because this is terrain you don't want it to go crazy the there's a lot of keyword arguments here most of them are related to the layering if you want to use multiple layers of noise to create some special effects but for this instance i'm just going to set the repeat to a bunch of nines the repeat is just how often the pattern repeats in in infinite world generation you don't want it repeating so i just threw in a bunch of nines anyways in this generation code i've got a few sections to determine if there's dirt grass or a plant the grass is just the top of the terrain and it's determined by y value so what i'm going to do here is replace this y value of 10 which determines where the ground is with the height value so i'm going to do eight minus height let's do by five i'm also going to want to convert this to an integer because i don't want things that are half a tile tall and what not in terms of the terrain that can cause some issues in this section where i'm saying it has to be an exact a certain value to place grass so if this is 8 minus height by 5 don't forget that the reason why i'm doing the times 5 is because the noise returns a value from actually i think it's negative 1 to 1. so by multiplying it by 5 i can make the amplitude of that noise larger which makes the train have taller mountains and stuff like that all right so this last section here is just taking this 9 and going 8 minus height by 5 minus 1. one that'll make it so that the grass appears on top of well the plant appears on top of the grass this is all relative to the ground level to determine the stuff it goes dirt grass plant actually made a mistake here since this is negative one to one i have to multiply by five in here not over here i don't know why i was doing it over here i want to apply the height amplification and then convert to an integer it was just being forced into zero because the range for the output is i think zero to one actually anyways with that fixed we can take a look at our terrain now so as you can see i spawn right on a hill and i can walk around and you'll see more train this is coincidentally pretty flat i just happened to be the noise pattern seems to like to hover around zero for whatever reason but there's hills and grass you can see the grass is being properly placed anyways that's pretty much it for implementing one-dimensional noise i'll probably do more videos on two-dimensional noise and the things you can do with it two-dimensional noise is a lot more useful for actually a top-down world generation and sometimes you can generate caves using two-dimensional noise too little caves in uh side views games the top-down stuff typically uses two-dimensional noise and it layers it for like different biomes and then heights so you'll have water at low enough heights and stuff like that but yeah the implementation is pretty simple and i don't really expect people to be working off of what i did like in my code in this video i'd expect that you'd implement it into your own projects however you see fit just keep in mind you pass in an x value and it gives you the in this case the height of the terrain you'll have to multiply the value you put in and divide get out to modify some of the parameters a bit for like how spread out things are and how tall the mountains are if you don't know how to do the world generation i do suggest you check out my episode 7 video anyways that's pretty much it for this video if you're interested in my projects you can check out my twitter where i post frequent updates or fairly frequent updates on whatever i'm working on i also have a dev vlog series on this channel where i'm showcasing a project i'm working on right now and i also have a discord server with a channel dedicated to questions so that i can answer questions people have may have although if you do have any issues with this specific video i do suggest you download the code from the description that fixes most of the issues people have hopefully i'll see you guys in the next video
Info
Channel: DaFluffyPotato
Views: 26,914
Rating: undefined out of 5
Keywords: python, pygame, DaFluffyPotato, python tutorial, pygame tutorial, tutorial, pygame world generation, world generation, terraria, pygame terraria, pygame terraria generation, worldgen, pygame perlin noise, perlin noise, simplex noise, noise, programming, computer science, learning, coding, terrain, terrain generation, platformer, pygame platformer
Id: 1yJI8znB5aQ
Channel Id: undefined
Length: 8min 14sec (494 seconds)
Published: Sun Jun 07 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.