From e1b5b590727e567c60715cb92281ab26378463b9 Mon Sep 17 00:00:00 2001 From: sayampradhan <112542130+sayampradhan@users.noreply.github.com> Date: Fri, 14 Oct 2022 23:27:03 +0530 Subject: [PATCH] Format Code --- 1_snake_game/4_build_snake_draw_apple.py | 43 ++++++++++++------------ 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/1_snake_game/4_build_snake_draw_apple.py b/1_snake_game/4_build_snake_draw_apple.py index ad6f1d7..611834c 100644 --- a/1_snake_game/4_build_snake_draw_apple.py +++ b/1_snake_game/4_build_snake_draw_apple.py @@ -8,6 +8,7 @@ SIZE = 40 + class Apple: def __init__(self, parent_screen): self.parent_screen = parent_screen @@ -20,45 +21,46 @@ def draw(self): pygame.display.flip() def move(self): - self.x = random.randint(1,25)*SIZE - self.y = random.randint(1,20)*SIZE + self.x = random.randint(1, 25) * SIZE + self.y = random.randint(1, 20) * SIZE + class Snake: def __init__(self, parent_screen, length): self.parent_screen = parent_screen self.image = pygame.image.load("resources/block.jpg").convert() - self.direction = 'down' + self.direction = "down" self.length = length - self.x = [40]*length - self.y = [40]*length + self.x = [40] * length + self.y = [40] * length def move_left(self): - self.direction = 'left' + self.direction = "left" def move_right(self): - self.direction = 'right' + self.direction = "right" def move_up(self): - self.direction = 'up' + self.direction = "up" def move_down(self): - self.direction = 'down' + self.direction = "down" def walk(self): # update body - for i in range(self.length-1,0,-1): - self.x[i] = self.x[i-1] - self.y[i] = self.y[i-1] + for i in range(self.length - 1, 0, -1): + self.x[i] = self.x[i - 1] + self.y[i] = self.y[i - 1] # update head - if self.direction == 'left': + if self.direction == "left": self.x[0] -= SIZE - if self.direction == 'right': + if self.direction == "right": self.x[0] += SIZE - if self.direction == 'up': + if self.direction == "up": self.y[0] -= SIZE - if self.direction == 'down': + if self.direction == "down": self.y[0] += SIZE self.draw() @@ -75,6 +77,7 @@ def increase_length(self): self.x.append(-1) self.y.append(-1) + class Game: def __init__(self): pygame.init() @@ -84,7 +87,6 @@ def __init__(self): self.apple = Apple(self.surface) self.apple.draw() - def play(self): self.snake.walk() self.apple.draw() @@ -115,10 +117,9 @@ def run(self): self.play() - time.sleep(.2) + time.sleep(0.2) -if __name__ == '__main__': + +if __name__ == "__main__": game = Game() game.run() - -