NeoSyn 0 Newbie Poster

Been stumped over this for a while.. I was just wondering how I'd go about creating pattern movement and randomising it. The movemnt itself is just simple back and forward. But I wanted to make it random rather then fixed (which is what it is now) The main isn't posted here. I've explained some of my code below, if you need anymore explainations please let me know.

import pygame
pygame.init()

screen = pygame.display.set_mode((640, 480))

class ControlData:
    def __init__(self):
        self.moveLeft = 0
        self.moveRight = 0
        self.repeat = 0

class Enemy(pygame.sprite.Sprite):

    def __init__(self, screen):
        pygame.sprite.Sprite.__init__(self)

        self.screen = screen

        self.image = pygame.image.load("ArcherBody.bmp")
        tranColor = self.image.get_at((0, 0))
        self.image.set_colorkey(tranColor)
        pygame.transform.flip(self.image, 100, 100) 
        self.rect = self.image.get_rect()

        self.image = pygame.transform.flip(self.image, 1, 0)

        self.x = 510
        self.y = screen.get_height() - 140
        self.rect.center = (self.x, self.y)
        self.speed = 0

        pdr = ControlData()
        pdr.moveRight = 2
        pdr.repeat = 5

        pdl = ControlData()
        pdl.moveLeft = 2
        pdl.repeat = 5

        self.pattern = [pdr, pdl]
        self.patternIndex = 0
        self.repeatIndex = 0


    def update(self):

        self.x += self.speed
        self.rect.center = (self.x, self.y)


        'Update the sprite position using the array of pattern data'
        self.x += self.pattern[self.patternIndex].moveRight
        self.x -= self.pattern[self.patternIndex].moveLeft

        'Increment the patternIndex so that we move to the next piece of pattern data'
        if self.pattern[self.patternIndex].repeat == 0:
            'If there is no repeat in the ControlData then proceed to the next control data'
            self.patternIndex += 1
        elif self.repeatIndex >= self.pattern[self.patternIndex].repeat:
            'If we have repeated the ControlData enough times then proceed to the next' 
            'control data and reset the repeat index' 
            self.patternIndex += 1
            self.repeatIndex = 0
        else:
            'Otherwise just increment the repeat index'
            self.repeatIndex += 1

        'Reset the patternIndex if we are at the end of the array'
        if self.patternIndex >= len(self.pattern):
            self.patternIndex = 0

        if self.x > self.screen.get_width():
            self.x = 0
        if self.x < 0:
            self.x = self.screen.get_width()
        if self.y > self.screen.get_height():
            self.y = 0
        if self.y < 0:
            self.y = self.screen.get_height()

        self.rect.center = (self.x, self.y)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.