Issue with “list has no bottom”. Python

Okay so I’m creating snake game as my CS project this semester and oh how many issues I’m constantly encountering… Althought there’s this one that I think will haunt me today and not allow me to sleep at night, I’m talking exactly about this: output
My code is a bit diffrent from most snake games that ppl are making using classes but my bright butt thought I’d make it without them and now I’m paying for it.

okay so I tried what could in my mind allow me to overcome this difficultity but all to no avail(mind that I’m not the brightest in CS but I really tried my best). I tried changing snake_body into 2 elemental list

snake_body=[[100, 100], [120, 100]]

and when it didn’t work I changed it into

something a bit diffrent

snake_body = pygame.rect.Rect([0, 0, screen_width - 2, screen_width - 2])

but none seems to be working. When I attempt making borders on sides of pop-up window it constantly whos up that my list out_of_bound has no bottom? I have no idea what is going on. I’m sending my whole code if you want to scroll trought it and find solution to that error bc not me nor any other advices I’ve been looking for in the internet are able to help me

PS:Don’t mind the comments 1. Some of them are for me to know what is going on with the code and are written in my native but all working parts of code are in english so sorry if it annoys you
2. #-ed lines are diffrent solutions I tried out to make the code work out. I’m going insane bc none of them are working.

import pygame
import sys
import random

# Inicjalizacja Pygame
pygame.init()

#ustawienia okna
screen_width = 800
screen_height = 600
background_color = (0, 50, 100)
window = pygame.display.set_mode((screen_width, screen_height))

def random_starting_position(screen_width):
    """Generate a random starting position within the screen."""
    start = screen_width // 2
    stop = screen_width
    return [random.randrange(start, stop), random.randrange(start, stop)]

position = random_starting_position(screen_width)

# Ustawienia węża
snake_length = 2  # Początkowa długość węża
snake_speed = 0.2  # Ilość pikseli o jakie porusza się wąż
#snake_body = [[100, 100], [120, 100]]  # Początkowa pozycja węża
#snake_body = pygame.rect.Rect([0, 0, screen_width - 2, screen_width - 2])
#snake_body.center = random_starting_position(screen_width)
#snake = [snake_body.copy()]
#snake_direction = (0, 0)
#snake_length = 1

#jabłko
# Apple setup
#apple = pygame.rect.Rect([0, 0, screen_width - 2, screen_height - 2])
#apple.center = random_starting_position(screen_width)
snake_body = [[100, 50],
              [90, 50],
              [80, 50],

              ]

# fruit position
fruit_position = [random.randrange(1, (screen_width // 10)) * 10,
                  random.randrange(1, (screen_height // 10)) * 10]

fruit_spawn = True

def is_out_of_bound(rect):
    """Check if a rectangle is out of the screen bounds."""
    return rect.bottom > screen_width or rect.top < 0 or rect.right > screen_width or rect.left < 0

for rect in snake_body:
    if is_out_of_bound(rect):
        snake_body.pop()

if is_out_of_bound(snake_body) or snake_body.collidelist(snake[:-1]) != -1:
    SOUND = True
snake_length = 1
apple.center = random_starting_position()
snake_body.center = random_starting_position()
snake = [snake_body.copy()]
'''
# Funkcja rysująca ściany w rogach okna gry
def draw_walls():
    wall_thickness = 20
    wall_color = (255, 255, 255)  # Kolor ścian

    # Lewa ściana
    pygame.draw.rect(window, wall_color, [0, 0, wall_thickness, screen_height])
    # Górna ściana
    pygame.draw.rect(window, wall_color, [0, 0, screen_width, wall_thickness])
    # Prawa ściana
    pygame.draw.rect(window, wall_color, [screen_width - wall_thickness, 0, wall_thickness, screen_height])
    # Dolna ściana
    pygame.draw.rect(window, wall_color, [0, screen_height - wall_thickness, screen_width, wall_thickness])
'''
# Funkcja główna która zawiera główną logikę gry
def main():
    global snake_direction  # Deklaracja zmiennej snake_direction jako globalnej
    snake_direction = 'RIGHT'  # Początkowy kierunek węża

    # Utworzenie okna
    window = pygame.display.set_mode((screen_width, screen_height))
    pygame.display.set_caption("Snake game")

    # Główna pętla gry
    while True:
        # Obsługa zdarzeń
        for event in pygame.event.get():
            if event.type == pygame.QUIT:   # Wyłączanie ekranu gry za pomocą X
                pygame.quit()
                sys.exit()

        # Poruszanie węża
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            snake_direction = 'LEFT'
        if keys[pygame.K_RIGHT]:
            snake_direction = 'RIGHT'
        if keys[pygame.K_UP]:
            snake_direction = 'UP'
        if keys[pygame.K_DOWN]:
            snake_direction = 'DOWN'

        # Aktualizacja pozycji węża
        if snake_direction == 'LEFT':
            new_head = [snake_body[0][0] - snake_speed, snake_body[0][1]]
        elif snake_direction == 'RIGHT':
            new_head = [snake_body[0][0] + snake_speed, snake_body[0][1]]
        elif snake_direction == 'UP':
            new_head = [snake_body[0][0], snake_body[0][1] - snake_speed]
        elif snake_direction == 'DOWN':
            new_head = [snake_body[0][0], snake_body[0][1] + snake_speed]

        # Dodanie nowej głowy węża
        snake_body.insert(0, new_head)

        # Skrócenie węża, jeśli jest za długi
        if len(snake_body) > snake_length:
            snake_body.pop()

            # Sprawdzenie kolizji z granicami planszy
            if (snake_body[0][0] < 0 or
                    snake_body[0][0] >= screen_width or
                    snake_body[0][1] < 0 or
                    snake_body[0][1] >= screen_height):
                snake_body.pop()


               # # Zablokowanie ruchu węża poza granicami planszy
                #if snake_body[0][0] < 0:
                 #   snake_body[0][0] = 0
                #elif snake_body[0][0] >= screen_width:
                 #   snake_body[0][0] = screen_width - snake_size
                #if snake_body[0][1] < 0:
                 #   snake_body[0][1] = 0
                #elif snake_body[0][1] >= screen_height:
                 #   snake_body[0][1] = screen_height - snake_size

        # Rysowanie tła
        window.fill(background_color)

        # Rysowanie ścian
        draw_walls()

        # Rysowanie węża
        for block in snake_body:
            pygame.draw.rect(window, (0, 100, 0), [block[0], block[1], 20, 20])

        # Aktualizacja ekranu
        pygame.display.flip()

# Uruchomienie głównej funkcji
if __name__ == "__main__":
    main()

New contributor

Luccio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật