Pygame (Python Game Development) Tutorial - 13 - Fixing the Hardcoding

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

Hey....You have some serious talent in making difficult things easy....
Keep up the good work!!!....You earned a sub!....

its_maalik
Автор

This tutorial may just be the best thing that's happened to me in regards to learning python and programming. Automate the Boring Stuff is amazing, and without first starting it I'd probably be a bit confused trying to follow this, but this tutorial is exactly what I needed; to get my nose out of a book and actually start doing something! I'm realizing how great games are for learning, because they give you a quick, visual reward as you go. Spices up the endless walls of text I've been consuming.

As far as this particular video, it seems to me like hardcoding just handicaps a program, keeping it from being able to run on multiple devices, etc. without a ton of unnecessary work. Nowadays I'd imagine people want their programs to be as flexible and portable as possible. It's awesome how he explains it all and it really opened my eyes up to where I've almost got all the pieces together now.

padger
Автор

Out of all the things I've learned in my short time programming, knowing not to hard code everything has probably made the biggest difference. My programs just seem to make more sense and they are SO much easier to maintain. Thanks for making note of that fact in your tutorials ;).

bloodface
Автор

I was just going to comment about this on your last video! I was going to say remember to use a constant at the top for your FPS and use your width (and height) variable from pygame in your collision detection

Coolfolder
Автор

thanks for your lecture, i studied a lot from your lectures

f.j.bradman
Автор

great tutorials man!!!thank you very much!!

yehonatanfranco
Автор

you have done a great job thanks for teaching you made my fundamentals i already tried from some books and i appreciate your work

dilipgupta
Автор

these are so good. you deserve more views!

zjack
Автор

Im also thinking the movement speed could be made a lot smoother. We can have the x and y of the snake lead always be at one of the grid points (grid size is 10 or the snake leads block size) But on frames in between location change. Have the snake move into position in increments of 1 pixel or 2 pixels. The movement would purely be represent in the graphics and wouldn't reflect the actual snake location. Snakes either located at 220, 300 or 230, 300 but not at 221, 300 222, 300 223, 300.

This would require modifying the frame rate to something greater and then just timing snake movement intervals. if snake movement size is say 10 we could double frame rate to 60 and have the snake movement occur in increments of 10 every 2 cycles (the snake speed will now be identical to what it was at 30 FPS) But have the snake graphics update every frame in increments of 5 pixels. (remember snake location is always on the grid and for collision detecting the x and y are always aligned with everything) Point is just a smoother snake movement. Have a separate loop for drawing this motion. More work but just an idea for anyone following this series.

spawn
Автор

I assumed that keeping the block size and movement the same were important for possibiliy collision detecting later on or just keeping apples aligned with the snake. I.E. keeping everything aligned on a grid that was 10 pixels so heres what i did.

blocksize = 10
winx = 800
winy = 600
xr = (winx/2) % blocksize
yr = (winy/2) % blocksize


lead_x = (winx/2) - xr
lead_y = (winy/2) - yr

This makes it so no matter what the window size is set to that your snapped to a 10 pixel wide grid. If the room size is 510 you dont start at 255 it will start you at 250. If the room size is also 519 you will start at 250. when the room size is 520 then it will change and go to 260.

if lead_x < 0 or lead_x > (winx - blocksize) or lead_y < 0 or lead_y > (winy - blocksize):
gameExit = True

This is my code for the boundary i subtract the blocksize (the snake lead pieces size) from the edge of the wall because x and y of the snake are always the top left corner. If my right edge goes past the boundary then guess what its game over. so now edge detection is identical for top left right and bottom.

And if at any point i allow the user to specify a custom room size, it wont be difficult to have it round the users size up so that its in increments of 10 or the block size.

spawn
Автор

If I were you I would use a floor division when dividing lead_x or y. It's just "best practice" as textbooks would say. But great videos man.

JohnSmith-bjuc
Автор

Who's here in 2022 getting this done?!?! Booyah!

jsonify
Автор

After making the variables I noticed that my block or "snake" moved a lot faster and not as smooth as it did before when everything was hard coded in. I am not sure how to fix this because when I change the FPS it goes faster, if I change the block_size, it also goes faster. so I ended making the FPS = 15 and the block_size = 20 to make it at the desired speed. I'm just curios as to why the size of the block changes the speed and why that happens. :)

nathansniegowski
Автор

WIDTH = 800
HEIGHT = 600
size = [WIDTH, HEIGHT]

gameDisplay = pygame.display.set_mode(size)

thmadgod
Автор

I'm curious to know why it is we are dividing by 2 when we write display_width and height. What does dividing by 2 do for us? I'm a little confused, and I don't feel it was explained very well. Any help and explanation with that would be appreciated!

aparkplace
Автор

The 10's under the KEYDOWN event represent velocity, not block size.
Block size is represented in the pygame.draw.rect() as the last two parameters
I chose to disentangle them, renamed the 10's under KEYDOWN to veloctiy.

OrbGoblin
Автор

how do you do the copy and paste thing so quick and without having to highlight stuff?

julianceasar
Автор

Why game exit if boundaries get touched ? try this (sorry bad english)


if lead_x > display_width:
lead_x = 1
if lead_x < 0:
lead_x = display_width-1
if lead_y > display_height:
lead_y = 1
if lead_y < 0:
lead_y = display_height-1

d_o_o_m_e_d
Автор

if lead_x > WIDTH - 11 or lead_x < 1 or lead_y > HEIGHT - 11 or lead_y < 1:

thmadgod
Автор

u haven't fixed the problem of the snake moving right and left in same line, as this is against the rules of the game..

DikshaSharma-tdom