portgram.blogg.se

On screen snake
On screen snake














In addition to another answer I will add these places to improve (I will not repeat all things from that answer, my answer is just an addition): 1. Increasing re-usability.ġ For instance, It may be the project defaults to "", but you need to print out " Hi " with the quotes, so print('" Hi "') may be chosen. This allows for me to import your project and not have it automatically execute the game function at the bottom. This is usually done through the introduction of: if _name_ = "_main_": You should consider adding a main function to your project. I'm not sure why you wrote: # check for collision with wall: Really think about what this comment contributes. The best example is probably: # move snake However, there may be some debate on this.

on screen snake

If you had to write comments I would instead write: # Initialize entities/data for new gameĮven then, I'm not quite satisfied with it, but the moral it to not add redundant comments.

#ON SCREEN SNAKE CODE#

I could see where a case can be made the game loop comments in the case that someone is trying to understand the code without knowledge of game loops, but I would argue the concept of a game loop is so ubiquitous that those comments get in the way. I, personally, would omit all the comments. (You will need to do some further refactoring to get this to run without error.) Unnecessary Comments In addition, there are probably some integer arithmetic tricks you could use to eliminate a lot of the if- elif- else chains. See here for more reasons why you should port your stringy code to enums. Within an enumeration, the members can be compared by identity, and the enumeration itself can be iterated over. EnumsĪn enumeration is a set of symbolic names (members) bound to unique, constant values. Usually a project will stick with either "" or '' unless you have particular reason not to. Or possibly an enum (more on this later) Inconsistent quotations So something like: yellow = (255, 255, 0) PEP 8 recommends that constants be written as "all capital letters with underscores separating words.". # check if food is still on screen and draw it (window, lor,, item, block_size,Ĭollision_with_wall = wall_collision(snake) If pressed or pressed:Įlif pressed or pressed: Text_on_screen = font.render("You scored: " + str(score) + If (s.head bg_width-block_size) or (s.head bg_height-block_size): Snake_rect = pygame.Rect(*snake_.head, block_size, block_size)įood_rect = pygame.Rect(food_target_x, food_target_y, block_size, Self.y = random.randint(0, bg_height // block_size-1) * block_sizeĭef collision(snake_: Snake, food_target_x: int, food_target_y: int) -> int: Self.x = random.randint(0, bg_width // block_size-1) * block_size Self.y = random.randint(0, bg_height//block_size - 1)*block_size Self.x = random.randint(0, bg_width//block_size - 1)*block_size Position: x,y-coordinates pair of food object

on screen snake

State: whether food object is on screen or not New_part =, self.body]ĭef get_body(self) -> List]: If self.direction != left and direc = right:Įlif self.direction != right and direc = left:Įlif self.direction != down and direc = up:Įlif self.direction != up and direc = down: Self.body = ]ĭef change_dir(self, direc: str) -> None: Every snake object consists of a headĭef _init_(self, color: Tuple) -> None: Window = _mode((bg_width, bg_height))įont = ('Times New Roman', 20)

on screen snake

I made it with python 3 using pygame library. I am a complete beginner when it comes to programming and writing games and this is my first ever game.














On screen snake