I am very new to Python and am trying to make a snake game, But I am being told multiple times that the 'rect argument is invalid' It is :
File "c:\Users\Idontwanttomention\Desktop\Coding\Pygame\Snake.py", line 47, in <module> pygame.draw.rect(screen,(255,0,0),(snake_pos[0], snake_pos[1],15, 15)) TypeError: rect argument is invalid
This is my code :
import pygame
import random
pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode( (800,600) )
#snake_pos = [300,200]
snake_pos = [ [400,300] , [420,300] , [440,300] ]
apple_pos = [ random.randint(100,700), random.randint(100,500) ]
step = 10
up = (0, -step)
down = (0, step)
left = (-step, 0)
right = (step, 0)
direction = left
# TITLE
running = True
while running:
screen.fill ((230,195,255))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
direction = up
if event.key == pygame.K_DOWN:
direction = down
#snake_pos[1] += 10
if event.key == pygame.K_LEFT:
direction = left
if event.key == pygame.K_RIGHT:
direction = right
#snake_pos[0] += 10
# MOVING SNAKE
pygame.draw.rect(screen,(255,0,0),(apple_pos[0], apple_pos[1],15, 15))
pygame.draw.rect(screen,(255,0,0),(snake_pos[0], snake_pos[1],15, 15))
clock.tick(5)
pygame.display.update()
snake_pos
is a list of tuples. Each tuple in the list is a part of the snakes body. Therefore, you must draw one rectangle for each part of the body. Use a for
-loop to draw the snake:
while running:
# [...]
# APPLE
pygame.draw.rect(screen,(255,0,0),(apple_pos[0], apple_pos[1], 15, 15))
# MOVING SNAKE
for pos in snake_pos:
pygame.draw.rect(screen,(255,0,0),(pos[0], pos[1], 15, 15))