pygame.Rect.move_ip() not updating rect attributeRemaking snake, cant make more segmentsHow to sort a list of objects based on an attribute of the objects?How to know if an object has an attribute in PythonSort a list by multiple attributes?'Chicken' object has no attribute 'rect'how to get rect attribute with no imagePygame: comparison flip() and update(rect)Pygame - locking the scroll of a surface area to a limited amountMy rect isn't updatingAble to get image to move in Pygame; but I have to keep on pressing the button to make it move'bool' object has no attribute 'rect'
How do I tell my boss that I'm quitting soon, especially given that a colleague just left this week
What is the difference between lands and mana?
Change the color of a single dot in `ddot` symbol
What kind of floor tile is this?
Taxes on Dividends in a Roth IRA
How to make money from a browser who sees 5 seconds into the future of any web page?
Can I turn my anal-retentiveness into a career?
How does electrical safety system work on ISS?
How to get directions in deep space?
Biological Blimps: Propulsion
What are some good ways to treat frozen vegetables such that they behave like fresh vegetables when stir frying them?
Can you use Vicious Mockery to win an argument or gain favours?
What does "Scientists rise up against statistical significance" mean? (Comment in Nature)
Does Doodling or Improvising on the Piano Have Any Benefits?
C++ check if statement can be evaluated constexpr
Delete multiple columns using awk or sed
Does the reader need to like the PoV character?
Giving feedback to someone without sounding prejudiced
US tourist/student visa
Permission on Database
How do I fix the group tension caused by my character stealing and possibly killing without provocation?
Is it allowed to activate the ability of multiple planeswalkers in a single turn?
Make a Bowl of Alphabet Soup
Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?
pygame.Rect.move_ip() not updating rect attribute
Remaking snake, cant make more segmentsHow to sort a list of objects based on an attribute of the objects?How to know if an object has an attribute in PythonSort a list by multiple attributes?'Chicken' object has no attribute 'rect'how to get rect attribute with no imagePygame: comparison flip() and update(rect)Pygame - locking the scroll of a surface area to a limited amountMy rect isn't updatingAble to get image to move in Pygame; but I have to keep on pressing the button to make it move'bool' object has no attribute 'rect'
I am creating a snake clone in pygame, and I am currently working on managing the turning mechanisms of the snake and its movement. I create a point at the time of turning in pygame, then store it in snake.pos_at_turn
. After that I adjust each snake segments direction the should move until either their x or y coordinate equals that of snake.pos_at_turn
then I change their direction again. The problem is that for some reason the snake segment doesn't move at all. Any help would be appreciated!
Code for snake object and event loop:
import pygame,sys,random
from pygame.locals import *
pygame.init()
WINDOW_HEIGHT = 500
WINDOW_WIDTH = 500
RECTANGLE_HEIGHT = 20
RECTANGLE_WIDTH = 20
GREEN = (0,255,0)
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
clock = pygame.time.Clock()
SCREEN = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
pygame.display.set_caption("Snake")
class Snake(object):
def __init__(self, color, width, height):
self.body = []
self.image = pygame.Surface((width,height))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.speed = 20
self.direction = 'right'
self.living = True
self.move = (0,0)
self.pos_at_turn = [0,0]
def movement(self):
if self.direction == 'right':
self.move = (self.speed,0)
elif self.direction == 'left':
self.move = (-self.speed,0)
elif self.direction == 'up':
self.move = (0,-self.speed)
elif self.direction == 'down':
self.move = (0,self.speed)
self.rect.move_ip(self.move)
self.rect.clamp_ip(SCREEN.get_rect())
def blit(self):
SCREEN.blit(self.image,self.rect)
def update(self,food):
self.movement()
self.add_segment(food)
self.draw_snake()
for snake_segment in self.body:
print(snake_segment.rect)
def add_segment(self,food):
snake_segment = Snake(GREEN,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
if len(self.body) == 0:
self.body.append(snake_segment)
if self.rect.colliderect(food.rect):
self.body.append(snake_segment)
def draw_snake(self):
for snake_segment in self.body:
if self.body.index(snake_segment) == 0:
self.blit()
else:
# original movement code:
# if self.direction == 'right':
# snake_segment.rect.x = self.rect.x - RECTANGLE_WIDTH * self.body.index(snake_segment)
# snake_segment.rect.y = self.rect.y
# snake_segment.blit()
# elif self.direction == 'left':
# snake_segment.rect.x = self.rect.x + RECTANGLE_WIDTH * self.body.index(snake_segment)
# snake_segment.rect.y = self.rect.y
# snake_segment.blit()
# elif self.direction == 'up':
# snake_segment.rect.x = self.rect.x
# snake_segment.rect.y = self.rect.y + RECTANGLE_HEIGHT * self.body.index(snake_segment)
# snake_segment.blit()
# elif self.direction == 'down':
# snake_segment.rect.x = self.rect.x
# snake_segment.rect.y = self.rect.y - RECTANGLE_HEIGHT * self.body.index(snake_segment)
# snake_segment.blit()
if self.direction == 'right':
if snake_segment.rect.y >= self.pos_at_turn[1]:
snake_segment.direction = 'down'
elif snake_segment.rect.y <= self.pos_at_turn[1]:
snake_segment.direction = 'up'
if snake_segment.rect.y == self.pos_at_turn[1]:
if snake_segment.rect.x >= self.pos_at_turn[0]:
snake_segment.direction = 'left'
elif snake_segment.rect.x <= self.pos_at_turn[0]:
snake_segment.direction = 'right'
snake_segment.blit()
elif self.direction == 'left':
if snake_segment.rect.y >= self.pos_at_turn[1]:
snake_segment.direction = 'down'
elif snake_segment.rect.y <= self.pos_at_turn[1]:
snake_segment.direction = 'up'
if snake_segment.rect.y == self.pos_at_turn[1]:
if snake_segment.rect.x >= self.pos_at_turn[0]:
snake_segment.direction = 'left'
elif snake_segment.rect.x <= self.pos_at_turn[0]:
snake_segment.direction = 'right'
snake_segment.blit()
elif self.direction == 'up':
if snake_segment.rect.x >= self.pos_at_turn[0]:
snake_segment.direction = 'left'
elif snake_segment.rect.x <= self.pos_at_turn[0]:
snake_segment.direction = 'right'
if snake_segment.rect.x == self.pos_at_turn[0]:
if snake_segment.rect.y >= self.pos_at_turn[1]:
snake_segment.direction = 'down'
elif snake_segment.rect.y <= self.pos_at_turn[1]:
snake_segment.direction = 'up'
snake_segment.blit()
elif self.direction == 'down':
if snake_segment.rect.x >= self.pos_at_turn[0]:
snake_segment.direction = 'left'
elif snake_segment.rect.x <= self.pos_at_turn[0]:
snake_segment.direction = 'right'
if snake_segment.rect.x == self.pos_at_turn[0]:
if snake_segment.rect.y >= self.pos_at_turn[1]:
snake_segment.direction = 'down'
elif snake_segment.rect.y <= self.pos_at_turn[1]:
snake_segment.direction = 'up'
snake_segment.blit()
class Food(object):
def __init__(self,color,width,height):
self.image = pygame.Surface((width,height))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = random.randrange(20,500,20)
self.rect.y = random.randrange(20,500,20)
self.state = 'uneaten'
def check_eaten(self,snake):
if snake.rect.colliderect(self.rect):
self.state = 'eaten'
def blit(self):
SCREEN.blit(self.image,self.rect)
def update_state(self,snake):
self.check_eaten(snake)
if self.state == 'uneaten':
self.blit()
elif self.state == 'eaten':
self.rect.x = random.randrange(0,500,20)
self.rect.y = random.randrange(0,500,20)
if self.rect.x == snake.rect.x or self.rect.x == snake.rect.y:
self.rect.x = random.randrange(0,500,20)
self.rect.y = random.randrange(0,500,20)
self.blit()
self.state = 'uneaten'
def update(self,snake):
self.update_state(snake)
def event_loop(snake,food):
while True:
SCREEN.fill(BLACK)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
snake.pos_at_turn = [snake.rect.x, snake.rect.y]
if event.key == K_RIGHT:
snake.direction = 'right'
elif event.key == K_LEFT:
snake.direction = 'left'
elif event.key == K_UP:
snake.direction = 'up'
elif event.key == K_DOWN:
snake.direction = 'down'
snake.update(food)
food.update(snake)
pygame.display.update()
clock.tick(5)
SNAKE = Snake(GREEN,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
FOOD = Food(RED,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
event_loop(SNAKE,FOOD)
python pygame
add a comment |
I am creating a snake clone in pygame, and I am currently working on managing the turning mechanisms of the snake and its movement. I create a point at the time of turning in pygame, then store it in snake.pos_at_turn
. After that I adjust each snake segments direction the should move until either their x or y coordinate equals that of snake.pos_at_turn
then I change their direction again. The problem is that for some reason the snake segment doesn't move at all. Any help would be appreciated!
Code for snake object and event loop:
import pygame,sys,random
from pygame.locals import *
pygame.init()
WINDOW_HEIGHT = 500
WINDOW_WIDTH = 500
RECTANGLE_HEIGHT = 20
RECTANGLE_WIDTH = 20
GREEN = (0,255,0)
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
clock = pygame.time.Clock()
SCREEN = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
pygame.display.set_caption("Snake")
class Snake(object):
def __init__(self, color, width, height):
self.body = []
self.image = pygame.Surface((width,height))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.speed = 20
self.direction = 'right'
self.living = True
self.move = (0,0)
self.pos_at_turn = [0,0]
def movement(self):
if self.direction == 'right':
self.move = (self.speed,0)
elif self.direction == 'left':
self.move = (-self.speed,0)
elif self.direction == 'up':
self.move = (0,-self.speed)
elif self.direction == 'down':
self.move = (0,self.speed)
self.rect.move_ip(self.move)
self.rect.clamp_ip(SCREEN.get_rect())
def blit(self):
SCREEN.blit(self.image,self.rect)
def update(self,food):
self.movement()
self.add_segment(food)
self.draw_snake()
for snake_segment in self.body:
print(snake_segment.rect)
def add_segment(self,food):
snake_segment = Snake(GREEN,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
if len(self.body) == 0:
self.body.append(snake_segment)
if self.rect.colliderect(food.rect):
self.body.append(snake_segment)
def draw_snake(self):
for snake_segment in self.body:
if self.body.index(snake_segment) == 0:
self.blit()
else:
# original movement code:
# if self.direction == 'right':
# snake_segment.rect.x = self.rect.x - RECTANGLE_WIDTH * self.body.index(snake_segment)
# snake_segment.rect.y = self.rect.y
# snake_segment.blit()
# elif self.direction == 'left':
# snake_segment.rect.x = self.rect.x + RECTANGLE_WIDTH * self.body.index(snake_segment)
# snake_segment.rect.y = self.rect.y
# snake_segment.blit()
# elif self.direction == 'up':
# snake_segment.rect.x = self.rect.x
# snake_segment.rect.y = self.rect.y + RECTANGLE_HEIGHT * self.body.index(snake_segment)
# snake_segment.blit()
# elif self.direction == 'down':
# snake_segment.rect.x = self.rect.x
# snake_segment.rect.y = self.rect.y - RECTANGLE_HEIGHT * self.body.index(snake_segment)
# snake_segment.blit()
if self.direction == 'right':
if snake_segment.rect.y >= self.pos_at_turn[1]:
snake_segment.direction = 'down'
elif snake_segment.rect.y <= self.pos_at_turn[1]:
snake_segment.direction = 'up'
if snake_segment.rect.y == self.pos_at_turn[1]:
if snake_segment.rect.x >= self.pos_at_turn[0]:
snake_segment.direction = 'left'
elif snake_segment.rect.x <= self.pos_at_turn[0]:
snake_segment.direction = 'right'
snake_segment.blit()
elif self.direction == 'left':
if snake_segment.rect.y >= self.pos_at_turn[1]:
snake_segment.direction = 'down'
elif snake_segment.rect.y <= self.pos_at_turn[1]:
snake_segment.direction = 'up'
if snake_segment.rect.y == self.pos_at_turn[1]:
if snake_segment.rect.x >= self.pos_at_turn[0]:
snake_segment.direction = 'left'
elif snake_segment.rect.x <= self.pos_at_turn[0]:
snake_segment.direction = 'right'
snake_segment.blit()
elif self.direction == 'up':
if snake_segment.rect.x >= self.pos_at_turn[0]:
snake_segment.direction = 'left'
elif snake_segment.rect.x <= self.pos_at_turn[0]:
snake_segment.direction = 'right'
if snake_segment.rect.x == self.pos_at_turn[0]:
if snake_segment.rect.y >= self.pos_at_turn[1]:
snake_segment.direction = 'down'
elif snake_segment.rect.y <= self.pos_at_turn[1]:
snake_segment.direction = 'up'
snake_segment.blit()
elif self.direction == 'down':
if snake_segment.rect.x >= self.pos_at_turn[0]:
snake_segment.direction = 'left'
elif snake_segment.rect.x <= self.pos_at_turn[0]:
snake_segment.direction = 'right'
if snake_segment.rect.x == self.pos_at_turn[0]:
if snake_segment.rect.y >= self.pos_at_turn[1]:
snake_segment.direction = 'down'
elif snake_segment.rect.y <= self.pos_at_turn[1]:
snake_segment.direction = 'up'
snake_segment.blit()
class Food(object):
def __init__(self,color,width,height):
self.image = pygame.Surface((width,height))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = random.randrange(20,500,20)
self.rect.y = random.randrange(20,500,20)
self.state = 'uneaten'
def check_eaten(self,snake):
if snake.rect.colliderect(self.rect):
self.state = 'eaten'
def blit(self):
SCREEN.blit(self.image,self.rect)
def update_state(self,snake):
self.check_eaten(snake)
if self.state == 'uneaten':
self.blit()
elif self.state == 'eaten':
self.rect.x = random.randrange(0,500,20)
self.rect.y = random.randrange(0,500,20)
if self.rect.x == snake.rect.x or self.rect.x == snake.rect.y:
self.rect.x = random.randrange(0,500,20)
self.rect.y = random.randrange(0,500,20)
self.blit()
self.state = 'uneaten'
def update(self,snake):
self.update_state(snake)
def event_loop(snake,food):
while True:
SCREEN.fill(BLACK)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
snake.pos_at_turn = [snake.rect.x, snake.rect.y]
if event.key == K_RIGHT:
snake.direction = 'right'
elif event.key == K_LEFT:
snake.direction = 'left'
elif event.key == K_UP:
snake.direction = 'up'
elif event.key == K_DOWN:
snake.direction = 'down'
snake.update(food)
food.update(snake)
pygame.display.update()
clock.tick(5)
SNAKE = Snake(GREEN,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
FOOD = Food(RED,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
event_loop(SNAKE,FOOD)
python pygame
What are you trying to achieve by remembering the turning point - a way to "slither" the snake around a corner?
– Kingsley
Mar 7 at 5:07
I store a turning point so I can change the direction adjust for and change the direction at that point. The segment is going down let's say and it passed through that point. Then it turns to the right once it's rect coordinates equal the coordinates of the turning point.
– Dovid Tor Sdayur
Mar 7 at 5:17
You don't really need to remember the point like this, just move the Nth body part to the position of the (N-1)th part. If this is what you're doing I answered a question explaining this a few days ago. stackoverflow.com/questions/54894355/… (But I'm not 100% sure this is what you mean, if not, please disregard this comment).
– Kingsley
Mar 7 at 5:36
I have looked at your comment. I will try that tomorrow.
– Dovid Tor Sdayur
Mar 7 at 5:41
add a comment |
I am creating a snake clone in pygame, and I am currently working on managing the turning mechanisms of the snake and its movement. I create a point at the time of turning in pygame, then store it in snake.pos_at_turn
. After that I adjust each snake segments direction the should move until either their x or y coordinate equals that of snake.pos_at_turn
then I change their direction again. The problem is that for some reason the snake segment doesn't move at all. Any help would be appreciated!
Code for snake object and event loop:
import pygame,sys,random
from pygame.locals import *
pygame.init()
WINDOW_HEIGHT = 500
WINDOW_WIDTH = 500
RECTANGLE_HEIGHT = 20
RECTANGLE_WIDTH = 20
GREEN = (0,255,0)
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
clock = pygame.time.Clock()
SCREEN = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
pygame.display.set_caption("Snake")
class Snake(object):
def __init__(self, color, width, height):
self.body = []
self.image = pygame.Surface((width,height))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.speed = 20
self.direction = 'right'
self.living = True
self.move = (0,0)
self.pos_at_turn = [0,0]
def movement(self):
if self.direction == 'right':
self.move = (self.speed,0)
elif self.direction == 'left':
self.move = (-self.speed,0)
elif self.direction == 'up':
self.move = (0,-self.speed)
elif self.direction == 'down':
self.move = (0,self.speed)
self.rect.move_ip(self.move)
self.rect.clamp_ip(SCREEN.get_rect())
def blit(self):
SCREEN.blit(self.image,self.rect)
def update(self,food):
self.movement()
self.add_segment(food)
self.draw_snake()
for snake_segment in self.body:
print(snake_segment.rect)
def add_segment(self,food):
snake_segment = Snake(GREEN,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
if len(self.body) == 0:
self.body.append(snake_segment)
if self.rect.colliderect(food.rect):
self.body.append(snake_segment)
def draw_snake(self):
for snake_segment in self.body:
if self.body.index(snake_segment) == 0:
self.blit()
else:
# original movement code:
# if self.direction == 'right':
# snake_segment.rect.x = self.rect.x - RECTANGLE_WIDTH * self.body.index(snake_segment)
# snake_segment.rect.y = self.rect.y
# snake_segment.blit()
# elif self.direction == 'left':
# snake_segment.rect.x = self.rect.x + RECTANGLE_WIDTH * self.body.index(snake_segment)
# snake_segment.rect.y = self.rect.y
# snake_segment.blit()
# elif self.direction == 'up':
# snake_segment.rect.x = self.rect.x
# snake_segment.rect.y = self.rect.y + RECTANGLE_HEIGHT * self.body.index(snake_segment)
# snake_segment.blit()
# elif self.direction == 'down':
# snake_segment.rect.x = self.rect.x
# snake_segment.rect.y = self.rect.y - RECTANGLE_HEIGHT * self.body.index(snake_segment)
# snake_segment.blit()
if self.direction == 'right':
if snake_segment.rect.y >= self.pos_at_turn[1]:
snake_segment.direction = 'down'
elif snake_segment.rect.y <= self.pos_at_turn[1]:
snake_segment.direction = 'up'
if snake_segment.rect.y == self.pos_at_turn[1]:
if snake_segment.rect.x >= self.pos_at_turn[0]:
snake_segment.direction = 'left'
elif snake_segment.rect.x <= self.pos_at_turn[0]:
snake_segment.direction = 'right'
snake_segment.blit()
elif self.direction == 'left':
if snake_segment.rect.y >= self.pos_at_turn[1]:
snake_segment.direction = 'down'
elif snake_segment.rect.y <= self.pos_at_turn[1]:
snake_segment.direction = 'up'
if snake_segment.rect.y == self.pos_at_turn[1]:
if snake_segment.rect.x >= self.pos_at_turn[0]:
snake_segment.direction = 'left'
elif snake_segment.rect.x <= self.pos_at_turn[0]:
snake_segment.direction = 'right'
snake_segment.blit()
elif self.direction == 'up':
if snake_segment.rect.x >= self.pos_at_turn[0]:
snake_segment.direction = 'left'
elif snake_segment.rect.x <= self.pos_at_turn[0]:
snake_segment.direction = 'right'
if snake_segment.rect.x == self.pos_at_turn[0]:
if snake_segment.rect.y >= self.pos_at_turn[1]:
snake_segment.direction = 'down'
elif snake_segment.rect.y <= self.pos_at_turn[1]:
snake_segment.direction = 'up'
snake_segment.blit()
elif self.direction == 'down':
if snake_segment.rect.x >= self.pos_at_turn[0]:
snake_segment.direction = 'left'
elif snake_segment.rect.x <= self.pos_at_turn[0]:
snake_segment.direction = 'right'
if snake_segment.rect.x == self.pos_at_turn[0]:
if snake_segment.rect.y >= self.pos_at_turn[1]:
snake_segment.direction = 'down'
elif snake_segment.rect.y <= self.pos_at_turn[1]:
snake_segment.direction = 'up'
snake_segment.blit()
class Food(object):
def __init__(self,color,width,height):
self.image = pygame.Surface((width,height))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = random.randrange(20,500,20)
self.rect.y = random.randrange(20,500,20)
self.state = 'uneaten'
def check_eaten(self,snake):
if snake.rect.colliderect(self.rect):
self.state = 'eaten'
def blit(self):
SCREEN.blit(self.image,self.rect)
def update_state(self,snake):
self.check_eaten(snake)
if self.state == 'uneaten':
self.blit()
elif self.state == 'eaten':
self.rect.x = random.randrange(0,500,20)
self.rect.y = random.randrange(0,500,20)
if self.rect.x == snake.rect.x or self.rect.x == snake.rect.y:
self.rect.x = random.randrange(0,500,20)
self.rect.y = random.randrange(0,500,20)
self.blit()
self.state = 'uneaten'
def update(self,snake):
self.update_state(snake)
def event_loop(snake,food):
while True:
SCREEN.fill(BLACK)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
snake.pos_at_turn = [snake.rect.x, snake.rect.y]
if event.key == K_RIGHT:
snake.direction = 'right'
elif event.key == K_LEFT:
snake.direction = 'left'
elif event.key == K_UP:
snake.direction = 'up'
elif event.key == K_DOWN:
snake.direction = 'down'
snake.update(food)
food.update(snake)
pygame.display.update()
clock.tick(5)
SNAKE = Snake(GREEN,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
FOOD = Food(RED,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
event_loop(SNAKE,FOOD)
python pygame
I am creating a snake clone in pygame, and I am currently working on managing the turning mechanisms of the snake and its movement. I create a point at the time of turning in pygame, then store it in snake.pos_at_turn
. After that I adjust each snake segments direction the should move until either their x or y coordinate equals that of snake.pos_at_turn
then I change their direction again. The problem is that for some reason the snake segment doesn't move at all. Any help would be appreciated!
Code for snake object and event loop:
import pygame,sys,random
from pygame.locals import *
pygame.init()
WINDOW_HEIGHT = 500
WINDOW_WIDTH = 500
RECTANGLE_HEIGHT = 20
RECTANGLE_WIDTH = 20
GREEN = (0,255,0)
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
clock = pygame.time.Clock()
SCREEN = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT))
pygame.display.set_caption("Snake")
class Snake(object):
def __init__(self, color, width, height):
self.body = []
self.image = pygame.Surface((width,height))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.speed = 20
self.direction = 'right'
self.living = True
self.move = (0,0)
self.pos_at_turn = [0,0]
def movement(self):
if self.direction == 'right':
self.move = (self.speed,0)
elif self.direction == 'left':
self.move = (-self.speed,0)
elif self.direction == 'up':
self.move = (0,-self.speed)
elif self.direction == 'down':
self.move = (0,self.speed)
self.rect.move_ip(self.move)
self.rect.clamp_ip(SCREEN.get_rect())
def blit(self):
SCREEN.blit(self.image,self.rect)
def update(self,food):
self.movement()
self.add_segment(food)
self.draw_snake()
for snake_segment in self.body:
print(snake_segment.rect)
def add_segment(self,food):
snake_segment = Snake(GREEN,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
if len(self.body) == 0:
self.body.append(snake_segment)
if self.rect.colliderect(food.rect):
self.body.append(snake_segment)
def draw_snake(self):
for snake_segment in self.body:
if self.body.index(snake_segment) == 0:
self.blit()
else:
# original movement code:
# if self.direction == 'right':
# snake_segment.rect.x = self.rect.x - RECTANGLE_WIDTH * self.body.index(snake_segment)
# snake_segment.rect.y = self.rect.y
# snake_segment.blit()
# elif self.direction == 'left':
# snake_segment.rect.x = self.rect.x + RECTANGLE_WIDTH * self.body.index(snake_segment)
# snake_segment.rect.y = self.rect.y
# snake_segment.blit()
# elif self.direction == 'up':
# snake_segment.rect.x = self.rect.x
# snake_segment.rect.y = self.rect.y + RECTANGLE_HEIGHT * self.body.index(snake_segment)
# snake_segment.blit()
# elif self.direction == 'down':
# snake_segment.rect.x = self.rect.x
# snake_segment.rect.y = self.rect.y - RECTANGLE_HEIGHT * self.body.index(snake_segment)
# snake_segment.blit()
if self.direction == 'right':
if snake_segment.rect.y >= self.pos_at_turn[1]:
snake_segment.direction = 'down'
elif snake_segment.rect.y <= self.pos_at_turn[1]:
snake_segment.direction = 'up'
if snake_segment.rect.y == self.pos_at_turn[1]:
if snake_segment.rect.x >= self.pos_at_turn[0]:
snake_segment.direction = 'left'
elif snake_segment.rect.x <= self.pos_at_turn[0]:
snake_segment.direction = 'right'
snake_segment.blit()
elif self.direction == 'left':
if snake_segment.rect.y >= self.pos_at_turn[1]:
snake_segment.direction = 'down'
elif snake_segment.rect.y <= self.pos_at_turn[1]:
snake_segment.direction = 'up'
if snake_segment.rect.y == self.pos_at_turn[1]:
if snake_segment.rect.x >= self.pos_at_turn[0]:
snake_segment.direction = 'left'
elif snake_segment.rect.x <= self.pos_at_turn[0]:
snake_segment.direction = 'right'
snake_segment.blit()
elif self.direction == 'up':
if snake_segment.rect.x >= self.pos_at_turn[0]:
snake_segment.direction = 'left'
elif snake_segment.rect.x <= self.pos_at_turn[0]:
snake_segment.direction = 'right'
if snake_segment.rect.x == self.pos_at_turn[0]:
if snake_segment.rect.y >= self.pos_at_turn[1]:
snake_segment.direction = 'down'
elif snake_segment.rect.y <= self.pos_at_turn[1]:
snake_segment.direction = 'up'
snake_segment.blit()
elif self.direction == 'down':
if snake_segment.rect.x >= self.pos_at_turn[0]:
snake_segment.direction = 'left'
elif snake_segment.rect.x <= self.pos_at_turn[0]:
snake_segment.direction = 'right'
if snake_segment.rect.x == self.pos_at_turn[0]:
if snake_segment.rect.y >= self.pos_at_turn[1]:
snake_segment.direction = 'down'
elif snake_segment.rect.y <= self.pos_at_turn[1]:
snake_segment.direction = 'up'
snake_segment.blit()
class Food(object):
def __init__(self,color,width,height):
self.image = pygame.Surface((width,height))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = random.randrange(20,500,20)
self.rect.y = random.randrange(20,500,20)
self.state = 'uneaten'
def check_eaten(self,snake):
if snake.rect.colliderect(self.rect):
self.state = 'eaten'
def blit(self):
SCREEN.blit(self.image,self.rect)
def update_state(self,snake):
self.check_eaten(snake)
if self.state == 'uneaten':
self.blit()
elif self.state == 'eaten':
self.rect.x = random.randrange(0,500,20)
self.rect.y = random.randrange(0,500,20)
if self.rect.x == snake.rect.x or self.rect.x == snake.rect.y:
self.rect.x = random.randrange(0,500,20)
self.rect.y = random.randrange(0,500,20)
self.blit()
self.state = 'uneaten'
def update(self,snake):
self.update_state(snake)
def event_loop(snake,food):
while True:
SCREEN.fill(BLACK)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
snake.pos_at_turn = [snake.rect.x, snake.rect.y]
if event.key == K_RIGHT:
snake.direction = 'right'
elif event.key == K_LEFT:
snake.direction = 'left'
elif event.key == K_UP:
snake.direction = 'up'
elif event.key == K_DOWN:
snake.direction = 'down'
snake.update(food)
food.update(snake)
pygame.display.update()
clock.tick(5)
SNAKE = Snake(GREEN,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
FOOD = Food(RED,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
event_loop(SNAKE,FOOD)
python pygame
python pygame
asked Mar 7 at 5:00
Dovid Tor SdayurDovid Tor Sdayur
173
173
What are you trying to achieve by remembering the turning point - a way to "slither" the snake around a corner?
– Kingsley
Mar 7 at 5:07
I store a turning point so I can change the direction adjust for and change the direction at that point. The segment is going down let's say and it passed through that point. Then it turns to the right once it's rect coordinates equal the coordinates of the turning point.
– Dovid Tor Sdayur
Mar 7 at 5:17
You don't really need to remember the point like this, just move the Nth body part to the position of the (N-1)th part. If this is what you're doing I answered a question explaining this a few days ago. stackoverflow.com/questions/54894355/… (But I'm not 100% sure this is what you mean, if not, please disregard this comment).
– Kingsley
Mar 7 at 5:36
I have looked at your comment. I will try that tomorrow.
– Dovid Tor Sdayur
Mar 7 at 5:41
add a comment |
What are you trying to achieve by remembering the turning point - a way to "slither" the snake around a corner?
– Kingsley
Mar 7 at 5:07
I store a turning point so I can change the direction adjust for and change the direction at that point. The segment is going down let's say and it passed through that point. Then it turns to the right once it's rect coordinates equal the coordinates of the turning point.
– Dovid Tor Sdayur
Mar 7 at 5:17
You don't really need to remember the point like this, just move the Nth body part to the position of the (N-1)th part. If this is what you're doing I answered a question explaining this a few days ago. stackoverflow.com/questions/54894355/… (But I'm not 100% sure this is what you mean, if not, please disregard this comment).
– Kingsley
Mar 7 at 5:36
I have looked at your comment. I will try that tomorrow.
– Dovid Tor Sdayur
Mar 7 at 5:41
What are you trying to achieve by remembering the turning point - a way to "slither" the snake around a corner?
– Kingsley
Mar 7 at 5:07
What are you trying to achieve by remembering the turning point - a way to "slither" the snake around a corner?
– Kingsley
Mar 7 at 5:07
I store a turning point so I can change the direction adjust for and change the direction at that point. The segment is going down let's say and it passed through that point. Then it turns to the right once it's rect coordinates equal the coordinates of the turning point.
– Dovid Tor Sdayur
Mar 7 at 5:17
I store a turning point so I can change the direction adjust for and change the direction at that point. The segment is going down let's say and it passed through that point. Then it turns to the right once it's rect coordinates equal the coordinates of the turning point.
– Dovid Tor Sdayur
Mar 7 at 5:17
You don't really need to remember the point like this, just move the Nth body part to the position of the (N-1)th part. If this is what you're doing I answered a question explaining this a few days ago. stackoverflow.com/questions/54894355/… (But I'm not 100% sure this is what you mean, if not, please disregard this comment).
– Kingsley
Mar 7 at 5:36
You don't really need to remember the point like this, just move the Nth body part to the position of the (N-1)th part. If this is what you're doing I answered a question explaining this a few days ago. stackoverflow.com/questions/54894355/… (But I'm not 100% sure this is what you mean, if not, please disregard this comment).
– Kingsley
Mar 7 at 5:36
I have looked at your comment. I will try that tomorrow.
– Dovid Tor Sdayur
Mar 7 at 5:41
I have looked at your comment. I will try that tomorrow.
– Dovid Tor Sdayur
Mar 7 at 5:41
add a comment |
1 Answer
1
active
oldest
votes
You've to traverse the snakes body in reverse order in the method movement
. For each part of the body, copy the position of the previous part. The head part gets the current position of the snake:
class Snake(object):
# [...]
def movement(self):
if self.direction == 'right':
self.move = (self.speed,0)
elif self.direction == 'left':
self.move = (-self.speed,0)
elif self.direction == 'up':
self.move = (0,-self.speed)
elif self.direction == 'down':
self.move = (0,self.speed)
self.rect.move_ip(self.move)
self.rect.clamp_ip(SCREEN.get_rect())
for i in range(len(self.body)-1, 0, -1):
self.body[i].rect.center = self.body[i-1].rect.center
if self.body:
self.body[0].rect.center = self.rect.center
Init the position of a part, when a new part is add to the body in the method add_segment
:
class Snake(object):
# [...]
def add_segment(self,food):
if len(self.body) == 0 or self.rect.colliderect(food.rect):
snake_segment = Snake(GREEN,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
snake_segment.rect.center = self.rect.center
self.body.append(snake_segment)
In the method draw_snake
it is sufficient to traverse the parts of the body and to "blit
" each part:
class Snake(object):
# [...]
def draw_snake(self):
for snake_segment in self.body:
snake_segment.blit()
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55036397%2fpygame-rect-move-ip-not-updating-rect-attribute%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You've to traverse the snakes body in reverse order in the method movement
. For each part of the body, copy the position of the previous part. The head part gets the current position of the snake:
class Snake(object):
# [...]
def movement(self):
if self.direction == 'right':
self.move = (self.speed,0)
elif self.direction == 'left':
self.move = (-self.speed,0)
elif self.direction == 'up':
self.move = (0,-self.speed)
elif self.direction == 'down':
self.move = (0,self.speed)
self.rect.move_ip(self.move)
self.rect.clamp_ip(SCREEN.get_rect())
for i in range(len(self.body)-1, 0, -1):
self.body[i].rect.center = self.body[i-1].rect.center
if self.body:
self.body[0].rect.center = self.rect.center
Init the position of a part, when a new part is add to the body in the method add_segment
:
class Snake(object):
# [...]
def add_segment(self,food):
if len(self.body) == 0 or self.rect.colliderect(food.rect):
snake_segment = Snake(GREEN,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
snake_segment.rect.center = self.rect.center
self.body.append(snake_segment)
In the method draw_snake
it is sufficient to traverse the parts of the body and to "blit
" each part:
class Snake(object):
# [...]
def draw_snake(self):
for snake_segment in self.body:
snake_segment.blit()
add a comment |
You've to traverse the snakes body in reverse order in the method movement
. For each part of the body, copy the position of the previous part. The head part gets the current position of the snake:
class Snake(object):
# [...]
def movement(self):
if self.direction == 'right':
self.move = (self.speed,0)
elif self.direction == 'left':
self.move = (-self.speed,0)
elif self.direction == 'up':
self.move = (0,-self.speed)
elif self.direction == 'down':
self.move = (0,self.speed)
self.rect.move_ip(self.move)
self.rect.clamp_ip(SCREEN.get_rect())
for i in range(len(self.body)-1, 0, -1):
self.body[i].rect.center = self.body[i-1].rect.center
if self.body:
self.body[0].rect.center = self.rect.center
Init the position of a part, when a new part is add to the body in the method add_segment
:
class Snake(object):
# [...]
def add_segment(self,food):
if len(self.body) == 0 or self.rect.colliderect(food.rect):
snake_segment = Snake(GREEN,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
snake_segment.rect.center = self.rect.center
self.body.append(snake_segment)
In the method draw_snake
it is sufficient to traverse the parts of the body and to "blit
" each part:
class Snake(object):
# [...]
def draw_snake(self):
for snake_segment in self.body:
snake_segment.blit()
add a comment |
You've to traverse the snakes body in reverse order in the method movement
. For each part of the body, copy the position of the previous part. The head part gets the current position of the snake:
class Snake(object):
# [...]
def movement(self):
if self.direction == 'right':
self.move = (self.speed,0)
elif self.direction == 'left':
self.move = (-self.speed,0)
elif self.direction == 'up':
self.move = (0,-self.speed)
elif self.direction == 'down':
self.move = (0,self.speed)
self.rect.move_ip(self.move)
self.rect.clamp_ip(SCREEN.get_rect())
for i in range(len(self.body)-1, 0, -1):
self.body[i].rect.center = self.body[i-1].rect.center
if self.body:
self.body[0].rect.center = self.rect.center
Init the position of a part, when a new part is add to the body in the method add_segment
:
class Snake(object):
# [...]
def add_segment(self,food):
if len(self.body) == 0 or self.rect.colliderect(food.rect):
snake_segment = Snake(GREEN,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
snake_segment.rect.center = self.rect.center
self.body.append(snake_segment)
In the method draw_snake
it is sufficient to traverse the parts of the body and to "blit
" each part:
class Snake(object):
# [...]
def draw_snake(self):
for snake_segment in self.body:
snake_segment.blit()
You've to traverse the snakes body in reverse order in the method movement
. For each part of the body, copy the position of the previous part. The head part gets the current position of the snake:
class Snake(object):
# [...]
def movement(self):
if self.direction == 'right':
self.move = (self.speed,0)
elif self.direction == 'left':
self.move = (-self.speed,0)
elif self.direction == 'up':
self.move = (0,-self.speed)
elif self.direction == 'down':
self.move = (0,self.speed)
self.rect.move_ip(self.move)
self.rect.clamp_ip(SCREEN.get_rect())
for i in range(len(self.body)-1, 0, -1):
self.body[i].rect.center = self.body[i-1].rect.center
if self.body:
self.body[0].rect.center = self.rect.center
Init the position of a part, when a new part is add to the body in the method add_segment
:
class Snake(object):
# [...]
def add_segment(self,food):
if len(self.body) == 0 or self.rect.colliderect(food.rect):
snake_segment = Snake(GREEN,RECTANGLE_WIDTH,RECTANGLE_HEIGHT)
snake_segment.rect.center = self.rect.center
self.body.append(snake_segment)
In the method draw_snake
it is sufficient to traverse the parts of the body and to "blit
" each part:
class Snake(object):
# [...]
def draw_snake(self):
for snake_segment in self.body:
snake_segment.blit()
answered Mar 7 at 14:42
Rabbid76Rabbid76
41.4k123353
41.4k123353
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55036397%2fpygame-rect-move-ip-not-updating-rect-attribute%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What are you trying to achieve by remembering the turning point - a way to "slither" the snake around a corner?
– Kingsley
Mar 7 at 5:07
I store a turning point so I can change the direction adjust for and change the direction at that point. The segment is going down let's say and it passed through that point. Then it turns to the right once it's rect coordinates equal the coordinates of the turning point.
– Dovid Tor Sdayur
Mar 7 at 5:17
You don't really need to remember the point like this, just move the Nth body part to the position of the (N-1)th part. If this is what you're doing I answered a question explaining this a few days ago. stackoverflow.com/questions/54894355/… (But I'm not 100% sure this is what you mean, if not, please disregard this comment).
– Kingsley
Mar 7 at 5:36
I have looked at your comment. I will try that tomorrow.
– Dovid Tor Sdayur
Mar 7 at 5:41