T
T
Tim2452020-07-26 16:45:32
Python
Tim245, 2020-07-26 16:45:32

Why doesn't it disappear when the green square touches the red one?

I’m making a game like a snake, but I can’t understand why it doesn’t enter the condition here

if (snake_pos['x'] == food_pos["x"]
            and snake_pos['y'] == food_pos["y"]):
        print(123)
        food_eaten += 1
        snake_tails.append([food_pos["x"], food_pos["y"]])
        food_pos = {
          "x": round(random.randrange(0, widht - snake_size[0]) / 10) * 10,
          "y": round(random.randrange(0, hight - snake_size[1]) / 10) * 10,
        }

and the code is below
import pygame
import random
pygame.init()
widht = 640
hight = 480
display = pygame.display.set_mode((widht, hight))
pygame.display.update()
pygame.display.set_caption("Змейка по андроид ОТ тимура")
game_end = False
colors = {
  "snake_head": (0, 255, 0),
  "snake_tail": (0, 255, 0),
  "apple": (255, 0, 0)
}
snake_pos = {
  'x': widht/2-10,
  'y': hight/2-5,
  "x_change": 0,
  "y_change": 0
}

snake_size = (10, 10)

snake_tails = []

food_pos = {
    "x" : round(random.randrange(0, widht - snake_size[0]) / 10) * 10,
    "y" : round(random.randrange(0, hight - snake_size[1]) / 10) * 10
}

food_size = (10, 10)
food_eaten = 0



snake_speed = 0.01
while not game_end:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_end = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                snake_pos['x_change'] = -snake_speed
                snake_pos['y_change'] = 0
            elif event.key == pygame.K_RIGHT:
                snake_pos['x_change'] = snake_speed
                snake_pos['y_change'] = 0
            elif event.key == pygame.K_UP:
                snake_pos['x_change'] = 0
                snake_pos['y_change'] = -snake_speed
            elif event.key == pygame.K_DOWN:
                snake_pos['x_change'] = 0
                snake_pos['y_change'] = snake_speed
    display.fill((0, 0, 0))

    ltx = snake_pos['x']
    lty = snake_pos['y']


    for i, v in enumerate(snake_tails):
        _ltx = snake_tails[i][0]
        _lty = snake_tails[i][1]
        snake_tails[i][0] = ltx
        snake_tails[i][1] = lty

        ltx = _ltx
        lty = _lty
    for t in snake_tails:
        pygame.draw.rect(display, colors['snake_tail'], [
            t[0],
            t[1],
            snake_size[0],
            snake_size[1]])
    snake_pos['x'] += snake_pos['x_change']
    snake_pos['y'] += snake_pos['y_change']
    if (snake_pos['x'] < -snake_size[0]):
        snake_pos['x'] = widht
    elif (snake_pos['x'] > widht):
        snake_pos['x'] = 0
    elif (snake_pos['y'] < -snake_size[1]):
        snake_pos['y'] = hight
    elif (snake_pos['y'] > hight):
        snake_pos['y'] = 0
    pygame.draw.rect(display, colors["snake_head"], [
        snake_pos['x'],
        snake_pos['y'],
        snake_size[0],
        snake_size[1]])


    pygame.draw.rect(display, colors['apple'], [
        food_pos["x"],
        food_pos["y"],
        snake_size[0],
        snake_size[1]])

    if (snake_pos['x'] == food_pos["x"]
            and snake_pos['y'] == food_pos["y"]):
        print(123)
        food_eaten += 1
        snake_tails.append([food_pos["x"], food_pos["y"]])
        food_pos = {
          "x": round(random.randrange(0, widht - snake_size[0]) / 10) * 10,
          "y": round(random.randrange(0, hight - snake_size[1]) / 10) * 10,
        }


    pygame.display.update()





pygame.quit()
quit()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
ScriptKiddo, 2020-07-26
@ScriptKiddo

Snake x,y: 371.97999999994363, 308.3599999999333. Fodd x,y: 370, 310
Snake x,y: 371.97999999994363, 308.36999999993327. Fodd x,y: 370, 310

The easiest way is to pin something like this
int(snake_pos['x'])
But the snake is mad and jumps over several pixels in one click. Might be an interval

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question