[Pygame] Figuring out AI

[Pygame] Figuring out AI

Postby metulburr » Thu May 09, 2013 1:27 am

I am not really sure where to start AI. I mean i have a so called enemy ship bare bones ( havent doesnt collision detection yet), but exactly what to do now is more of a blank?

I could think of a ton of layouts, like enemy ship can dart away from player's shots, but then at what point would you ever hit him? Or enemy ship hover above player just shooting at whatever angle the player is until the player destroys the enemy. Or the enemy shoots at the player while slowly doing a kamikaze and blows both you and him up.

I dont know


Code: Select all
import pygame

def image_from_url(url):
    try:
        from urllib2 import urlopen
        from cStringIO import StringIO as inout
    except ImportError:
        from urllib.request import urlopen
        from io import BytesIO as inout
    myurl = urlopen(url)
    return inout(myurl.read())

class Projectile:
    limit = 3
    def __init__(self, loc):
        self.surf = pygame.Surface((5,10)).convert()
        self.surf.fill((255,255,255))
        self.rect = self.surf.get_rect()
        self.rect.center = loc
        self.speed = 8
       
class Enemy:
    def __init__(self):
        link = 'http://i1297.photobucket.com/albums/ag23/metulburr/spaceship_enemy3_zpscfeb13bf.png'
        self.orig_image = pygame.image.load(image_from_url(link)).convert()
        self.orig_image = pygame.transform.scale(self.orig_image, (40,80))
        self.orig_image.set_colorkey((255,0,255))
        self.image = pygame.transform.rotate(self.orig_image, 180)
        self.mask = pygame.mask.from_surface(self.image)
        self.rect = self.image.get_rect()
       
       

class Player:
    def __init__(self):
        self.speed = 4
       
        link = 'http://i1297.photobucket.com/albums/ag23/metulburr/spaceship2_zps095c332a.png'
        self.orig_image = pygame.image.load(image_from_url(link)).convert()
        self.orig_image = pygame.transform.scale(self.orig_image, (40,80))
        self.orig_image.set_colorkey((255,0,255))
       
        self.image = pygame.transform.rotate(self.orig_image, 180)
        self.mask = pygame.mask.from_surface(self.image)
        self.rect = self.image.get_rect()
       
        self.bullets = []
       
    def move(self, x, y):
        self.rect[0] += x * self.speed
        self.rect[1] += y * self.speed

       
class Control:
    def __init__(self):
        self.screensize = (800,600)
        self.screen = pygame.display.set_mode(self.screensize)
        self.screenrect = self.screen.get_rect()
        self.clock = pygame.time.Clock()
        self.player = Player()
        self.enemy = Enemy()
 
       
        self.mainloop()
       
    def mainloop(self):
        run = True
        while run:
            self.screen.fill((0,0,0))
           
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_SPACE:
                        if len(self.player.bullets) <= Projectile.limit:
                            self.player.bullets.append(Projectile(self.player.rect.center))
           
            self.update()
            pygame.display.flip()
            self.clock.tick(60)
           
    def update(self):
        self.keys = pygame.key.get_pressed()

        if self.player.bullets:
            for obj in self.player.bullets:
                self.screen.blit(obj.surf, obj.rect)
                obj.rect[1] -= obj.speed
                #obj.loc = (obj.rect[0], obj.rect[1] - obj.speed)
 
                if obj.rect[1] < 0:
                    self.player.bullets.remove(obj)
       
        if self.keys[pygame.K_UP]:
            self.player.move(0,-1)
        if self.keys[pygame.K_DOWN]:
            self.player.move(0,1)
        if self.keys[pygame.K_RIGHT]:
            self.player.move(1,0)
        if self.keys[pygame.K_LEFT]:
            self.player.move(-1,0)
       
        self.player.rect.clamp_ip(self.screenrect) #make sure rect does not go outside of screen
        self.screen.blit(self.enemy.image, self.enemy.rect)
        self.screen.blit(self.player.image, self.player.rect)
           
app = Control()
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] Figuring out AI

Postby Mekire » Thu May 09, 2013 2:02 am

Hmm... I am fairly novice when it comes to good AI myself. One suggestion I have, although it probably won't directly pertain to the AI you end up using here, is to learn how to implement an efficient A* algorithm. This is the fundamentals of most basic pathfinding.

Specifically for this game I would again say to try to look through Paul Paterson's QANAT code, and see if you can't find his AI. He says that, "The enemies have a few simple rules that determine their motion and one of those is to avoid your bullets. This turns out to make them behave in quite interesting ways, particularly when they are in groups! "

Finding that medium between a seemingly intelligent opponent, and one that is impossible, is the name of the game.
-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: [Pygame] Figuring out AI

Postby metulburr » Thu May 09, 2013 2:57 am

looked through his code:

looks like random to drop bombs:
Code: Select all
        if not self.crashing and random.random() < self.bomb_prob:
            self.dropBomb()


but i dont really see the method he used to at what point does the enemy get hit opposed to veer away from the players shot

although i do have a method of possibly mimicing it, in theory.
random selection of speed for enemy to move out of the way of bullet. So it would appear to always attempt to get out of the way, but for slow velocity increaments, it would not and get destroyed whereas the faster speed would succeed in mocing out of the way of the players bullet. In that way, they dont just sit there like idiots waiting to die, at least "looks" like they are trying to get out of the way.

ACtually that might be what i implement and see what becomes of it.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1476
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: [Pygame] Figuring out AI

Postby Mekire » Thu May 09, 2013 3:19 am

Sounds like a good plan at least to start with. I think the idea of setting probability rates for given events and then using random.random() is also worth investigating, in fact I will probably be doing just this when I rewrite my Cabbages game and AIs.

-Mek
User avatar
Mekire
 
Posts: 988
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to Game Development

Who is online

Users browsing this forum: No registered users and 0 guests