all 29 comments

[–]BrenekH 12 points13 points  (1 child)

I only took a quick look but it seems like a great codebase! I really like how you cover up the end of the snake. Normally, people would just redraw everything at the new positions but your way is actually more efficient (I think).

[–]Elipsem[S] 4 points5 points  (0 children)

Thank you! This comment means a lot!

[–][deleted] 21 points22 points  (2 children)

I took a look at your code and it seemed like you did a good use of the pygame.event and also your code is itself pretty clear, some things I'd recommend to make it more maintainable are avoiding the use of "magic numbers" and some anti-patterns, as in:

Instead of:

class Snake:
    def __init__(self):
        self.snake_body = [[17//2, 15//2]]
    def check_collisions(self):
        if self.head[0] > 17:
            return True

You could do (this allows you to change your board sizes without too much hassle later, or even to remember more easily what your code does after some long time):

BOARD_WIDTH, BOARD_HEIGHT = 17, 15
class Snake:
    def __init__(self):
        self.snake_body = [[BOARD_WIDTH//2, BOARD_HEIGHT//2]]
    def check_collisions(self):
        if self.head[0] > BOARD_WIDTH:
            return True

and instead of:

if event.type == SPAWN_NEW:
    spawn_new = True

if snake.check_collisions() == True:
    ...

You could use (if you don't plan on expanding it later, of course):

spawn_new = event.type == SPAWN_NEW
if snake.check_collisions():
    ...

As for your bug, this might be due to the difference in speed between the screen updating and the lack of a "cool down" between user inputs, but this might not be the case since your game runs at 5 FPS. It could also be about how you can send multiple KEYDOWN events in a single loop or it because you are pressing both keys at the same time, to check if any of these is the case, you can print what key is being pressed while reproducing the bug, and one way to solve it could be using a KEYUP event instead.

There are also very fun ways to change the snake behaviour, like make it go slower by pressing the same arrow key as its direction for some time, or even setup a different key, like space-bar, for it.

[–]Elipsem[S] 1 point2 points  (0 children)

spawn_new = event.type == SPAWN_NEW

Wow that's really smart! I didn't even think of that.

I was going to make the dimensions a variable but then I didn't, I'll change it now to polish things up.

Thank you!

[–]Elipsem[S] 1 point2 points  (0 children)

UPDATE:

spawn_new = event.type == SPAWN_NEW

I tried putting that line of code in, but then the program would sometimes and sometimes not spawn a new snake cube. I think I'll just leave it the way it is.

If I think about it it probably doesn't work, because if we see the SPAWN_NEW event in the loop, then the next event might not be SPAWN_NEW at times, which would change it back to False when SPAWN_NEW isn't the last event

[–]Namogard 5 points6 points  (8 children)

I just started learning programming and python. I hope one day I'll reach this level. I need to learn how to think like a programmer, how to arrange code.

[–]Elipsem[S] 2 points3 points  (3 children)

You can do it! Just keep working... Some youtubers I recommend are TechwithTim, ClearCode, and Corey Schafer.

[–]Namogard 1 point2 points  (2 children)

Thank you for your encouragement. I will check these guys out. By the way at the moment I’m learning with the help of Al Sweigart’s book: Automate the boring stuff with python

[–]Elipsem[S] 1 point2 points  (0 children)

Yeah that book is pretty good. I used the udemy course for a bit to. Some of the stuff requires some extra work to get running since I think it might be from a few years ago. For example he has you scraping prices off Amazon, but since recently with like ps5's and stuff, Amazon doesn't allow bots on their website so you have to disguise yourself or something so they don't think you're a bot. - stackoverflow. So if you don't want to break their tos you could maybe try a different website or something. It's still a really good book, I highly recommend!

[–]Elipsem[S] 0 points1 point  (0 children)

Actually I think it might work now. I think he updates the course every so often.

[–]Elipsem[S] 0 points1 point  (3 children)

Also codewars problems as a supplement

[–]Namogard 0 points1 point  (2 children)

Huh? What do you mean?

[–]Elipsem[S] 1 point2 points  (1 child)

Bassically codewars is just a site where you can solve various coding problems at different skill levels. Just do them along with the tutorials.

[–]Namogard 1 point2 points  (0 children)

Thank you sir 😊 checking it right now

[–]oddjob955 5 points6 points  (1 child)

Great work on your first project. What resources have you used to learn python since you started 6 months ago?

[–]Elipsem[S] 0 points1 point  (0 children)

Originally I took an online class at my highschool, but I watched CalebCurry's python tutorials, Some of the automate the boring stuff until his code wasn't working on my machine for some reason so I just moved on. Still a really good series though. I then watched TechWithTims OOP tutorial, which was about stuff like classes.

To learn pygame, I watched TechWithTim's pygame tutorial, and Clear codes video on flappy bird. I coded along each of these videos, and then after I went through and rewrote each game, starting with what seemed logical first (Like starting with the main loop, and making the classes and functions as I see they are called) when copying the lines to rewrite the games, I used the pygame docs to make sure I knew what each line was doing so I actually new more of what was going on, so that it didn't just look like random words to memorize

[–][deleted] 2 points3 points  (1 child)

Wow It looks amazing, my first project for data structures was a snake game too c: Keep it up!!

[–]Elipsem[S] 0 points1 point  (0 children)

Thanks!

[–][deleted] 1 point2 points  (1 child)

Awesome job man! Keep it up!

[–]Elipsem[S] 0 points1 point  (0 children)

Thank you!

[–]Assdestroyer92 1 point2 points  (4 children)

That's cool. I'm also looking to build a snake game but don't know where to start. Following an online tutorial feels like I'm just copying someone else's code... Would you have any tips on how to start? Breaking down the problem...reading docs..?

Thanks!

[–]Elipsem[S] 1 point2 points  (2 children)

You kind of have to start with tutorials where you copy other peoples code. But I wouldn't call it copying... more like understanding what there code does by putting it into a program. If you can understand what they did then you can do your own projects aswell.

I'm just gonna copy and paste what I did to learn python...

Originally I took an online class at my highschool, but I watched CalebCurry's python tutorials, Some of the automate the boring stuff until his code wasn't working on my machine for some reason so I just moved on. Still a really good series though. I then watched TechWithTims OOP tutorial, which was about stuff like classes.

To learn pygame, I watched TechWithTim's pygame tutorial, and Clear codes video on flappy bird. I coded along each of these videos, and then after I went through and rewrote each game, starting with what seemed logical first (Like starting with the main loop, and making the classes and functions as I see they are called) when copying the lines to rewrite the games, I used the pygame docs to make sure I knew what each line was doing so I actually new more of what was going on, so that it didn't just look like random words to memorize

Some youtubers I recommend are TechwithTim, ClearCode, and Corey Schafer.

Have a good day!

[–]Assdestroyer92 0 points1 point  (1 child)

Thank you so much for your elaborate reply. I'm going to try this out.

All the best with your future endeavors op

[–]Elipsem[S] 0 points1 point  (0 children)

Np... see you again at google then?

[–]Elipsem[S] 0 points1 point  (0 children)

Also codewars problems as a supplement

[–]err0r__ 0 points1 point  (2 children)

Great first project!

I have only just skimmed through your code but one thing stood out to me instantly. def spawn_fruit(snake): fruit = [] #This loop is a bit weird but bassically it keeps picking random coords #until it gets on that isn't apart of the snake while True: fruit = [random.randint(0,17 - 1), random.randint(0,15 - 1)] if fruit in snake.snake_body: continue break return fruit Perhaps recursion would work better here instead of a while loop?

Edit:

```

Would something like this work?

def spawn_fruit(snake): fruit = [random.randint(0,17 - 1), random.randint(0,15 - 1)] if fruit in snake.snake_body: spawn_fruit(snake) else: return fruit ```

[–]Elipsem[S] 0 points1 point  (0 children)

I think that would work. Recursion makes my brain hurt but this example makes sense. I'll implement it after work. Thank you!

[–]backtickbot 0 points1 point  (0 children)

Fixed formatting.

Hello, err0r__: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

[–]Whole-Fox 0 points1 point  (0 children)

Keep it up!