I have tried many sites but still cannot come up with anything. All online tutorials are not helpful. I have just started learning python and every time I try to move it goes out of the border. I have tried basically all online guide and came across an error...........................
pygame.init()
screen_width = 800
screen_height = 600
window = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Test')
bg_color1 = (135,142,142) #MAIN BG COLOR
bg_color2 = (255,0,0) #red
bg_color3 = (255,255,0) #yellow
clock = pygame.time.Clock()
crashed = False
UFO = pygame.image.load('ufo.png')
rect = UFO.get_rect()
obstacle = pygame.Rect(400, 200, 80, 80)
def car(x, y):
window.blit(UFO, (x, y))
x = (screen_width * 0.45)
y = (screen_height * 0.8)
x_change = 0
car_speed = 0
y_change = 0
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
############SIDE TO SIDE################
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -5
elif event.key == pygame.K_RIGHT:
x_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_change = 0
###########UP AND DOWN#################
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_change = -5
elif event.key == pygame.K_DOWN:
y_change = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
##
x += x_change
y += y_change
##
window.fill(bg_color1)
car(x, y)
pygame.display.update()
clock.tick(100)
pygame.quit()
quit()