def bootstrap_pygame(frame): # Set the variable to show PyGame where to place its window os.environ['SDL_WINDOWID'] = str(int(frame.winId())) wid = PygameWrapper.PygameWrapper(frame.width(), frame.height()) frame.wid = wid # Set the PyGame update loop be handled by PyQt4 frame.update_func = wid.main_loop frame.start_updates()
def __init__(self, posx = CENTRE[0], posy = CENTRE[1], bounceValue=0, pushValue=0, moveValue=0, pushSpeed = 0, pushOrientation = 0, dirXY = [0,0]): MovingObject.MovingObject.__init__(self) self.load_image() self.setBounceValue(bounceValue) self.setPushValue(pushValue) self.setMoveValue(moveValue) self.setPushSpeed(pushSpeed) self.setPushOrientation(pushOrientation) self.setDirXY(dirXY) # self.pitch = pygame.Rect(WALL_WIDTH, WALL_WIDTH, FIELD[0], FIELD[1]) self.pitch = pygw.rectangle(WALL_WIDTH, WALL_WIDTH, FIELD[0], FIELD[1]) self.pitch.center = CENTRE self.rect.center = (posx, posy) self.center = (posx, posy)
def update(self): self.update_AI() new_pos = self.rect.center dir_mod = 0 # if self.command is not 'Wait': # new_angle = self.new_angle() if self.command is 'TurnLeft': dir_mod = 1 elif self.command is 'TurnRight': dir_mod = -1 elif self.command is 'Forward': new_pos = self.new_pos() if self.command is 'Kick': self.kicking = 1 else: self.kicking = 0 # new_orientation = 0 new_orientation = self.orientation + dir_mod * self.speed_rotation if new_orientation < -180: new_orientation = 180 elif new_orientation > 180: new_orientation = -180 old_center = (new_pos[0], new_pos[1]) self.image = pygw.rotate(self.or_image, new_orientation) self.rect = self.image.get_rect() self.rect.center = old_center self.orientation = new_orientation self.angle = self.agent_to_abs(self.orientation) self.pos = new_pos
def __init__(self, posxy, playerid, angle, WMSubscriber): MovingObject.MovingObject.__init__(self) self.player_icon(playerid) self.load_image() self.rect.center = (posxy[0], posxy[1]) self.orientation = self.abs_to_agent(angle) self.or_image = self.image self.image = pygw.rotate(self.or_image, self.orientation) self.player_id = playerid self.pos = posxy self.angle = angle # ANGLE = [0 to 360 Clockwise] self.WorldModel = WMSubscriber self.AI = AIModule(self.player_id, self.WorldModel) self.speed_rotation = 0.5 self.speed_forward = 1.5 self.cnt = 30 self.command = 'Wait'
def main(): """This is the main function called when the program starts. It initializes everything it needs, then runs in a loop until exited. """ display = Display() background = display.drawBackground() display.drawPitch(background) display.centreTitleOnBackground(background) # Prepare Game Objects # clock = pygame.time.Clock() clock = pygw.clock() WM = WorldModel() ball = Ball() blue1 = Agent(BLUE1_START_POS, 1, BLUE_START_ANGLE, WM) blue2 = Agent(BLUE2_START_POS, 2, BLUE_START_ANGLE, WM) red1 = Agent(RED1_START_POS, 3, RED_START_ANGLE, WM) red2 = Agent(RED2_START_POS, 4, RED_START_ANGLE, WM) ball.setName("ball") blue1.setName("blue1") blue2.setName("blue2") red1.setName("red1") red2.setName("red2") # ballSprite = pygame.sprite.RenderPlain(ball) ballSprite = pygw.renderplainsprite(ball) blue1Sprite = pygw.renderplainsprite(blue1) blue2Sprite = pygw.renderplainsprite(blue2) red1Sprite = pygw.renderplainsprite(red1) red2Sprite = pygw.renderplainsprite(red2) frame = 0 going = True # Main game loop while going: clock.tick(FPS) if frame >= 30: frame = 0 else: frame += 1 allData = [ball, blue1, blue2, red1, red2] if (frame % WORLD_MODEL_UPDATE) == 0: WM.update_info(allData) #Update Sprites ballSprite.update() blue1Sprite.update() blue2Sprite.update() red1Sprite.update() red2Sprite.update() #Draw Everything display.drawEverything(background, ballSprite, blue1Sprite, blue2Sprite, red1Sprite, red2Sprite) display.updateFeaturesOnScreen(frame, ball, blue1, blue2, red1, red2) #Check for kicks ball.setPushValue(0) if blue1.kicking or blue2.kicking or red1.kicking or red2.kicking: ball.setPushValue(1) ball.setPushSpeed(5) if blue1.kicking: ball.setPushOrientation(blue1.angle) elif blue2.kicking: ball.setPushOrientation(blue2.angle) elif red1.kicking: ball.setPushOrientation(red1.angle) elif red2.kicking: ball.setPushOrientation(red2.angle) # # ball.setPushValue(0) # # if ball.speed == 0: # ball.setPushValue(1) # ball.setPushOrientation(np.random.randint(0, 360)) # ball.setPushSpeed(5) # pygame.display.flip() pygw.updatefulldisplay() # for event in pygame.event.get(): for event in pygw.getIOevent(): if event.type == pygw.QUIT or event.type == pygw.KEYDOWN and event.key == pygw.K_ESCAPE: going = False print('User quit the game') # pygame.quit() pygw.quitgame() sys.exit()
def load_image(self): self.image, self.rect = Loader.load_image(self.name) self.image = pygw.transform(self.image, (75, 75))
def load_image(self): self.image, self.rect = Loader.load_image('ball.png', -1) # self.image = pygame.transform.scale(self.image, (25, 25)) self.image = pygw.transform(self.image, (25,25))