Python Forum
[PyGame] Made my first Python program: Snake. Please help me improve it
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Made my first Python program: Snake. Please help me improve it
#4
(Feb-18-2019, 09:14 PM)andrerocha1998 Wrote: I also check the Rect functions. My understanding is that for the Rect collision functions to work, snake.body should be a string of Rects, instead of center.points. This would mean I'd have to change the whole generation and movement functions.
Rect have center attributes.
Quote:x,y
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
size, width, height
w,h
You wouldnt really have to do it for food as the head of the snake would be the only thing colliding first with food. So you would only have to worry about the snake intersecting on itself. Ive never made a snake game, so i am not sure how the best approach would be. If its a small game, the way it works is good enough.

(Feb-18-2019, 09:14 PM)andrerocha1998 Wrote: The controls seem unresponsive at times because of the delay, however. This didn't happen when I controlled the speed via FPS. Any way to fix this?
I do not know what you mean? When i run it it seems fine. Can you explain?

(Feb-18-2019, 09:14 PM)andrerocha1998 Wrote: On an unrelated note, from the documentation you linked, sometimes classes are defined with (object), (something) or (). (something) I understand is to make a subclass from something. However, I was under the impression that (object) and () worked the same way.
object is for python2.x to use new classes, while python3.x automatically uses new classes. Python2.x is almost dead now anyways, so there is not much point anymore except for habit. Anything within these parenthesis is that class' super class. In the example of making a state machine...States is the super class and Menu and Game are the sub classes. All of States attributes are passes to each subclass. And anything that needs to remain between states can be in the super class as well. There is more detailed info on multiple parts class tutorials here.

(Feb-18-2019, 09:14 PM)andrerocha1998 Wrote: What is considered spaghetti code by the way?
Your remake is a little cleaner. It drove me nuts that you had 3 separate functions to handle movement in your first code.
Recommended Tutorials:
Reply


Messages In This Thread
RE: Made my first Python program: Snake. Please help me improve it - by metulburr - Feb-18-2019, 10:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to add segments to the snake body blacklight 1 2,954 Sep-13-2023, 07:33 AM
Last Post: angelabarrios
  [PyGame] Snake game: how to get an instance for my snake's body multiple times? hajebazil 2 2,209 Jan-30-2022, 04:58 AM
Last Post: hajebazil
  help with snake game blacklight 3 2,656 Jul-30-2020, 01:13 AM
Last Post: nilamo
  Snake Game - obstacle problem Samira 3 5,725 Oct-31-2019, 02:58 PM
Last Post: Samira
  Creating Snake game in Turtle Shadower 1 8,695 Feb-11-2019, 07:00 PM
Last Post: woooee
  [PyGame] Basic Snake game (using OOP) PyAlex 1 12,630 Sep-10-2018, 09:02 PM
Last Post: Mekire
  [PyGame] Snake not changing directions in Snake Game Bjdamaster 4 5,031 Aug-13-2018, 05:09 AM
Last Post: Bjdamaster
  [PyGame] Snake controls not working jakegold98 5 6,523 Dec-12-2017, 01:45 AM
Last Post: Windspar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020