1
votes

I am writing a snake game with pygame but I am just prompted with a black window without any error.

I checked similar problems and they incorrectly used the screen object. However this does not seem to be the case here, since it is only created once?

Any idea what I am doing wrong? Since I am not given any error message I can't figure out why I am only shown a black screen. The key input events work just fine.

import pygame
import random
import sys


# Define colors
GRAY = (240, 240, 245)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (50, 50, 50)

# Field size
width = 400
height = 400



# Calculate the distance between two points
def calculate_distance(x1, x2, y1, y2):
    return (x2 - x1)**2 + (y1 - y2)**2 ** 1/2

# Draws a label showing the current score in the top mid of the window
def draw_score_text(surface, text, font_size, x, y):
    font_name = pygame.font.match_font('arial')
    font = pygame.font.Font(font_name, font_size)
    text_surface = pygame.font.render(text, True, WHITE)
    text_rect = text_surface.get_rect()
    text_rect.midtop = (x, y)
    surf.blit(text_surface, text_rect)


class Snake(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((12,12))
        self.image.fill(WHITE)
        self.rect=self.image.get_rect()
        self.rect.center = (100,100)
        self.speedx=0
        self.speedy=0
        self.score=0
        self.tail=[]

    def update(self):
        if self.score == len(self.tail):
            self.tail.append((self.rect.x, self.rect.y))
        else:
            self.tail.append((self.rect.x, self.rect.y))
            self.tail.pop(0)

        self.rect.x += self.speedx
        self.rect.y += self.speedy

        keys = pygame.key.get_pressed()

        if(keys[pygame.K_RIGHT] and self.speedx >=0):
            self.speedx = 10
            self.speedy = 0
        if(keys[pygame.K_LEFT] and self.speedx <=0):
            self.speedx = -10
            self.speedy = 0
        if(keys[pygame.K_UP] and self.speedy <=0):
            self.speedx = 0
            self.speedy = -10
        if(keys[pygame.K_DOWN] and self.speedy >=0):
            self.speedx = 0
            self.speedy = 10

        if self.rect.left >= width:
            self.rect.left = 1

        if self.rect.top >= height:
            self.rect.top = 0

        if self.rect.bottom <= 0:
            self.rect.bottom = height

        if self.rect.left <= 0:
            self.rect.left = width

    def exit(self):
        for i in range(1, len(self.tail)):
            if dist(self.rect.x, self.rect.y, self.tail[i][0], self.tail[i][1]) < 1:
                sys.exit()

class Food(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((12,12))
        self.image.fill(RED)
        self.rect=self.image.get_rect()
        self.rect.center = (x, y)


# Start dah game
pygame.init()
screen = pygame.display.set_mode([width,height])
clock = pygame.time.Clock()
running = True

print("ok?")
all_sprites = pygame.sprite.Group()
player = Snake()
food = Food(random.randrange(20, width - 20), random.randrange(20, height - 20))
all_sprites.add(player)
all_sprites.add(food)

while running:

    clock.tick(3)
    #pygame.display.update()
    keys_pressed = pygame.event.get()
    print(keys_pressed)
    for i in keys_pressed:
        if i.type == pygame.QUIT:
            print("quit")
            running = False
    if pygame.sprite.collide_rect(player, food):

        food.kill()
        player.score += 1
        food = Food(random.randrange(20, width - 20), random.randrange(20, height - 20)) 
        all_sprites.add(food)

    all_sprites.update()
    screen.fill(BLACK)
    player.exit()

    for i in range(1, len(player.tail)):
        pygame.draw.rect(screen, WHITE, (player.tail[i][0], player.tail[i][2], 12, 12))
        #draw_score_text(screen, str(player.score), 18, width/2, 10)
        all_sprites.draw(screen)
        pygame.display.flip()


pygame.quit()
1
pygame.display.flip() and all_sprites.draw(screen) has to be done in the main loop rather than in the inner for loop - Rabbid76

1 Answers

1
votes

The update of the display and the drawing of the sprites has to be done in the main loop rather than in the inner for loop:

while running:

    # [...]

    for i in range(1, len(player.tail)):
        pygame.draw.rect(screen, WHITE, (player.tail[i][0], player.tail[i][2], 12, 12))

    # <--
    all_sprites.draw(screen)
    pygame.display.flip()

Note, if the snake consists of a head only, then the inner loop is not executed. If the length of the tail would be > 1, then the display would be updated multiple times per frame, which is not what you want.


I am getting an out of range exception

Traceback (most recent call last): File "snake.py", line 132, in <module> > 
pygame.draw.rect(screen, WHITE, (player.tail[i][0], player.tail[i][2], 12, 12)) 
IndexError: tuple index out of range `

The index of the 2nd (y) coordinate is 1 rather than 2. Ist has to be player.tail[i][1]:

for i in range(1, len(player.tail)):
    pygame.draw.rect(screen, WHITE, (player.tail[i][0], player.tail[i][1], 12, 12))