私の蛇「ゲーム」にエラーがあり、それが何であるかわからない、私は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'
は、それは素晴らしいだろう!
なし
が必要
range()
を置く')(レンジ 'なし' – furas
このcut + pasteエラーかもしれませんが、move()メソッドはSnakeクラスの一部になるように十分にインデントされていません。それは独立型の関数として解析されます。 –
BTW: 'pygame.display.set_mode()'の前にすべてのクラスと関数を置くようにコードを読みやすくする - 引数のためではなく 'CamelCase'の名前だけをクラスに使う - def draw_environmentの小文字' snake'を使うのが良い(snake): ' – furas