here I have the problem in line 68 of the code where I evaluate the string of code"snake_y=snake_y-14" how can I evaluate a string with the assignment because I have not seen any questions regarding assignment operations being used in eval() function
import pygame
import random
import time
import sys
def basic_defaults():
pygame.init()
snake_x = 200
snake_y = 175
font_for_game_title = pygame.font.Font(None, 36)
screen = pygame.display.set_mode((800, 600)) #Making Screen for the game
screen.blit(font_for_game_title.render("Snake Game", True, (0, 255, 255)), (300, 10))# Making title appear
Clock = pygame.time.Clock()
yolo=""
return screen, snake_x, snake_y,Clock,yolo
def snake_movements():
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
yolo = "snake_y = snake_y - 14"
return yolo
elif event.key == pygame.K_DOWN:
yolo = "snake_y = snake_y + 14"
return yolo
elif event.key == pygame.K_LEFT:
yolo = "snake_x = snake_x - 14"
return yolo
elif event.key == pygame.K_RIGHT:
yolo = "snake_x = snake_x + 14"
return yolo
else:
#yolo = "snake_x += 14"
#return yolo
pass
def game_loop():
screen, snake_x, snake_y,clocko,yolo = basic_defaults()
running = True
while (running):
#for moving of snake in the game [code starts]
screen.fill((0, 0, 0))
'''for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
yolo="snake_y - 14"
return yolo
elif event.key == pygame.K_DOWN:
yolo="snake_y + 14"
return yolo
elif event.key == pygame.K_LEFT:
yolo="snake_x - 14"
return yolo
elif event.key == pygame.K_RIGHT:
yolo="snake_x + 14"
return yolo
else:
pass
'''
yolo=snake_movements()
if yolo == None:
pass
else:
#print(type(yolo))
print(yolo)
eval(yolo)
#exec(yolo)
print(snake_x, snake_y)
pygame.draw.rect(screen, (102, 255, 51), (snake_x, snake_y, 10, 10), 4)
pygame.display.update()
#for moving of snake in the game [code ends]
time.sleep(1)
clocko.tick(30)
game_loop()
please help fast as I need to work on the code and need to complete this task thanks in advance