Pygame Tutorial #2 - Jumping and Boundaries

Поділитися
Вставка
  • Опубліковано 2 січ 2025

КОМЕНТАРІ • 886

  • @TechWithTim
    @TechWithTim  6 років тому +41

    Interested in what its like to be a computer science student? Check out my Podcast: anchor.fm/tech-with-tim
    Follow me on Twitter: twitter.com/TechWithTimm
    Join my Discord to Ask Questions and get help with pygame! discord.gg/pr2k55t
    SOURCE CODE: techwithtim.net/tutorials/game-development-with-python/pygame-tutorial/jumping/

    • @Rajat-Sharma1
      @Rajat-Sharma1 5 років тому

      Hey!!
      How can we make it bounce after colliding with a boundary on any of the four edges depending on the angle with which it is incident on any surface?
      I tweaked the code to make it like a SNAKElike set up wherein if you take the box to the top right-hand corner, it would pop up out of the bottom left hand corner. in fact that was what I thought when you said about this in the last tutorial.
      PLEASE REPLY.

    • @rohansingh1057
      @rohansingh1057 5 років тому +1

      Can you please tell me why when i code a pygame code in IDLE then it shows an error that pygame has no attribute called init.
      I can make it run by writing the code in ATOM and then i double click on the saved .py file then it creates a folder with a compiled python file which can run just like normal scripts.
      It is very difficult as if i make a mistake in the code the program does not tell me where the error is and it just closes. I have to go through the entire code to check what is wrong(usually capitalization mistakes) it would be nice if i could just do it from IDLE.
      Please help because after watching your videos I am really excited about pygame and can't make big projects without this problem being fixed.

    • @strawberryyogurt0
      @strawberryyogurt0 5 років тому

      @Respact King ... I was having the same issue, until I re-watched and video and retyped the code out. I mispelled the word jumpCount. But here's the working code:
      import pygame
      pygame.init()
      win = pygame.display.set_mode((500,500))
      pygame.display.set_caption("First Game")
      x = 50
      y = 425
      width = 40
      height = 60
      vel = 5
      isJump = False
      jumpCount = 10
      run = True
      while run:
      pygame.time.delay(50)
      for event in pygame.event.get():
      if event.type == pygame.QUIT:
      run = False

      keys = pygame.key.get_pressed()
      if keys[pygame.K_LEFT] and x > vel:
      x -=vel
      if keys[pygame.K_RIGHT] and x < 500 - width - vel:
      x += vel
      if not(isJump):
      if keys[pygame.K_UP] and y > vel:
      y -= vel
      if keys[pygame.K_DOWN] and y < 500 - height - vel:
      y += vel
      if keys[pygame.K_SPACE]:
      isJump = True
      else:
      if jumpCount >= -10:
      neg = 1
      if jumpCount < 0:
      neg = -1
      y -= (jumpCount ** 2) * 0.5 * neg
      jumpCount -=1
      else:
      isJump = False
      jumpCount = 10
      win.fill((0,0,0))
      pygame.draw.rect(win, (255,0,0), (x, y, width, height))
      pygame.display.update()
      pygame.quit()

    • @tshokelo1219
      @tshokelo1219 4 роки тому

      Bro my code requires that i press and hold space to jump or else it jumps half way

    • @iiindexxx
      @iiindexxx 4 роки тому

      hey, i got a question
      every time i run your games in this step, it says: "DeprecationWarning: an integer is required (got type float). Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
      pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))"
      what is this? and how can a avoid this?

  • @zecko9534
    @zecko9534 5 років тому +1498

    Bro I thought I was being hacked when it said powering off

  • @adpadillar
    @adpadillar 4 роки тому +351

    Tim: I’m sorry it took so long for me to upload the second video
    Me: Laughs in 2023

    • @karanpatel8267
      @karanpatel8267 3 роки тому +4

      same hahaha

    • @joe-bd6mf
      @joe-bd6mf 3 роки тому +2

      me too lmao

    • @freshness_of_air7909
      @freshness_of_air7909 3 роки тому +1

      lol same

    • @zoezulma594
      @zoezulma594 3 роки тому +1

      Not me. I am outraged or something

    • @Resuri02
      @Resuri02 3 роки тому +2

      Yo did u come into any problems with the jump mechanic? When I jump i fall through the floor

  • @ravenssunglasses2660
    @ravenssunglasses2660 5 років тому +442

    2:30 i had to go back a few seconds in the video to make sure it was your device thats getting powered off and not mine lol

    • @waterfallbw
      @waterfallbw 5 років тому +24

      yo saaaaame
      i was like " i knew i shouldnt have installed that program"

    • @fanatic1277
      @fanatic1277 5 років тому +3

      @@waterfallbw damn son that also happened to me

    • @itzinky1506
      @itzinky1506 4 роки тому +1

      i kind of almost crapped my self

    • @kidskpadonou894
      @kidskpadonou894 4 роки тому

      ahlie that seriously spooked the living daylight out of me

    • @rare5x
      @rare5x 4 роки тому

      Same bro

  • @MattTBN
    @MattTBN 5 років тому +110

    We have now successfully constrained ourselves inside this box.
    Most relatable thing here.

  • @boopypasta
    @boopypasta 6 років тому +244

    POWERING OFF!!! Good, so I'm not the only one. I've also been hearing random beeps. IDK. It's weird, but good tutorial!!!

  • @gregoryyang8988
    @gregoryyang8988 4 роки тому +153

    Comments at the bottom: "Wow nice video." "What if XXXX happens?" "Can you make a tutorial about YY?"
    Comments at the top: "pOWeRinG OFf>"

  • @ndamasimon5732
    @ndamasimon5732 6 років тому +442

    did anyone notice the "Powering off" sound effect?

    • @TechWithTim
      @TechWithTim  6 років тому +65

      Lol i think that was headset in the background... oops

    • @shreesinghi6356
      @shreesinghi6356 6 років тому +76

      That scared the shit out of me!

    • @tehcude9155
      @tehcude9155 6 років тому +10

      holy f wtf was that

    • @retroeagle8151
      @retroeagle8151 6 років тому +1

      yea XD

    • @test-bh9su
      @test-bh9su 6 років тому +14

      How the fuck can you not notice that?

  • @daniel-hc8gr
    @daniel-hc8gr 4 роки тому +12

    7:01 There's an easier way to do that :
    if x_cor >= 750:
    #screen width - Character width
    x_cor = 750
    #screen width - Character width
    elif x_cor

    • @donk7001
      @donk7001 Рік тому

      what are the hashtags for

    • @Hiniachi
      @Hiniachi Рік тому +1

      @@donk7001 notes they dont affect the code

  • @oreo9268
    @oreo9268 4 роки тому +2

    You are the most genuine youtuber ever. Here we can get pure raw knowledge without any bs
    Huge respect for you bro.

  • @sohampatil6539
    @sohampatil6539 6 років тому +132

    if jumpCount >= -10 :
    y -= (jumpCount * abs(jumpCount)) * 0.5
    jumpCount -= 1
    Much quicker using absolute value function

    • @samuelsamuel4099
      @samuelsamuel4099 5 років тому +14

      This is a video to learn how to code. Sure it's more compact, but not everybody will understand it.

    • @danyalmalik4245
      @danyalmalik4245 5 років тому +14

      Nah it’s as easy to understand as what he did in the video if not easier

    • @meirklemfner114
      @meirklemfner114 5 років тому +7

      Much quicker is to use hyperbolic function (x ** 3)

    • @meirklemfner114
      @meirklemfner114 5 років тому +7

      sorry it isn't called hyperbolic

    • @pepehimovic3135
      @pepehimovic3135 5 років тому +5

      Round of applause...

  • @refaelnesterov6547
    @refaelnesterov6547 Рік тому +2

    bro I thought my computer was haunted from those background noises from your computer.
    I got so scared when it screamed "POWERING OFF" 🤣

  • @paulburkart3575
    @paulburkart3575 5 років тому +13

    For the boundaries, I just created "_WIDTH" and "_HEIGHT" for the main windows dimensions (both are still 500), then I just did the following:
    K_LEFT:
    if x >= 1
    x -= vel
    K_RIGHT:
    if x < _WIDTH - width
    x += vel
    K_UP:
    if y >= 1
    y -= vel
    K_DOWN:
    if y < _HEIGHT - height
    y += vel
    This allows you to get right up to the edge of the window, without the 5px gap

    • @rockguitarist8907
      @rockguitarist8907 2 роки тому

      I believe that’s going to cut off the right side of your rectangle by 1-4 pixels. It will almost look identical, but slightly go over. Same thing with down except cuts off the bottom by 1-4 pixels.

    • @iGavid_Doggins
      @iGavid_Doggins Рік тому

      1337 H4xXoR

  • @gorgestboi1028
    @gorgestboi1028 6 років тому +279

    2:31 wtf

    • @lucasresendegoes6840
      @lucasresendegoes6840 6 років тому +36

      This scared the heck out of me

    • @8_bit-dev293
      @8_bit-dev293 6 років тому +8

      yeah why is is there

    • @ianhackzofficial313
      @ianhackzofficial313 6 років тому +2

      IanHackz died

    • @dakk3
      @dakk3 6 років тому +24

      I thought for a second that it was the end of the world and my computer is talking to me

    • @puzzled295
      @puzzled295 6 років тому +31

      it must be his sex doll turning off due to inactivity for 5 minutes explains the youtube break

  • @Petarglio
    @Petarglio 5 років тому +7

    I found that jumping equations work better with linear equations. This is because the derivative of a quadratic equation is a linear equation, so while you want the shape of your jump to be quadratic, the rate at which you change the y value per frame is linear. An example would be y -= 30 - (2 * jumpCount).
    Great video!

    • @Medhusalem
      @Medhusalem 5 років тому +2

      I tried to use both, but the quadratic equation is much better at simulating a real jump. Because the values rise faster in the beginning and slower near the end. This is what you would see during a normal jump. Your equation wouldn't do that. There no "gravity" is implemented where you start slowing down near the end of the jump. I like not having a uniform speed during a jump animation.

    • @ksy8585
      @ksy8585 5 років тому +3

      That's what I wanna say. Linear equation makes sense physically.

  • @svijayiitk
    @svijayiitk 5 років тому +20

    0:33 I freaked out with that "Beep-beep"
    1:33 I freaked out with that "Beep-beep", again. Wait! That was a one minute 0 second difference!
    2:31 Why is this saying "Powering off"?!

  • @MozartJunior22
    @MozartJunior22 5 років тому +4

    11:23
    The correct formula should be y -= a * jumpCount. This is because the *change* in y at each step should be the *derivative* of the parabolic formula

  • @maharshi9999
    @maharshi9999 6 років тому +125

    U r doing tutorial at 1:20 Am. Omg plz sleep

  • @intansyarina862
    @intansyarina862 6 років тому +8

    I've been following your tutorials from the 1st and this for 2 hours and im so happy that im actually did a Mario character (beginner) hahaha

  • @Mr.Dirkelz
    @Mr.Dirkelz 6 років тому +18

    Great!Just Great, finnaly a good mentor for pygame, you just got a biiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiig fan

  • @markrobintaylor
    @markrobintaylor 5 років тому

    I think it's simpler to just store the initial y position and then add height to that by means of a parabola. No negative checks required. I also added something to the jumpCount variable to prevent the block from jumping off the top of the screen. Now it does a little jump, bumps the boundary and comes down at the appropriate speed. Check it out:
    else:
    if jumpCount == -10: ytemp = y
    a = jumpHeight/-100
    if jumpCount

  • @Dude-cf8hb
    @Dude-cf8hb 4 роки тому +1

    If anyone doesn't get the jump thing, I'll explain it:
    First remember that if jumpCount >= 10 then neg (which is like gravity) is equal to 1. This means that your character will go up as jumpCount squared * 0.5 is going to be * by 1. But if jumpCount is < 0 (or if jumpCount is a negative number) then neg is equal to 1, so jumpCount squared * 0.5 will be * by *negative* 1, so y will be minused by a negative number, so y will add it because a positive minus a negative number (p - -d) be the same as adding the number (p - -d is the same as p + d). Therefore, your y value will go down, because the lower on the window you go, the higher your 'y' value will be. Then, jumpCount is being minused by 1 everytime the loop will loop around, so your character goes up but at a decreasing rate. Then, when jumpCount reaches 0, like the peak of a graph, neg becomes -1 and you start going down. Then, when jumpCount becomes -11, it is < 10, so the else statement plays, isjump is false, and the jumping stops.
    If this explanation was crap for you, tell me

    • @eeshashahbaz2382
      @eeshashahbaz2382 4 роки тому

      THANKYOUU

    • @vikrantsinghbhadouriya4367
      @vikrantsinghbhadouriya4367 2 роки тому

      Thanks for that one sir! The only thing that remains is learning about parabolas and its equations, for it's definitely interesting!

  • @TheSafeSword
    @TheSafeSword 6 місяців тому

    Session 2 completed, managed to get this tutorial done and loved doing it. thank you for these videos

  • @Noobshreds
    @Noobshreds 4 роки тому +3

    For anyone having issues with jumping/choppiness/cube still going off screen, I switched my code over to Mu(an interpreter) from pycharm and it's working as intended. Not sure why, but maybe try that because a lot of you are having the same issue as I was.

    • @ySkipery
      @ySkipery Рік тому

      For anyone having the problems in 2023. If you write this line before the loop:
      clock = pygame.time.Clock()
      and in the main loop write this instead of the pygame.time.delay(100):
      while run:
      clock.time(60)
      you set the framerate to 60 fps instead of the fixed pygame.time.delay(100). The Clock controls the frame rate and ensures consistent frame rate over the game loop for you.

    • @fazefijiii6932
      @fazefijiii6932 Рік тому

      thx for the comment
      @@ySkipery

  • @powerupminion
    @powerupminion 2 роки тому +2

    Very nice tutorial.
    I'm currently remaking my pygame framework handler and needed a brushup on how pygame worked.
    It basicly makes this relativly low end programming integrated so you can just create an object and the framework will give you easy event handling like key presses, mouse movement, point and click buttons, movement to objects, simple physics and not to mention the main loop is already set up. All you gotta do is running the FRAMEWORK.tick() each frame and the rest is easy! Very handy for doing simple testing and fun projects that require a quick UI.
    I'll definently be watching on!

  • @emanualwondemenh1160
    @emanualwondemenh1160 5 років тому +35

    7:30 auto english subtitle

  • @s3m1ot1c2
    @s3m1ot1c2 6 років тому +16

    please explain the quadratic part. I want a clear idea. Great Tutorial btw! GOOD JOB

  • @sasori913
    @sasori913 6 років тому +28

    Pretty hard for a newbie like me, but keep it up!

  • @1exsto
    @1exsto 6 років тому +33

    Why when you make borders of the screen you use vel variable?
    Why not make screen pixel positioning instead of vel:
    if keys[pygame.K_LEFT] and x > 0:
    x -= vel
    if keys[pygame.K_RIGHT] and x < (screen_x - width):
    x += vel
    if keys[pygame.K_UP] and y > 0:
    y -= vel
    if keys[pygame.K_DOWN] and y < (screen_y - height):
    y += vel

    • @nathings
      @nathings 6 років тому +2

      Yea me too

    • @MiniiFN
      @MiniiFN 6 років тому +2

      thanks it fixed mine

    • @mauriciolinares1182
      @mauriciolinares1182 6 років тому +6

      if you make a screen_width var and a screen_height var and use that when you get to subtracting height and width in K_RIGHT and K_DOWN, you'll be able to update your code easier by only changing the value of those vars once and still have the capability to not get off the screen. Sorry if that was complicated, I just wanted to h a l p

    • @निलेश-ग5न
      @निलेश-ग5न 5 років тому

      Wow it fix that :D

    • @mimicottage
      @mimicottage 3 роки тому

      woah thankx it works

  • @lyricspectrum6438
    @lyricspectrum6438 3 роки тому

    bro your so smart to make that kind of idea for jumping..
    a great respect bro.....

  • @MrVestacus
    @MrVestacus 6 років тому +6

    If the fact that the character is not touching the border because of your velocity is bothering you (like it bothered me), here:
    # height and width is for the character, screenHeight and screenWidth is for the window
    if keys[pygame.K_LEFT] and x - vel >= 0:
    x -= vel
    elif keys[pygame.K_LEFT]:
    x = 0
    if keys[pygame.K_RIGHT] and x + width + vel = 0:
    y -= vel
    elif keys[pygame.K_UP]:
    y = 0
    if keys[pygame.K_DOWN] and y + height + vel

  • @uumatter_0106
    @uumatter_0106 6 років тому +15

    LMAO I was listening to epic music and the POWER OFF sound came just at the right time so I thought it was part of the music

  • @nasejuckt3435
    @nasejuckt3435 4 роки тому

    This videos are a double goal for me because with your videos I`m improving my English and my coding skills... Thank you

  • @maxpoppe
    @maxpoppe 3 роки тому +1

    y -= abs(jumpCount)*jumpCount/2
    here is a more efficient implementation of the jump part, no need for all the if statements and multiplying by neg

  • @megamemes8691
    @megamemes8691 3 роки тому +1

    Might be able to keep object in the center of the screen and have the background move instead....possible video #7?

  • @neckslicer
    @neckslicer 6 років тому +12

    First of all:
    Good tutorial that definitely convinced me to subscribe to this channel.
    Second of all:
    1. Can you tell me what was that "Powering Off" sound? It *almost* scared the h#ll outta me.
    2. Also tell me why would ANYONE bother jumping if he can move freely up and down? Like wtf
    And third of all:
    There is no third of all but making a list of only 2 notes makes me feel like it's incomplete so I added this.
    Thanks for the gr8 tutorial!

    • @TechWithTim
      @TechWithTim  6 років тому +2

      1. Headset sitting on my desk
      2. It’s just for the purpose of learning because in most games you have one or the other and I wanted people that aren’t following the tutorial completely to be able to create their own games with the knowledge of how to jump.

    • @neckslicer
      @neckslicer 6 років тому +2

      Wow gr8 thank you so much
      Also you replied instantly I love youtubers that do this

    • @TechWithTim
      @TechWithTim  6 років тому +1

      You can jump with a mathematical function is what I believe I do in this series or you can jump with a list of preset values to move each frame. Have to watch my side-scroller tutorial to understand that more in depth.

    • @Tony626
      @Tony626 5 років тому

      Ahmed Mokhles also you can use jumping for example if you decide to make a “floor” then you remove y axis controls so you can only go left or right like an adventure game which will allow creating obstacles for example rectangles on the “floor” that will trigger end_event aka you lose like the dinosaur game on google with no connection although not useful for his game very useful for others

  • @axton_alam_narz
    @axton_alam_narz 4 роки тому

    at 10:24, the else statment which carries the isJump = False and Jumpcount = 10.
    The else statment says "invalid syntax"

  • @suprrnice571
    @suprrnice571 3 роки тому +2

    Thanks a lot! Nextime when I know more about pygame I would start making my own games! 😀

  • @abhishek.rathore
    @abhishek.rathore 4 роки тому

    8:35 just a correction to the physics
    During a standard jump, the acceleration is constant. It is always 9.8 m/s2 in the downwards direction. It is the velocity that changes and becomes zero at the top of the jump. I hope it helps ;)

    • @abhishek.rathore
      @abhishek.rathore 4 роки тому

      10:55 Another correction, it is actually the opposite of it. You are faster the closer you are to the ground.

  • @adityajoshi1606
    @adityajoshi1606 4 роки тому

    just came across this video while i was surfing and i am really surprised how cool this tutorial is

  • @raghavajagarwal
    @raghavajagarwal 5 років тому +2

    If everything in the universe depends on everything else in a fundamental way, it might be impossible to get close to a full solution by investigating parts of the problem in isolation !
    Tim> Hope that make sense

  • @adityajoshi1606
    @adityajoshi1606 4 роки тому +19

    2:31 i literally checked my laptop's battery lmao

  • @jonathanpettersson3421
    @jonathanpettersson3421 5 років тому +1

    There's a much simpler way to do boundaries. Just check if x respectively y is > 0 or < 500 before changing its position. Although you have to take into consideration the rectangles size so it doesn't move halfway outside or something similiar

    • @dubjokesguttylike
      @dubjokesguttylike Рік тому

      I tried it before but your character can escape in the corners

  • @tinylittleanj2
    @tinylittleanj2 5 років тому +2

    That's pretty sweet!! I am so impressed with my jumping rectangle!!

  • @simonlee3210
    @simonlee3210 5 років тому +3

    your tutorials are so helpful with my programming project, thanks so much!

  • @bludgeon1081
    @bludgeon1081 5 років тому

    The jump mechanic is genius.

  • @vaibhavkrkm
    @vaibhavkrkm 4 роки тому

    I think we also do this for restricting the player inside the boundaries:
    For left, x>0
    For right, x0
    For down, y

    • @fazefijiii6932
      @fazefijiii6932 Рік тому +1

      I got the same kind of Boundary bevore i watched this vid :D.btw with this tutorial my guy keeps jumping do you know what i could have done wrong?it never gets to the else statement and instantly rejumps for some reason

    • @vaibhavkrkm
      @vaibhavkrkm Рік тому

      ​@@fazefijiii6932maybe you have put the else in the wrong indentation or messed up the indentation somehow, I need to see the code to know what maybe going wrong, if u can, pls share the code, there r some sites which allow sharing of code

  • @Amitbidermoon
    @Amitbidermoon 4 роки тому

    Great tutorial! one note tho, no need for "if keys[pygame.K_LEFT] and x > vel" , just do x > 0 , the vel is speed related and the greater you'll make the rectangle move the bigger the gap will be from it to the screen.

  • @alibahari3526
    @alibahari3526 6 років тому +8

    Thank you very much for this tutorial, its great. But I have a problem while doing this script. when I press SPACE my rect doesn't keep jumping! I mean I have to hold the SPACE to see its complete jump. I exactly wrote the same codes as you did but I don't know where is the problem. please help me to fix it. thanks a lot.

    • @prajitt.288
      @prajitt.288 4 роки тому

      I've got the same problem, IDK why it's happening either!

    • @gamie0412
      @gamie0412 3 роки тому +1

      I had the same! Fortunaly i found a way to fix it. Replace the "else:" from if not(isJump), with "if isJump:" and after that remove 1 tab from every line!

  • @thehappypeople7495
    @thehappypeople7495 3 роки тому

    thanks so much i was looking up so many tutorials of how to make a palyer and they were too complicated for me to add more onto them until i found this

  • @cintra4867
    @cintra4867 3 роки тому +1

    if you want to increase the speed without breaking the boundaries, you need to create another variable and use it instead of vel. Like this:
    screenvel = 5
    if keys[pygame.K_a] and x > screenvel:

    • @Trueleopard42
      @Trueleopard42 3 роки тому

      Does this increase the speed of the jump?

  • @GamePlay-1234-v4x
    @GamePlay-1234-v4x 4 місяці тому

    you dont need to subtract the velocity when you are stopping player right at the movement but you need it when you are checking the player position and repositioning it to its expected position

  • @MenOfPurposeHQ
    @MenOfPurposeHQ 6 років тому +3

    Loving your tutorials, pace, coding style and clarifications. +1 sub

  • @rojadsorum9515
    @rojadsorum9515 4 роки тому +3

    you should make a video on quadratic function I don't understand anything after jumpcount

  • @AceTrainerBanjo
    @AceTrainerBanjo 5 років тому +4

    Hi Tim! Thank you so much for making this content! Very informative and helpful.

  • @Centriez
    @Centriez 4 роки тому +1

    Love your videos bro. Keep on doing the good work. I am 13 year old and I am doing python courses, and your videos help me alot.

  • @blehprojects5776
    @blehprojects5776 4 роки тому

    Probably already commented on but @5:50 is a little confusing. I can't get my rectangle to go all the way to the left or top edge

  • @empanada4326
    @empanada4326 4 роки тому

    another way to do the boundaries check, is by setting X and Y to a certain coodinate if they reachs a certain point in the screen.
    for Example, i did:
    if x > 450:
    x = 450
    if y > 430:
    y = 430
    if x < 10:
    x = 10
    if y < 10:
    y = 10

  • @alexandrevoinier5591
    @alexandrevoinier5591 6 років тому +13

    I copy the same code on my computer and when I jump one time, the rect never stop jumping ???

    • @Vikramjoshi2010
      @Vikramjoshi2010 4 роки тому

      The same happened for me . I intended the else statement for the jump twice instead of once .

    • @VikramSingh-hu2hs
      @VikramSingh-hu2hs 4 роки тому +1

      May be you did something wrong bcoz it's working for me perfectly..

    • @harvardadventure6603
      @harvardadventure6603 4 роки тому

      I think it is because it is in the if (event.type == pygame.KEYDOWN) loop

    • @E13ctr09
      @E13ctr09 4 роки тому

      dude did you resolved the problem ???? if so tell me too .... i'm stuck with this

    • @harvardadventure6603
      @harvardadventure6603 4 роки тому

      My earlier comment was the solution, check the indents because it might be in the wrong loop,in other words just check the number of spaces there is ment to be behind each sentence of code.
      For example:
      While True:
      Print(number)
      Is different from
      While True:
      Print(number)
      So just remember becareful of the spaces

  • @littleharry7977
    @littleharry7977 2 місяці тому

    Tip: you can get rid of the black bars on the collisions by putting = instead of just < or >

  • @Niblik
    @Niblik Рік тому

    I'm having this as a school project, and it's going to be a great project 😊

  • @dynajay4106
    @dynajay4106 4 роки тому

    Ah yes! Classic arrow key controls are one of the first things I ever implement on a game.
    I always have a habit of getting a tad fancier with it.
    Suppose the space between the player x and the edge of the screen are less than the velocity but still greater than 0.
    We have a remainder in there so it is good to use a modulus function and abs to compare the distances.
    if keys[pygame.K_LEFT]:
    if x > 0:
    x -= vel
    elif abs(x - screen_width) % vel != 0:
    x -= (abs(x - screen_width) % vel)
    if keys[pygame.K_RIGHT]:
    if x < screen_width - width:
    x += vel
    elif x % vel != 0:
    x += (x % vel)
    if keys[pygame.K_UP]:
    if y > 0:
    y -= vel
    elif abs(y - screen_height) % vel != 0:
    y -= abs(y - screen_height) % vel
    if keys[pygame.K_DOWN]:
    if y < screen_height - height:
    y += vel
    elif y % vel != 0:
    y += (y % vel)
    I'm sure it can be refactored but that is how I did it. ^_^

    • @zutru
      @zutru 4 роки тому

      Is this 100% correct? Because suppose that x = 1 and vel = 5, now if I press LEFT, the reactangle will go off-screen because you only checked "if x > 0" and not "if x > vel". Similar with the other directions.

  • @saptarsibandyopadhyay7047
    @saptarsibandyopadhyay7047 4 роки тому

    I was on my way to uninstall my sound drivers after hearing random noises.Comment section saved me.Awesome video though

  • @gameryusic825
    @gameryusic825 4 роки тому

    Quick small tip which is probaly obvious but if you change the width and height to 1920 by 1080 and tweak some other lines of codes you will be able to "play" your "game" in full screen.

  • @alexismandelias
    @alexismandelias 4 роки тому +3

    8:21 I died a little inside with this physics explanation of a jump

    • @beneze3286
      @beneze3286 4 роки тому

      Yep. Was getting very mad at his explanation of a parabola.
      "you get faster as you jump up higher" - no you don't
      "you have, like, a hang time" - mmm, I mean it's instantaneous
      "[at the top] a position with zero acceleration" - nope, not that either, it has zero (y-component of) velocity
      *sigh*

    • @beneze3286
      @beneze3286 4 роки тому

      "hope that makes sense" - bish I lost brain cells

  • @ninjoushinja9478
    @ninjoushinja9478 7 місяців тому +1

    Is there a way to make that rectangle "double-jump"? Thank you in advance if u noticed this...

  • @pic01
    @pic01 3 роки тому

    Thanks again Tim for this video, wouldn't it be right to not use "- vel" when preventing the rectangle to move out of the screen (down and right)
    Refer to 6:30

  • @ashishneupane8181
    @ashishneupane8181 5 років тому +1

    that space is due to velocity....... while ending at the screen boundary....... can be corrected as:
    if keys[pygame.K_LEFT] and x > 0:
    x = x-velocity
    if keys[pygame.K_RIGHT] and x < (300 - rect_width):
    x = x + velocity
    if keys[pygame.K_UP] and y > 0:
    y -= velocity
    if keys[pygame.K_DOWN] and y < (200 - rect_height):
    y += velocity

  • @viktorstefanov4848
    @viktorstefanov4848 4 роки тому

    Could someone clarify to me why do we use the neg variable at 13:30. If i am not wrong, y will go up and then go back because of the negative jumpCount? I even did the math with a calculator and was correct but the program shows otherwise.

  • @bobquinney5613
    @bobquinney5613 4 роки тому

    width 800 on the x < 760 < 0 height 600 on the y > 0 < 540

  • @vicbelleton6729
    @vicbelleton6729 6 років тому +4

    Nice jump ! :) I already love that game !

  • @ucvan6998
    @ucvan6998 3 роки тому +1

    hi Tim, I stuck in Jump, could you please do some math in there. hope you receive

  • @VikramSingh-hu2hs
    @VikramSingh-hu2hs 4 роки тому +1

    Jumping part is a bit hard... But trying to understand. Nice tutorial btw 😇

  • @helkat9876
    @helkat9876 6 років тому

    oh shiiit you made writting boundaries *so simple* ! I remember when i first wrote my boundaries and it was like 20 (or more) lines of code, and i was reaaally happy that i diud that, because i found it pretty difficult .

  • @YouthWaster
    @YouthWaster 2 місяці тому

    Your light mode burned my eyes and heart both T-T
    Thanks anyways 🙏

  • @suhrudhsarathy1263
    @suhrudhsarathy1263 5 років тому +1

    instead of the quadratic you could have used an odd exponent to produce the same effect in the jump say like 3 and then multiply it by 0.1 or something like that

  • @shaneofgames3825
    @shaneofgames3825 5 років тому

    pygame works very slowly on mac presumably due to resolution conflics, if anyone is having issues when you set the display options do the following:
    pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
    note:
    you won't be able to hit the x icon to exit the program, hold shift + ctr + opt + esc for 1-3 seconds
    put a key command along with the player controls to exit easily

  • @labibmimon2668
    @labibmimon2668 4 роки тому +1

    i wrote this code but my character is jumping on the first click but it is not working if i click spacebar for the second time or more. how can i fix this problem ?

  • @linkgamer2481
    @linkgamer2481 3 роки тому +1

    I'm Sorry that this is such a late comment, I just found this tutorial a while ago, so:
    Do you have any videos on the formula you used for jumping?
    If so, Please leave the link down below. Thanks!

  • @rojadsorum9515
    @rojadsorum9515 4 роки тому +1

    is there another tutorial series explaining this

  • @jasonjoubert5003
    @jasonjoubert5003 4 роки тому +1

    At 2:30 i almost died thought my phone was being hacked or siri turned evil when i heard that 😂

  • @rawrs6488
    @rawrs6488 4 роки тому +1

    For anyone from ep1 wondering why their jump/game is laggy, it's cos pygame.time.delay is still 100. Tim uses delay 50 in this.

    • @fathomgaming25
      @fathomgaming25 2 роки тому

      Thank you, I changed it to 50 and it's a lot smoother now!

  • @waterfallbw
    @waterfallbw 5 років тому +4

    Hey, I noticed that the rectangle moves smooth in the video, but the rectangle in my code moves in noticable increments... and not very smooth...
    Did anyone else have this problem??

    • @cagatayyilmaz2098
      @cagatayyilmaz2098 5 років тому

      yes I have that too.

    • @VikramSingh-hu2hs
      @VikramSingh-hu2hs 4 роки тому +1

      No, I don't.... It's working for me pretty good

    • @lint5678
      @lint5678 4 роки тому +2

      If anybody is still wondering how to solve this, setting " pygame.time.delay( ) " to 34 will make the program MUCH smoother(you might still have it at 100). Apparently setting it to 34 will give you 30 fps, but you can just delete the line and it doent make a difference.

  • @ReynoldJudithia
    @ReynoldJudithia 4 роки тому +2

    Hello Tim! Love your tutorial.
    Btw I want to ask about what if I want to give time to 'recharge' the jump?
    Like regeneration time. What is the function?
    Thank you very much. Have a nice day!
    Edit: Words

  • @avisinha9331
    @avisinha9331 2 роки тому +1

    Rather than using neg as a positive or negative variable, why not use abs(). Absolute function will return the absolute value of the answer that is always positive

  • @dannyyeoh2340
    @dannyyeoh2340 5 років тому

    Thanks for this pygame code Tim. It is quite complicated especially the else and indent part got to get it correct or the window will not show. But at last get the jump part working after examining how the code works.

  • @techmech7196
    @techmech7196 3 роки тому

    Thank u so much it really helped a lot ,it's a best pygame turtorial.

  • @ramadeviyendluri8742
    @ramadeviyendluri8742 3 роки тому

    11:52 what was that windows notif beep?

  • @Kuba-xw1mw
    @Kuba-xw1mw 27 днів тому

    A simpler way to do it is by having a "SpeedY" variable and suybtracting it by 1 every frame, and changing the Y position by SpeedY. Jumping would just be setting the SpeedY variable to something positive, and speed y can be set to 0 on vertical collisions to not move any further. However, I never tried it with python, so I'm not sure if it works well here

  • @skmm1102sk
    @skmm1102sk 5 років тому

    I like this video. you explain your game in a compact and interesting way! well done! thank you

  • @atharva-theexpert4714
    @atharva-theexpert4714 3 роки тому +1

    u were recording at midnight?!

  • @jaden8611
    @jaden8611 5 років тому +5

    Nobody:
    Not a soul:
    His Computer: POWERING OFF

  • @tolulopeadekemiolufiranye7497
    @tolulopeadekemiolufiranye7497 3 роки тому

    Your videos are amazing now I can make my games

  • @sayedkamaluddinhashimy8182
    @sayedkamaluddinhashimy8182 2 роки тому

    what was the sound that says powering off 02:36

  • @legend644
    @legend644 4 роки тому

    There's a bug. Set the velocity really high, see what happens? The problem is that if the game calculates you are close to the border it just stops moving, the way to fix this is instead of stating it like this
    if keys[pygame.K_RIGHT] and x < screenWidth - width - vel:
    x += vel
    state it like this
    if keys[pygame.K_RIGHT]:
    x += vel
    if x > screenWidth - width:
    x = screenWidth - width
    because now, instead of detecting that an extra full velocity move would go over and just stop moving, you just clamp the amount it moves
    I am not here to mock you or anything, I am just kind of a code geek, I know 4 other languages and have Unity experience, so I have a good eye for bugs. I really love these videos because they give me knowledge about how the pygame namespace works in programming a game.
    PS I haven't watched the rest of the series yet so if you fixed this issue already I don't know and I'm sorry if you did and I commented this.

  • @grant.elliott
    @grant.elliott 5 років тому +2

    I'm still not able to hold down the arrow keys; I have to press every time. Any help?

  • @mr.goldenarmy
    @mr.goldenarmy Місяць тому

    Loved the video and was also tricked by Powering off sound 🤣 had to check my airpods 😂

  • @biggdek2871
    @biggdek2871 27 днів тому

    I wanted to ask if we could do a simple if statement where we check if the x and y positions are greater than or less than the screen dimensions. For example:
    if bird_x > 1920 or bird_x < 0 or bird_y > 1000 or bird_y < 0:
    bird_x = 100
    bird_y = 500
    This basically just resets the sprite back to its original position

  •  5 років тому +5

    Wasn't really doing much youtube at the time 0:32
    youtube yells at you after you said that.

  • @hamadaag5659
    @hamadaag5659 5 років тому

    i solved the black bar problem by subtracting the rectangle's width from the screen width and did the same thing with height, here is that code:
    keys = pygame.key.get_pressed()
    if keys [pygame.K_LEFT] and x > vel-5:
    x -= vel
    if keys[pygame.K_RIGHT] and x < screen_width - char_width:
    x += vel
    if keys[pygame.K_UP] and y > vel-5:
    y -= vel
    if keys[pygame.K_DOWN] and y < screen_height - char_height:
    y += vel