Pygame (Python Game Development) Tutorial - 17 - Eating the Apple

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

For the random number, you could also just do random.randrange(0, 400, 10) which will pick a number within 0 and 400 and stepping by 10

paradoxdotexe
Автор

To be consistent with the block_size variable, you could have something as follows:

randAppleY = random.randrange(0, display_height - block_size, block_size)

rafaelnegron
Автор

hy Herison ! There is just a so much easier way of making the apple appear on a place that is a multiple of ten :
you just use RANDOM.RANDRANGE(0, DISPLAY_WIDTH - APPLE_X, 10).
just in case you didnt know, there is a third argument in the randrange function - the third argument is the INCREMENT - this makes sure you always get places that are multiple of ten .

MrDadCrazy
Автор

Instead of the rounding thing, which I also thought of first, I changed it to this:
Randrange has actually a third parameter called step.
So if you do:
randAppleX = random.randrange(0, display_width, block_size, block_size)
This will only return numbers from 0 to display_with in steps of 10.
So it will always put the apple on the same grid as the snake.

kingoftheperforations
Автор

Or you could just: randAppleX = random.randrange(0, gamewidth - boxDimensions, 10)

Seytonic
Автор

I am working on a pong game and these tutorials are very helpful. Thank you, Bucky Roberts!

snoop
Автор

Uploaded on my birthday :D
Great video by the way!! :D

MrKiller
Автор

Also, you could use floor division instead of round. (x//10)*10. That way you keep your value as an integer.

JohnSmith-bjuc
Автор

I wonder how much lines were used for writing the code

XOPGAMINGX
Автор

I just put /2 after the randomx and the randomy

Haru-pcsm
Автор

for the apples coordinates you could also make the appleX and appleY into 1 list, (apple for exampe) and then make the two parts of that list a randrange from between Screen_height and screen_width, i have slightly different code from yours but my screen heght is 400 and width is 700, and i have done some more advanced things to clean up my code a bit better, but basicall the apples location could be summed up in on line of code: apple = [random.randrange(0, 390, 10), random.randrange(0, 690, 10)] and this would save a bit of code and processing power(not a whole lot but maybe if ur on a nokia or something)

dillondriskill
Автор

Hey I use floor() instead by importing math in python which is pre installed

ttzky_
Автор

Can't you just make the randrange step by the block size

kingmelonhead
Автор

So in some versions of snake I found that if you (for example) hold the up arrow key then press the left arrow key it disregards the first cmd and focuses on the newest only and makes the snake only move left. I figured out how to code it. 

Example:

if event.key == pygame.K_LEFT:
    if lead_y != 0:
        lead_x_change = -block_size
        lead_y_change = 0
    else:
        lead_x_change = -block_size

Hope that helps :)

JohnSmith-bjuc
Автор

If you press after game over c, is the apple in the same location!!!

vanallesennogwat
Автор

I like using python's oop features 'cause it looks slightly more readable, at least that's what I think...

XOPGAMINGX
Автор

I used the distance formulae to detect the collision

shaharyarahmed
Автор

Hi! Wouldnt be easier just add '10' as step to our randrange?

IlmoOtt
Автор

.. . . .. . .. . Episode 17. How many letters does Python have in it? 6. Whats 17-6? 11. How many letters does snake have in it? 5. 11-5 = 6, and if you include that you need a double equals sign (==) for if statements, whats 6-double(2) = 4. HALF LIFE 4 CONFIRMED!±!@#E@!#!$R Keep up the great vids m8

bufferoverload
Автор

I found this also works:

randAppleX = random.randrange(0, (display_width-snakeSize)/snakeSize)*snakeSize

jacksonvanderkooy
welcome to shbcf.ru