0

so im making a simple snake game in pygame for food i have designed an apple myself and now i want snake to eat that apple which is actually an image obviously. its just i dont know how to combine that image with random generation of food.

this is the code

foodw = 10
foodh = 10

foodx = random.randint(0, 450)
foody = random.randint(0, 350)

if abs(x - foodx) <20 and abs(y - foody) <20:
scorevalue += 1
foodx = random.randint(30, 470)
foody = random.randint(30, 370)
snklen += 5

food = pygame.image.load('E:/snkae/apple.png')
screen.blit(food, [foodw, foodh])
ali abid
  • 11
  • 2
  • you code seems to correctly check if the snake is in the food position and then you reassign `foodx` and `foody`. What is the problem you are facing? You need to put the image of food in the coordinates `(foodx, foody)` –  May 20 '22 at 10:39
  • like should i type foodx, foody in place of foodw, foodh ?? screen.blit(food, [foodw, foodh]) @SembeiNorimaki – ali abid May 20 '22 at 10:49
  • yes. but you will need this inside some type of loop where you can move the snake and do this checks and reprinting regularly. and then the loading of the image has do be in some initializer outside the loop –  May 20 '22 at 10:52
  • and now ive found out that the apple actually is just placed beside the coordinates of foodx and foody like if i pass the snake from top left side of the apple still a few pixels away it eats the apple so now the problem is that somehow the apple image is not appearing directly over the foodx foody coordinate @SembeiNorimaki – ali abid May 20 '22 at 10:53
  • well, the parameters are probably the left corner of the image, so you will have to substract half of the image width and height. I'd recommend checking some pygame tutorials where you will get an idea of how to place images, make the main loop and use controls for interaction. –  May 20 '22 at 10:55
  • okok thanks i will figure out – ali abid May 20 '22 at 10:58

0 Answers0