0

I have created a snake game in python IDLE, however there is an attribute error on line 24 and I'm not quite sure what that means. Does anyone know? The game was starting to work as it showed my rectangle and the screen however it is just a black window. Not sure wherer i have gone wrong.... This is for my a - level coursework.

from typing import List, Tuple
import pygame

FPS = 10
WINDOW_WIDTH = 900    #how big the window is going to be
WINDOW_HEIGHT = 900


class Snake(object):
    WIDTH = 40              #how big ther snakes body starts off
    HEIGHT = 40

    def __init__(self):
        self.pos_a = 1
        self.pos_b = 1   #position it starts in

        self.vel_a = 0
        self.vel_b =0

    def update(self, events: List[pygame.event.Event]) -> None:
        self.pos_a += self.vel_a
        self.pos_a += self.vel_b

        self.vel_a, self.vel_b = self.handle_mov(events)

    def draw(self,screen: pygame.Surface):
        pygame.draw.rect(               #what colour the snake is 
            screen,
            pygame.color.Color("purple"),
            (
               self.pos_a,
               self.pos_b,
               self.WIDTH,                    #spelt it colour 
               self.HEIGHT,
            ), 0
        )

        def handle_mov(self, events: List[pygame.event.Event]) -> Tuple[int, int]:
            travelling = (self.vel_a, self.vel_b)

            for ev in events:
                if ev.type == pygame.KEYDOWN:
                    if ev.key == pygame.K_UP:
                        travelling = (0, -1)
                    elif ev.key == pygame.K_DOWN:
                        travelling = (0, 1)
                    elif ev.key == pygame.K_LEFT:
                        travelling = (-1, 0)
                    elif ev.key == pygame.K_RIGHT:
                        travelling = (1, 0) 


            return travelling

def run() -> None:
    pygame.init()
    screen = pygame.display.set_mode(
        (WINDOW_WIDTH, WINDOW_HEIGHT), #syntax error on height
        pygame.HWSURFACE

    )
    snakegame_loop(screen)


def snakegame_loop(screen: pygame.Surface) -> None:
    snake = Snake()
    clock = pygame.time.Clock()

    while True:
        events = pygame.event.get()
        handle_quit(events)

        screen.fill(pygame.color.Color("pink"))

        snake.update(events)
        snake.draw(screen)           #the screen color

        pygame.display.update()    #updating all the adjustments added
        clock.tick(FPS)


def handle_quit(events: List[pygame.event.Event]) -> None:
    for ev in events:
        if ev.type == pygame.QUIT:
            exit(0)
        if ev.type == pygame.KEYDOWN and ev.key == pygame.K_ESCAPE:
            exit(0)



run()
Tamzin
  • 1
  • 2
  • 6
    Please post a **minimal** working example and read "How to ask". We are not here to solve your homework problems. – buhtz May 13 '18 at 10:22
  • 1
    You have to dedent the `handle_mov` method, because it's currently only defined inside of the `draw` method. – skrx May 13 '18 at 10:25
  • Maybe it's not minimal, but at least it's working. I don't get any `AttributeError`. Please add the full traceback. – Jeronimo May 13 '18 at 10:26
  • Can you post the **exact** error message and the line throwing the error? – Nico Haase May 13 '18 at 10:43
  • the error message is - Traceback (most recent call last): File "C:\Users\tamzin\Downloads\Python3\PYGAME NEW.py", line 91, in run() File "C:\Users\tamzin\Downloads\Python3\PYGAME NEW.py", line 62, in run snakegame_loop(screen) File "C:\Users\tamzin\Downloads\Python3\PYGAME NEW.py", line 75, in snakegame_loop snake.update(events) File "C:\Users\tamzin\Downloads\Python3\PYGAME NEW.py", line 24, in update self.vel_a, self.vel_b = self.handle_mov(events) AttributeError: 'Snake' object has no attribute 'handle_mov' – Tamzin May 13 '18 at 11:05
  • 1
    Ok so this was indeed no copy&paste effect and @skrx is right, you have to dedent `handle_mov` one level. – Jeronimo May 13 '18 at 11:12
  • what do you meanthis was no copy and paste effect? @jeronimo – Tamzin May 13 '18 at 11:34
  • I saw the wrong indentation of that method, too, and thought it was just due to how you copy/pasted and formatted your code. But looking at your error message, that's exactly the problem. – Jeronimo May 14 '18 at 08:02

0 Answers0