Python Forum
Simple randomised AI (NOT ai learning)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple randomised AI (NOT ai learning)
#1
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()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020