Introduction to Ursina (by coding the Breakout Game)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome what if i told you that you could make a breakout game in under 60 lines of python code i'm talking about this game so this is only possible thanks to a new python library called orcina now arsena is an environment that's designed to help pythonistas make games so it's a bit like pie game with 3d functionality and even sort of more radical shortcuts in terms of what you can do so it would be something of a cross between unity and pie game with a lot of automatic functions so yeah that is the game we're going to build and we're going to do it step by step um taking baby steps through essentially a set of tutorials i've pre-prepared so on my website you're gonna be given a link to this so there's going to be the cheat sheets page and this is the or cena cheat sheet now the ursina cheat sheet is going to have i think six steps in programs so we're going to start by just drawing shapes then we're going to go on to i don't know why the scroll slowly but then we're going to go on to rotate those shapes after rotations we're going to do some collision detection then we're going to create more objects we're going to have like a room where the ball bounces and you can see sort of how that works out uh to to beat the breakout game so not only uh will you be able to just watch this and learn how to do it but you'll be able to download this pdf cheat sheet that's gonna make your life easier okay so should we get started let's do it so how do you import your cena and um just basically run some code that does something so it's going to be the general import from our cena import star that basically gets in all of the functions within the library to work now you need to basically create the app object app equals the arcena then you gotta run the app now this won't do anything other than create um a sort of blank screen like that check that out so that's your cena and you can put stuff on that screen without actually having to do any sort of special functions so you can create like a a game object so this would be like a sprite and pie game and that is called an entity so if we were to create a shape like square and square would be an entity and an entity has a collection of um attributes so we're going to say the model of the entity model of the entity is a quad and then we're going to specify where it is and what color it is so perhaps we're just going to say color equals the color dot blue and [Music] scale is now the scale of it basically this is sort of the weird thing your cena dimensions go from minus four to four uh on the y-axis and minus six to six on the x-axis so it's sort of like a four by yeah it's like a four by three um dimension um so it's not really using pixels as one would expect but don't worry go with it um so if that is the scale i think this already might be enough to display it model is not defined well that's because i didn't use the equal sign well let's try that again so yeah now we've gotten a square on the screen so you can basically specify like x y positions but you can just say something like position equals and this is going to be like x y z so let's just say we wanted to move the square up you could say zero comma one comma zero so i would expect that um square to move up a bit position that needs to be separated with a comma so yeah squares moved up so this is how you draw shapes now you can change this around to make this um a circle for example so maybe we can just copy this and say you know circle is a different entity circle is a different entity and then the shape is a circle and maybe we're going to draw it somewhere lower and all of a sudden you're going to see a square and a circle and then you can change color dot orange so you get the idea very very easy to draw shapes on the screen now you can do something called editor camera and this will allow you to sort of drag the environment and just see hey so that this is painted there's nothing behind it but you can right click and just observe you know your entities in a three-dimensional space okay so that sort of covers um the very basics let me show you something else now this is this is kind of crazy there is a function called update now when you define the function update this automatically runs once a frame i mean the the rasina engine is sticking over at 60 frames a second so this is gonna do stuff once a frame and you can do for example like square dots rotation and underscore x and you just i don't know add some stuff to it plus equals the two maybe so you'd expect the square to start spinning and there it is like i said there's nothing on the on the back of it i mean you could have done now you could do the same thing for like y as well and now it's gonna like spin on two different axes there you go and you can do the same to the circle if you want so yeah this thing actually updates the movements of all your entities so basically the the structure of an arsenal program is uh going to consist of like defining whatever we're gonna have in terms of objects i mean entities or objects and then um defining the sort of rules of motion that they follow i imagine in between like depending on what you want to do you might want to define some of your own customized functions but you know with this structure as it is we can create um sort of bare bones of the movement dynamics um of a game so now that we've done that let's see if we can just create a collision because collisions are going to make our life more interesting so perhaps like we got that circle already and now we can bounce the circle of the square or something like that right so maybe position the circle to go up like two there and maybe make the square uh go down like i don't know minus three maybe make the square p wide like eight let's just see if this is uh i'm gonna position them correctly yeah i don't know let's spin let's not spin the square i'm just gonna make that uh as an empty function because why not and let's see if that's like yeah maybe move that square minus 2 here because i want to be able to see it right and i'll tell you what i'm going to try to do that's the ball is like way too big 0.5 [Music] i want to make the ball bounce off of the the square and then that way we can actually have ourselves a collision so how is that going to work okay so first things first let's make the the circle the ball full so circle dot y minus equals one so we increment it down by oh i think yeah 0.1 maybe let's try to see what happens yeah it falls 0.01 what about now yeah that's nicer so what i want to do is i want to make that bounce off of the edge and see what we can do with that how do we create that intersection all right so this is going to require the dot intersects function so i'm basically going to copy one of the examples that racina has provided on their own website which was like the pawn game and i'm going to say in info equals the circle dot intercepts now that hit info here is going to be an object that's going to create it's going to have like some attributes like um hit info dot entity for example and that's going to be able to identify an entity but um it also is going to have this so you could basically check if hit info dot hit so any given frame like the dot hit attribute is going to give you a boolean a true if there has been a collision so this is like the simplest possible example it just checks if the circle collided with something well if the circle collided with something you want it to bounce right back and i guess that this would mean this would mean that this here has to change so maybe i'm just going to create like watch this circle dot d y so that's going to be like the sort of delta on y and in the circle i'm just going to add d y equals to 0.01 and if there's an intersection then i can just say circle dot d y equals the negative circle dot d y and watch this should be a bounce huh let's i guess um what am i missing i think we needed to specify what type of collider we're using so i'm just going to say collider equals to the box so um i had assumed that the collider was like a default parameter but no i think you need to specify so by using the box collider i mean you can actually change the the view here you can see it bounce but that that's we're basically treating the ball like it is a box you can use a sphere collider but a sphere collider really like this is an infinitely thin 2d shape a sphere would make that a 3d shape and that would be a bit awkward so like boxes is sort of the easiest one to use i know this from trial and error all right so we can draw some entities move them collide them now we can actually begin to attempt to make our game so what's that going to take well let's sort of break up the pieces and then try to start coding them one by one so i guess the first thing you want to do is um build some walls right so like the ball is going to bounce off of the off of the walls and and you can just start by renaming that the ceiling and then you know we can figure that one out um i guess we're going to need to [Music] create a ball and we can already have the ball so that's nice um well after that you create a paddle so something to bounce the ball so that's gonna be three create a paddle um after you've created a paddle you gotta build some bricks right like [Music] create some bricks to destroy and after you've got some bricks um i mean you've got gotta then have an update function that handles all movements and collisions so i suppose the number five is really the the hard one um and these guys well this is just essentially you know creating this entity function and calling it however many times i suppose we're going to need some precision so i'm going to perhaps copy some you know trial and tested code to make this quicker and easier for you guys and once we've created all of the objects then it's going to be a question of writing the logic to make the whole thing work so let's get started um let's run this and just have a look at our uh oh circle's not defined yeah because that was the ball i guess we don't really need the we don't really need the logic here i'm just gonna pass and all i want to do is is just figure out how to do the walls right um also what color to do them so ceiling i'm going to go with orange i'll stay true to the thing we did before so scale um that's if eight is good enough let's make that like thinner incoming 0.2 now the position hey so ceiling it's going to be like very nice very nice that's got to be like -2 that's got to be 2. okay that's that could be our ceiling and now we're gonna do left wall right wall [Music] so let's try that left um so the scale goes opposite here 0.2 comma 8 i guess 6 would be because 8 is the width of the screen 6 would be the height [Music] minus four and the width i'm not sure and we'll find out yeah i think it's got to go like minus 4 or something like that minus 4 going to be off the screen we're about to find out 3.5 so these the scales are just yeah they're just a tad weird right i don't even know like 3.6 seems to be the the right number um you can actually modify the scale there is there is a special function you can call um you can check that out on the official cheat sheet but i warn you the official cheat sheet was it's a bit complicated i spent days studying it before i could build anything so i mean if that's going to be the right wall then that should just be 3.6 as opposed to negative 3.6 so yeah we're we're on it right ball why doesn't the ball start in the center of the screen that'll make my life happier tank balls too big though maybe 0.2 and i think like uh uh i'm not sure if you could just because balls only got a radius i'm not sure to draw it in a in a box but anyway that that that gives us the ball of the right size so paddle um paddle are we gonna go orange again well we can basically copy the ceiling right and then change the dimensions and the rotation [Music] paddle and that's an entity what's going to be the width of the pedal um like let's try like 2 0.2 sounds about right for the thickness um [Music] what about minus two won't be enough will be like minus one and a half or something what okay like that's very bottom and i don't know i think that the the paddle is like too wide um scale what about what if it was one one this paddle too big yeah one point one point five okay so we've got a paddle we got these walls we got a ball and maybe just make the ball a different color um ball color white there you go so we got ourselves most of the pieces um bricks i'll tell you what i'm gonna do the bricks after the logic let's see if we can like get the ball movement um if we can get the ball movement happening and then we can figure out the rest now one thing that i'd like to do with the ball movement if you remember from the advanced breakout if i just stand here the ball's going to bounce straight back up right but if if i go here the ball goes sideways so you can actually like you could aim look at that you can actually aim where to take the ball thanks to like where it lands right um so you're not really changing the direction the direction is fully determined by which part of the paddle it lands making it easier to aim so that's what i'm going for but to do that i can't really just do the change in dx or change in the y i'm gonna have to use the built-in direction so that was the direction was like sorry the rotation was the thing i was spinning at the start so we're gonna need to give the ball some kind of uh z rotation because that axis is the one coming out of the screen so where's the ball uh rotation zed nope sorry that was that's what we're gonna do here um no more this we will have row rotation z equals i think 90 faces it downwards but we're we're about to find out yeah so we're going to do that so rotation z is 90. that would make the ball face downwards it doesn't have any movement but that's okay we can actually program that in so now we're gonna figure out how to do the um the collisions but one thing i realized we haven't done yet is figured out how to move the paddle that's going to be remarkably easy so watch this paddle paddle x which is sort of a built in paddle x equals to and we can say held keys right arrow so let's try that and you can multiply that by um so that's basically how long you're holding down the keys so you're adding that if you're holding i'm sorry you're you will multiply by how long if you're holding it and that could be like time dot dt this so if you're holding it this basically turns into like a one and then you're adding for how long at time now this might mean that it moves really slowly oh actually this is fine maybe i want to make it a bit quicker like i don't know times five might be too fast no that's cool i like i like fast and i guess you can now say that that's the right arrow and then you can say minus left arrow times tiny t i don't know if this is gonna work i think so i think this is what i did the last time [Music] um so that would mean that if you hold both keys it stays still and don't that's cool all right how easy was that so that like the event handling loop and pie game would have been to do this would have been like 10 lines okay so now we can move right and left and now let's see if we can make the ball fall so [Music] if we think about position equals the speed times time all dot position equals to [Music] ball dot right so i mean that is like the the turn times times dt times speed actually let me just so since you're in a two-dimensional space position equals the direction times speed times time so that would make that would make sense if especially if it works get rid of that okay oh plus equals so ball it doesn't position so the ball actually could be given a speed speed equals to i don't know what one is if that's slow or fast [Music] okay there you go it's falling so this time it has a direction so i mean if i was to make the 60 you're gonna see it go sideways right so okay and by the way speed one is nothing so if it was p10 that might be more appropriate yeah it's gone so maybe three cool so now um and how's going to come this idea of well how do we make the thing bounce now we're using direction rotations that goes from 0 to 360. so when it hits the paddle we're gonna need to translate that zero to 360 to some number that depends on which part of the paddle is being hit so we've got the the basic movement there um so we're just going to do what we did before right so we're going to say hit hit info equals the ball dot intersects and then if hit info dot dot hit now that means if there isn't if there is a collision if there is a collision then you could say something like if hit info dot entity double equals the paddle and we should have we should have three of statements here we should have the paddle we should have the um left wall right wall and ceiling right three sorry three more ceiling left wall now we don't have bricks yet so we're eventually we're going to have bricks but let's just um let's see if we can program these in and then the bricks are going to be the last thing i add so these are the the things that we currently have uh in our environment that the ball can collide with and we're gonna need some rules for all of these now some of them some of them are easy like left wall and right wall all that happens is sort of you you uh say 180 minus so like watch this fold up rotation underscore z so if you change the direction by 180 right if you change the direction by 180 you just sort of go back the the way you came from and so ball rotation equals to 180 minus 4 z now it should be the same for right wall or left wall but um [Music] let me just do it this way first and let's point that ball in the direction of the right or left wall so like like what's what if this was five so now it's gonna hit that and if it bounces right off of it hey that's good and now it's gonna bounce off of the other wall um as you can see i can still spin this but the actual accuracy of these collisions isn't perfect weirdly enough or cena does a better job in three dimensions and two dimensions but this this works if we run into trouble there is also something called collision cooldown which is a special variable that doesn't allow collisions to be detected for another certain amount of time after a collision has been detected so that's going to be up our sleeve if we get some glitches but i don't want to use it until um until basically program starts running into trouble i think the thing i want to do here though [Music] i have a feeling that that's the one we want so it goes there i'll just check the left wall and after that we're going to get the ceiling and last but not least we're going to get the the paddle sorted yeah that all works happiness so how do things bounce off of the ceiling okay let's have a look um if we had like if we just reversed the z rotation so i don't know how to describe it to you but um if you've done the unit circle in uh trigonometry if you take an angle that's like 70 then the angle minus 70 would be like the ball bouncing back off the ceiling right so like if you just sort of take the negative then then that should work ball dot rotation underscore z equals the negative and just take my word on it i guess let's try minus [Music] 50 here and let's see let's watch it bounce off of that correctly cool so we're bouncing off of the um the ceiling the wall and things are looking good we're we're staying within the space so now finally the paddle how are we going to handle this well i guess we're going to need to calculate like how far off the center of the paddle um i'm just going to call it like delta and delta is going to be it dot hit info the entity so like given that we're doing ball intersect it's going to be hit them for the entity right that is going to be the x of the paddle i really could have just written bloody paddle at x um i mean that might make more sense it's the paddle x and the paddle x minus ball x so if you hit in the center that delta is going to be 0 if you hit to the right then that's going to be some decimal and what you want to do is use this delta to point where the ball is going to go so if you basically say right um ball dot rotation z equals the delta times x plus so imagine you want to point the ball up because if that that's zero you want like pointing up which is like minus 90 i think or or 270 or minus 90 whatever you want to do so like if this was delta times let's just make it times 10 i don't minus minus 90. so i don't know if there's going to be any difference between -90 and plus 270 but like just just watch here it should deflect when we hit it right i'm gonna try to hit it with the center of the bat and when the center of the bat it should deflect like straight up watch huh well that was ridiculous um but i did the ball rotation there and everything um it should have changed direction if you hit info is the paddle that's delta [Music] yeah see i don't even know how this was allowed a misspelt rotation rotation jeez that must have been it so like the fact that that didn't bring up an error is just alarming so yes see how it bounces straight up so now we're going to try the edge to figure out like whether it was 10 times was the right amount oh i think it's not only like it's going in the wrong direction watch this it'll bounce slightly to the left here yeah so instead of minus 90 i mean i don't know plus 270. it doesn't make sense because it should be the same maybe like we need to reverse that 217 like 270 minus that all right let's try 20 just just just check that it's going in the right direction and if it is going in the right direction then you know rock and roll baby um ha it is going in the right direction but like 20 is nowhere near enough [Music] i mean how much do you want to be able to bend it 60 [Music] 80. like much yeah more i don't care 80. [Music] so once we get this guy to be able to aim the ball basically and then now you can aim the ball the ball really should be a lot faster but we'll worry about that later that this is all perfect for testing um once you can get the once you have all of the elements that we do i think that we can create some bricks so um how do we do that well i guess the uh you know we're gonna need like a double loop for x in range something and then four y in range something and then we're going to create like a brick oh we definitely want to have like a list of bricks so maybe bricks is a list and brick equals the entity and you just sort of want to draw like a quad and you want to draw a quad on some type of a range so we're gonna need like i don't know i mean how does the if if the screen begins at negative 4 on the x so watch this like minus four to four and that's gonna be the range and then this is gonna be minus three to three so don't worry these numbers like we're not really trying to get it right we're just trying to prove the the concept here so if if this was something like a ceiling right um i'm just going to just just hang hang here all right we're gonna turn the bricks red we are going to have them on on some sort of scale and um let's see like if the paddle was one and a half right so brick is gonna be scale one point two or point maybe make it point point three and this is the position um what if this was x comma y right okay see we got some bricks here um bit too wide though bit too high it was like 0.3 just just to see just to show you how scattered they are this should work see there you go those are the bricks so now we just got to figure out how to like oh how to make that work um so if for x and y i mean like let's just say uh i'll just do you guys a favor and um i'll copy out this code because this stuck i kid you not about 20 minutes of finagling trying to get the numbers right and how is it that my other game has a completely different scale um maybe because i have an editor camera that changed the proportions or something great so that editor camera changed the proportions and so my proportions were all wrong compared to that jeez all right um what do i got to change now um [Music] let's just start with the ceiling okay so this was like 16 because everything seems to be double the width and what positions are you going to be in let me just try i just made things wider okay so we need more height on the ceiling three four is this four enough yeah and then these guys could be like stubble i guess 7.2 [Music] that's i guess a lesson we should learn the oh my god this seems to be working um i need to change the position of the paddle i probably need to make the paddle a bit bigger so paddle can be two here and i can make it what if it was minus four would it still fit in if it was like flush with the bottom yeah i don't know like this is fine this was actually this is actually surprisingly quick let's see how good the collisions are make that ball ever so slightly bigger um ball point five i guess it's not ever so slightly let's see how much bigger that is that's too big i didn't means slightly yeah that'll do okay so we have we've got the bricks we've got everything in the correct dimensions and now we are going to figure out how to intersect with bricks let's do that so we've got ceiling ball and finally if hit info dot entity now if this entity in bricks then we've hit a brick what do you do if you hit a brick wow um you're gonna need to bounce the same way you would off the ceiling wouldn't you so um you're going to re do the z rotation right except [Music] except of course you want to destroy the brick and you want to start somewhere on the screen and not on the bricks so where is the ball created position maybe begin with a slightly negative y and maybe face downwards which was i think i don't know was it 90 yeah and it bounces so now we destroy the bricks there is a thing called like um the dot destroy this troy uh let's see that so let's see if it's gonna get rid of a brick no attribute destroy uh oh sorry the the function went like this that and if that works then we can also remove it from the list of bricks and destro destroy let's see boom there you go that works so we don't really have like winning losing conditions yet and this ball sure could be a bit faster in fact um why don't we like ever so slightly increase the ball speed every time we hit it so if you hit the paddle ball dot speeds times equals to one point times equals actually better this is nicer times 1.01 so it gets one percent faster so it'll start off slow but it'll just keep getting maybe two percent or whatever but we can just keep slightly increasing the speed i thought that that might destroy two bricks because it's gonna be like colliding with two of them at once [Music] the collisions remain accurate enough um yeah that's all pretty good so we really have in under an hour reached basic proof of concept breakout right so at this point you can create some global variables for for score and have some text displays that are going to make the game work perhaps create some more custom-made functions uh these functions could serve to maybe display the score on the screen or end the game or something like that um but yeah this thing works sorry this was going to say bricks dots [Music] think that can go there so let's see if i can i just want to make sure this doesn't cause an error and if this works we're going to wrap it up in the next 10. okay so that hasn't crashed brilliant all right so now we can create some sort of a global variable we can call that score is zero every time we destroy a brick score plus equals to one um if so now like you could also declare i suppose like if all the bricks are on the screen and i think all the bricks are on the screen you could say if the if flynn bricks double equals zero uh we could win right um it's sort of i don't want to spend half an hour playing this but yeah okay so that could be the winning condition um and print twin for now and when do you lose let's do the losing condition so the losing condition is if ball dot y so if you like miss it and ball dot y falls below the screen so maybe less than negative five print [Music] um [Music] just i suppose it's worth testing the the losing condition i'll just get out of the way and it reference before assignment [Music] how about now so if i get out of the way it should start printing you lose aggressively um is it still right um what let me think of that for a second ah yeah no no no no it's not inside info.hit that should be there and it should just start printing you'll lose like crazy here yep that works so it's all right stop that and so now that we've just sort of proved how these work i would like to write a function for ending the game and [Music] that function is going to contain a message and it will also sort of like freeze the screen with that particular message okay so this is gonna be the last function so i'm gonna call this def and game all right so that's going to be the endgame function the endgame function is going to have a user message a message for the user so obviously that's going to be a string and we're going to use a message um that's going to be like a message object that's the text object and in there you decide what your text is going to be and our text is going to be whatever we say here then we specify scale and from experience i figured that 2 is the right one origin i don't know if this is like a required parameter i believe zero zero would be the default but that's that's what it is so we might as well specify it back ground present and color color equals the color dot green all right so if we get green color um that would work so let's try the endgame function and game here's the message you lose um maybe specify score let's see if that works oh one more thing one more thing one more thing application dot pause [Music] application dot pause so this will sort of freeze the application because last time you guys didn't notice because i sneakily had to restart visual studio code when it was saying you lose on a loop look at that you lose score one and this could be i guess you don't need anything you could just you win so how in the world how in the world are we gonna test that this is gonna work um can i count how many bricks we got one two three four five five by one two three four five six seven eight nine ten eleven twelve thirteen fourteen five minus seven do we have seventy breaks so why don't we just like i don't know less than sixty so that's gonna be set to zero i just want to make sure that this thing works so when i destroy 10 bricks um it's actually going to tell me that i win if that is the case then the game is indeed complete um line facer um you don't you can't you need to remove hidden info dot you're moving that entity let's try that again so break one break two why not for the fun of it make it ten percent more this is gonna be pretty radical in terms of how how much faster this is going to get with every it it might be fun to play but it'll be over so quick here we go boom it's already getting faster we're going to win oh my god 10 10 percent of pop did i destroy 10 bricks yes you win so if it works on 10 bricks it's gonna work on double equals to zero and when there are no bricks left and please don't do that that's gonna make the game impossible maybe 1.02 so two percent faster with every hit i think that's how the original breakout worked so you pretty much got to get your sort of ball in behind the bricks as soon as possible and there it is 60 lines under one hour breakout game in orcina python so i hope you guys enjoyed that as much as i did catch you later more videos coming up on the topic of orcina take care and subscribe please do bye bye
Info
Channel: Sanjin Dedic
Views: 9,997
Rating: undefined out of 5
Keywords: Python programming, beginners python, python workshops, python education, learn coding, ursina, ursina engine, ursina game engine, breeakout game
Id: P8J6K6p-JzY
Channel Id: undefined
Length: 57min 57sec (3477 seconds)
Published: Fri Jan 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.