Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Changing the food (square) into image - snake game

testoviron420

New Coder
Hello,

Hopefully someone will be able to help me on this. I was trying to change the square into apple image however unsuccessfully. Not sure how I can do this. Below pasted some of my code. Grateful for any suggestions.


Python:
def init_vars():
    global head_pos, snake_body, food_pos, food_spawn, score, direction
    direction = "RIGHT"
    head_pos = [120, 60]
    snake_body = [[120,60]]
    food_pos = [random.randrange(1,(frame_size_x // square_size)) * square_size, random.randrange(1,(frame_size_y // square_size)) * square_size]
    food_spawn = True
    score = 0

init_vars()

def show_score(choice, color, font, size):
    score_font = pygame.font.SysFont(font, size)
    score_surface = score_font.render("Twuj wynik: " + str(score), True, color)
    score_rect = score_surface.get_rect()
    if choice == 1:
        score_rect.midtop = (frame_size_x / 10, 15)
    else:
        score_rect.midtop = (frame_size_x/2, frame_size_y/1.25)

    game_window.blit(score_surface, score_rect)

#game loop

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if ( event.key == pygame.K_UP or event.key == ord("w")
                and direction != "DOWN"):
                direction = "UP"
            elif ( event.key == pygame.K_DOWN or event.key == ord("s")
                and direction != "UP"):
                direction = "DOWN"
            elif ( event.key == pygame.K_LEFT or event.key == ord("a")
                and direction != "RIGHT"):
                direction = "LEFT"
            elif ( event.key == pygame.K_RIGHT or event.key == ord("d")
                and direction != "LEFT"):
                direction = "RIGHT"

    if direction == "UP":
        head_pos[1] -= square_size
    elif direction == "DOWN":
        head_pos[1] += square_size
    elif direction == "LEFT":
        head_pos[0] -= square_size
    else:
        head_pos[0] += square_size

    if head_pos[0] < 0:
        head_pos[0] = frame_size_x - square_size
    elif head_pos[0] > frame_size_x - square_size:
        head_pos[0] = 0
    elif head_pos[1] < 0:
        head_pos[1] = frame_size_y - square_size
    elif head_pos[1] > frame_size_y - square_size:
        head_pos[1] = 0

    #eating apple
    snake_body.insert(0, list(head_pos))
    if head_pos[0] == food_pos[0] and head_pos[1] == food_pos[1]:
        score += 1
        speed += 1
        food_spawn = False
    else:
        snake_body.pop()
    
    #food spawn
    if not food_spawn:
        food_pos = [random.randrange(1,(frame_size_x // square_size)) * square_size, random.randrange(1,(frame_size_y // square_size)) * square_size]
        food_spawn = True
    # GFX

    game_window.fill(black)
    for pos in snake_body:
        pygame.draw.rect(game_window, white, pygame.Rect(pos[0] + 2, pos[1] + 2, square_size -2, square_size))

    pygame.draw.rect(game_window, red, pygame.Rect(food_pos[0], food_pos[1], square_size, square_size))
 
The code you provided is not a working code but, one way is to use pygame_emojis. This will provide an apple emoji. Here is a quick example.
Python:
import pygame
from pygame_emojis import load_emoji

window = pygame.display.set_mode((500, 500))
size = (24, 24)

apple = load_emoji('\U0001F34E', size)

active = True

while active:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            active = False
    window.blit(apple, (50,100))
    pygame.display.update()

output
 

Attachments

  • pygame_emoji_apple.png
    pygame_emoji_apple.png
    6.9 KB · Views: 3

New Threads

Buy us a coffee!

Back
Top Bottom