Set range of values for random.randrange ()

2

I have a problem in Pygame when trying to get random values within a range with random.randrange , this is my code:

import random
import pygame


WIDTH = 800
HEIGHT = 600
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)


game_display = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Snake Game")
clock = pygame.time.Clock()


class Snake:
    def __init__(self, color):
        self.color = color
        self.x = random.randrange(range(0, WIDTH))
        self.y = random.randrange(range(0, HEIGHT))
        self.size = random.randrange(range(4, 8))

    def move(self):
        self.move_x = random.randrange(-1, 2)
        self.move_y = random.randrange(-1, 2)
        self.x += self.move_x
        self.y += self.move_y

        if self.x < 0:
            self.x = 0
        elif self.x > WIDTH:
            self.x = WIDTH

        if self.y < 0:
            self.y = 0
        elif self.y > HEIGHT:
            self.y = HEIGHT


def draw_environment():
    game_display.fill(WHITE)
    pygame.draw.rect(game_display, Snake.color, [Snake.x, Snake.y],     Snake.size)
    pygame.display.update()
    Snake.move()


def main():
    green_snake = Snake(GREEN)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        draw_environment(green_snake)
        clock.tick(60)

if __name__ == "__main__":
    main()

And this is the exception that generates me:

Traceback (most recent call last):
  File "C:/Users/santi/Desktop/Python/Test.py", line 60, in <module>
    main()
  File "C:/Users/santi/Desktop/Python/Test.py", line 50, in main
    green_snake = Snake(GREEN)
  File "C:/Users/santi/Desktop/Python/Test.py", line 21, in __init__
    self.x = random.randrange(range(0, WIDTH))
  File     "C:\Users\santi\AppData\Local\Programs\Python\Python35\lib\random.py", line 180,      in randrange
    istart = _int(start)
TypeError: int() argument must be a string, a bytes-like object or a number,      not 'range'
    
asked by Santiago Pardal 25.01.2017 в 00:43
source

1 answer

3

Randrange , already establishes a range, just as range supports a start value and an end value as parameters (not necessarily the only parameters). But in order to answer the question, more specifically, the syntax would be:

random.randrange(inicio,final)

where inicio and final are integers. Your code must be modified at the beginning of the class definition:

class Snake:
    def __init__(self, color):
        self.color = color
        self.x = random.randrange(range(0, WIDTH))
        self.y = random.randrange(range(0, HEIGHT))
        self.size = random.randrange(range(4, 8))

for

class Snake:
    def __init__(self, color):
        self.color = color
        self.x = random.randrange(0, WIDTH)
        self.y = random.randrange(0, HEIGHT)
        self.size = random.randrange(4, 8)

That would solve that error.

    
answered by 25.01.2017 в 03:46