2017-01-25 20 views
-1

私の蛇「ゲーム」にエラーがあり、それが何であるかわからない、私はsysntaxisは大丈夫だと思うが、その "self.x" 」、 『self.y』、色やサイズ、ここではコードです:PythonのPygameエラー__init__

import random 
import pygame 


WIDTH = 800 
HEIGHT = 600 
GREEN = (0, 255, 0) 
RED = (255, 0, 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(Snake): 
    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() 

とエラーはここにある:あなたが私を助けることができる場合

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' 

は、それは素晴らしいだろう!

+0

なし

self.x = random.randrange(0, WIDTH) self.y = random.randrange(0, HEIGHT) 

が必要range()

を置く')(レンジ 'なし' – furas

+0

このcut + pasteエラーかもしれませんが、move()メソッドはSnakeクラスの一部になるように十分にインデントされていません。それは独立型の関数として解析されます。 –

+0

BTW: 'pygame.display.set_mode()'の前にすべてのクラスと関数を置くようにコードを読みやすくする - 引数のためではなく 'CamelCase'の名前だけをクラスに使う - def draw_environmentの小文字' snake'を使うのが良い(snake): ' – furas

答えて

0

randrange() 2つの数値を期待していますが、あなたは `random.randrange(0、WIDTH)が必要range()

+0

あなたの助けをありがとう、それはそれを修正しましたが、今私は他の問題があります:ファイル "C:/Users/santi/Desktop/Python/Test.py"、メインの行56、 draw_environment(green_snake) ファイル "C: /Users/santi/Desktop/Python/Test.py "、行44、draw_environment pygame.draw.rect(game_display、snake.color、[snake.x、snake.y]、snake.size) TypeError:Rect引数は無効です –

+0

'draw.rect()'は最後の引数、または少なくとも '(x、y、width、heigh)のタプル'として 'pygame.Rect()'を期待しますが、 '[snake.x、snake.y ] '。最後の引数はボーダーサイズを意味しますが、 'snake.size'を使用します。 – furas