Simple randomised AI (NOT ai learning) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: Code sharing (https://python-forum.io/forum-5.html) +--- Thread: Simple randomised AI (NOT ai learning) (/thread-24723.html) |
Simple randomised AI (NOT ai learning) - noodlespinbot - Mar-01-2020 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() |