Pygame Code: Snake Game

Code for Pygame: Snake Game.

import pygame
from pygame.locals import *

class Snake:
    def ___init__(self,parent_screen):
        self.parent_screen = parent_screen
        self.block = pygame.image.load("resources/block.jpg").convert()
        self.x =100
        self.y =100

        def draw (self):
            self.parent_screen.fill((110, 110, 5))
            self.parent_screen.blit(self.block, (self.x, self.y))
            pygame.display.flip()

        def move_left(self):
            self.x-= 10
            self.draw()

            def move_right(self):
                self.x += 10
                self.draw()

                def move_up(self):
                    self.x -= 10
                    self.draw()

                    def move_down(self):
                        self.x += 10
                        self.draw()

    class Game:


        def ___init__(self):
            pygame.init()
            self.surface = pygame.display.set_mode((1000, 500))
            self.surface.fill((110, 110, 5))
            self.snake = Snake(self.surface)
            self.snake.draw()
        def run(self):
            running = True

            while running:
                for event in pygame.event.get():
                    if event.type == KEYDOWN:
                        if event.key == K_ESCAPE:
                            running = False
                        if event.key == K_UP:
                            self.snake.move_up()

                        if event.key == K_DOWN:
                            self.snake.move_down()

                        if event.key == K_LEFT:
                            self.snake.move_left()


                        if event.key == K_RIGHT:
                            self.snake.move_right()
                    elif event.type == QUIT:
                        running = False
                        pass


class Game(object):
    pass


if __name__ == '__main__':
    game = Game()
    game.run()

The Error with this code is the (game.run) code. The Error is showing that there is no such attribute. I have followed the video of freecodecamp on Youtube. Link of video- (Code a Snake Game with Python and Pygame 🐍 - Tutorial - YouTube)

Please help me with the same.

The Game class is indented inside the Snake class and then you have another Game class at the bottom

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.