Python Forum

Full Version: Simple randomised AI (NOT ai learning)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I made a simple AI with randomised movement. It will stay away from the edges of the screen.


import pygame
import random


pygame.init()

screen = pygame.display.set_mode((800, 700))
pygame.display.set_caption("Random AI")

run = True
x = 400
y = 350
vel = 10

#Main loop# 
while run:
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    

    
    pygame.draw.rect(screen, (255, 0, 0), (x, y, 20, 20))
    pygame.display.update()
    screen.fill((0, 0, 0))


#AI 
    if random.random() < 0.25:
        x -= vel

    if random.random() < 0.25:
        x+= vel

    if random.random() < 0.25:
        y-= vel

    if random.random() < 0.25:
        y+= vel
                
        
    if x > 700:
        x -= vel

    if x < 100:
        x -= vel

    if y > 600:
        y -= vel

    if y < 100:
        y += vel



pygame.quit()