sjgood 0 Newbie Poster

I need help with two things....

1. I would like to lower the speed of the snake.
2. I want to make the screen, snake, and food bigger.

I really need to change the speed. The 2nd thing is just something extra.
But, I've tried a bunch of little things for both, but nothing seems to work.

Here's my code so far... any suggestions would help!

from livewires import games, color
import random, sys, pygame
from pygame import *

class PlayingArea():
    
    def __init__(self):
        self.size = 60
        self.max_food = 3
        self.current_food = 0
        self.draw_map()
        self.food()

    def draw_map(self):
        map = []
        row_1 = []
        for x in range(self.size + 2):
            row_1.append(2)
        map.append(row_1)
        row = []
        for x in range(self.size):
            row.append(2)
            for y in range(self.size):
                row.append(0)
            row.append(2)
            map.append(row)
            row = []
        map.append(row_1)
        self.map = map

    def food(self):
        while self.current_food < self.max_food:
            x = random.randint(0, self.size)
            y = random.randint(0, self.size)
            while self.map[x][y] == 2:
                x = random.randint(0, self.size)
                y = random.randint(0, self.size)
            self.map[x][y] = 1
            self.current_food += 1

class Snake:

    def __init__(self, start_pos, color):
        self.location = [start_pos, ]
        self.length = 3
        self.max_length = 300
        self.color = color
        self.new_direction = 'down'
        self.direction = ''

    def eat(self):
        if not self.length == self.max_length:
            self.length += 1

    def move(self):
        self.direction = self.new_direction
        updated_location = []
        x = self.location[0]
        if self.direction == 'right':
            updated_location.append((x[0], x[1]+1))
        elif self.direction == 'left':
            updated_location.append((x[0], x[1]-1))
        elif self.direction == 'up':
            updated_location.append((x[0]-1, x[1]))
        elif self.direction == 'down':
            updated_location.append((x[0]+1, x[1]))
        updated_location.extend(self.location)
        if not self.length > len(self.location):
            updated_location.pop()
        self.location = updated_location

    def change_direction(self, direction):
        self.new_direction = direction

class Main:

    def __init__(self, screen):
        self.screen = screen
        self.PlayingArea = PlayingArea()
        color = (255, 0, 204)
        self.snake = Snake([5,5], color)
        self.score = 0
        self.font = pygame.font.Font(None, 30)
        self.restart = 0
        self.map = self.PlayingArea.map
        self.start_up()

    def draw_score(self):
        ren = self.font.render(str(self.score), True, (229, 204, 0))
        self.screen.blit(ren, (4,4))

    def collision(self):
        pos = self.Snake.location[0]
        x = pos[0]
        y = pos[1]
        hit = self.map[x][y]
        if hit == 1:
            self.Snake.eat()
            self.score += 1
            self.map[x][y] = 0
            self.PlayingArea.current_food -= 1
        if hit == 2:
            self.restart = 1
        if pos in self.Snake.location[1:]:
            self.restart = 1

    def event(self, events):
        for event in events:
            if event.type == USEREVENT+1:
                self.Snake.move()
                self.collision()
                self.output()
                self.PlayingArea.food()
            if event.type == QUIT:
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_LEFT and not self.Snake.direction == 'right':
                    self.Snake.change_direction('left')
                if event.key == K_RIGHT and not self.Snake.direction == 'left':
                    self.Snake.change_direction('right')
                if event.key == K_UP and not self.Snake.direction == 'down':
                    self.Snake.change_direction('up')
                if event.key == K_DOWN and not self.Snake.direction == 'up':
                    self.Snake.change_direction('down')
                if event.key == K_ESCAPE:
                    sys.exit()

    def output(self):
        self.screen.fill((0, 0, 0))
        pos = [0, 0]
        size = 4
        for x in range(len(self.map)):
            for y in range(len(self.map[0])):
                if self.map[x][y] == 1:
                    color = (255, 0, 0)
                if self.map[x][y] == 0:
                    color = (255, 255, 255)
                if self.map[x][y] == 2:
                    color = (0, 0, 0)
                if (x, y) in self.Snake.location:
                    color = self.Snake.color
                draw_pos = []
                draw_pos = [pos[0], pos[1], size, size]
                pygame.draw.react(self.screen, color, draw_pos)
                pos[0] += size
            pos[1] += size
            pos[0] = 0
        self.draw_score()

    def start_up(self):
        self.output()
        ren = self.font.render("Press any key to begin!", True, (0, 0, 0))
        self.screen.blit(ren, (20, 50))
        pygame.display.flip()
        clock = pygame.time.Clock()
        while True:
            clock.tick(10)
            for event in pygame.event.get():
                if event.type == KEYDOWN:
                    if event.type == K_ESCAPE:
                        sys.exit()
                    return
                if event.type == QUIT:
                    sys.exit()

    def update(self):
        events = pygame.event.get()
        self.event(events)


pygame.init()
window = pygame.display.set_mode((248, 248))
screen = pygame.display.get_surface()
game = Main(screen)
clock = pygame.time.Clock()
pygame.time.set_timer(USEREVENT+1, 50)
while True:
    game.update()
    clock.tick(200)
    pygame.display.flip()
    if game.restart == 1:
        game = Main(screen)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.