def __init__(self, radius, speed, pos_init, SCREEN_WIDTH, SCREEN_HEIGHT): pygame.sprite.Sprite.__init__(self) self.radius = radius self.speed = speed self.pos = vec2d(pos_init) self.vel = vec2d((speed, -1.0*speed)) self.SCREEN_HEIGHT = SCREEN_HEIGHT self.SCREEN_WIDTH = SCREEN_WIDTH image = pygame.Surface((radius*2, radius*2)) image.fill((0, 0, 0, 0)) image.set_colorkey((0,0,0)) pygame.draw.circle( image, (255, 255, 255), (radius, radius), radius, 0 ) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def __init__(self, speed, rect_width, rect_height, pos_init, SCREEN_WIDTH, SCREEN_HEIGHT): pygame.sprite.Sprite.__init__(self) self.speed = speed self.pos = vec2d(pos_init) self.vel = vec2d((0, 0)) self.rect_height = rect_height self.rect_width = rect_width self.SCREEN_HEIGHT = SCREEN_HEIGHT self.SCREEN_WIDTH = SCREEN_WIDTH image = pygame.Surface((rect_width, rect_height)) image.fill((0, 0, 0, 0)) image.set_colorkey((0,0,0)) pygame.draw.rect( image, (255, 255, 255), (0, 0, rect_width, rect_height), 0 ) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def __init__(self, speed, length, pos_init, width, color, SCREEN_WIDTH, SCREEN_HEIGHT): self.dir = vec2d((1, 0)) self.speed = speed self.pos = vec2d(pos_init) self.color = color self.width = width self.length = length self.body = [] self.update_head = True # build our body up for i in range(self.length): self.body.append( # makes a neat "zapping" in effect SnakeSegment( (self.pos.x - (width) * i, self.pos.y), self.width, self.width, tuple([c - 100 for c in self.color] ) if i == 0 else self.color ) ) # we dont add the first few because it cause never actually hit it self.body_group = pygame.sprite.Group() self.head = self.body[0]
def __init__(self, color, radius, pos_init, dir_init, speed, reward, TYPE, SCREEN_WIDTH, SCREEN_HEIGHT): pygame.sprite.Sprite.__init__(self) self.SCREEN_WIDTH = SCREEN_WIDTH self.SCREEN_HEIGHT = SCREEN_HEIGHT self.TYPE = TYPE self.speed = speed self.reward = reward self.radius = radius self.pos = vec2d(pos_init) self.direction = vec2d(dir_init) self.direction.normalize() #normalized image = pygame.Surface((radius * 2, radius * 2)) image.fill((0, 0, 0, 0)) image.set_colorkey((0, 0, 0)) pygame.draw.circle(image, color, (radius, radius), radius, 0) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def init(self): """ Starts/Resets the game to its inital state """ self.creep_counts = {"GOOD": 0, "BAD": 0} if self.player is None: self.player = Player( self.AGENT_RADIUS, self.AGENT_COLOR, self.AGENT_SPEED, self.AGENT_INIT_POS, self.width, self.height ) else: self.player.pos = vec2d(self.AGENT_INIT_POS) self.player.vel = vec2d((0.0, 0.0)) if self.creeps is None: self.creeps = pygame.sprite.Group() else: self.creeps.empty() for i in range(self.N_CREEPS): self._add_creep() self.score = 0 self.ticks = 0 self.lives = -1
def __init__(self, speed, rect_width, rect_height, pos_init, SCREEN_WIDTH, SCREEN_HEIGHT): pygame.sprite.Sprite.__init__(self) self.speed = speed self.pos = vec2d(pos_init) self.vel = vec2d((0, 0)) self.rect_height = rect_height self.rect_width = rect_width self.SCREEN_HEIGHT = SCREEN_HEIGHT self.SCREEN_WIDTH = SCREEN_WIDTH image = pygame.Surface((rect_width, rect_height)) image.fill((0, 0, 0, 0)) image.set_colorkey((0, 0, 0)) pygame.draw.rect( image, (255, 255, 255), (0, 0, rect_width, rect_height), 0 ) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def __init__(self, radius, color, speed, pos_init, SCREEN_WIDTH, SCREEN_HEIGHT): pygame.sprite.Sprite.__init__(self) self.SCREEN_WIDTH = SCREEN_WIDTH self.SCREEN_HEIGHT = SCREEN_HEIGHT self.pos = vec2d(pos_init) self.vel = vec2d((0, 0)) image = pygame.Surface([radius * 2, radius * 2]) image.set_colorkey((0, 0, 0)) pygame.draw.circle( image, color, (radius, radius), radius, 0 ) self.image = image.convert() self.rect = self.image.get_rect() self.radius = radius
def __init__(self, radius, speed, pos_init, SCREEN_WIDTH, SCREEN_HEIGHT): pygame.sprite.Sprite.__init__(self) self.radius = radius self.speed = speed self.pos = vec2d(pos_init) self.vel = vec2d((speed, -1.0 * speed)) self.SCREEN_HEIGHT = SCREEN_HEIGHT self.SCREEN_WIDTH = SCREEN_WIDTH image = pygame.Surface((radius * 2, radius * 2)) image.fill((0, 0, 0, 0)) image.set_colorkey((0, 0, 0)) pygame.draw.circle( image, (255, 255, 255), (radius, radius), radius, 0 ) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def _handle_player_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: key = event.key #left = -1 #right = 1 #up = -1 #down = 1 if key == self.actions["left"] and self.player.dir.x != 1: self.player.dir = vec2d((-1, 0)) if key == self.actions["right"] and self.player.dir.x != -1: self.player.dir = vec2d((1, 0)) if key == self.actions["up"] and self.player.dir.y != 1: self.player.dir = vec2d((0, -1)) if key == self.actions["down"] and self.player.dir.y != -1: self.player.dir = vec2d((0, 1))
def _handle_player_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: key = event.key #left = -1 #right = 1 #up = -1 #down = 1 if key == self.actions["left"] and self.player.dir.x != 1: self.player.dir = vec2d((-1, 0)) if key == self.actions["right"] and self.player.dir.x != -1: self.player.dir = vec2d((1, 0)) if key == self.actions["up"] and self.player.dir.y != 1: self.player.dir = vec2d((0, -1)) if key == self.actions["down"] and self.player.dir.y != -1: self.player.dir = vec2d((0, 1)) self.player.update_head = True
def __init__(self, pos_init, attr, SCREEN_WIDTH, SCREEN_HEIGHT): pygame.sprite.Sprite.__init__(self) self.pos = vec2d(pos_init) self.attr = attr self.SCREEN_WIDTH = SCREEN_WIDTH self.SCREEN_HEIGHT = SCREEN_HEIGHT image = pygame.Surface( (self.attr["radius_outer"] * 2, self.attr["radius_outer"] * 2)) image.fill((0, 0, 0, 0)) image.set_colorkey((0, 0, 0)) pygame.draw.circle( image, self.attr["color_outer"], (self.attr["radius_outer"], self.attr["radius_outer"]), self.attr["radius_outer"], 0) image.set_alpha(int(255 * 0.75)) pygame.draw.circle( image, self.attr["color_center"], (self.attr["radius_outer"], self.attr["radius_outer"]), self.attr["radius_center"], 0) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def __init__(self, pos_init, speed, SCREEN_WIDTH, SCREEN_HEIGHT): pygame.sprite.Sprite.__init__(self) self.pos = vec2d(pos_init) self.width = int(SCREEN_WIDTH*0.1) self.height = int(SCREEN_HEIGHT*0.2) self.speed = speed self.SCREEN_WIDTH = SCREEN_WIDTH self.SCREEN_HEIGHT = SCREEN_HEIGHT image = pygame.Surface((self.width, self.height)) image.fill((0, 0, 0, 0)) image.set_colorkey((0,0,0)) pygame.draw.rect( image, (120, 240, 80), (0, 0, self.width, self.height), 0 ) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def __init__(self, speed, SCREEN_WIDTH, SCREEN_HEIGHT): pygame.sprite.Sprite.__init__(self) pos_init = ( int( SCREEN_WIDTH*0.35 ), SCREEN_HEIGHT/2 ) self.pos = vec2d(pos_init) self.speed = speed self.climb_speed = speed*-0.875 #-0.0175 self.fall_speed = speed*0.09 #0.0019 self.momentum = 0 self.width = SCREEN_WIDTH*0.05 self.height = SCREEN_HEIGHT*0.05 image = pygame.Surface((self.width, self.height)) image.fill((0, 0, 0, 0)) image.set_colorkey((0,0,0)) pygame.draw.rect( image, (255, 255, 255), (0, 0, self.width, self.height), 0 ) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def __init__(self, pos_init, width, color, SCREEN_WIDTH, SCREEN_HEIGHT, rng): pygame.sprite.Sprite.__init__(self) self.pos = vec2d(pos_init) self.color = color self.SCREEN_WIDTH = SCREEN_WIDTH self.SCREEN_HEIGHT = SCREEN_HEIGHT self.width = width self.rng = rng image = pygame.Surface((width, width)) image.fill((0, 0, 0, 0)) image.set_colorkey((0, 0, 0)) pygame.draw.rect( image, color, (0, 0, self.width, self.width), 0 ) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def __init__(self, pos_init, attr, SCREEN_WIDTH, SCREEN_HEIGHT): pygame.sprite.Sprite.__init__(self) self.pos = vec2d(pos_init) self.attr = attr self.SCREEN_WIDTH = SCREEN_WIDTH self.SCREEN_HEIGHT = SCREEN_HEIGHT image = pygame.Surface((self.attr["radius_outer"]*2, self.attr["radius_outer"]*2)) image.fill((0, 0, 0, 0)) image.set_colorkey((0,0,0)) pygame.draw.circle( image, self.attr["color_outer"], (self.attr["radius_outer"], self.attr["radius_outer"]), self.attr["radius_outer"], 0 ) image.set_alpha(int(255*0.75)) pygame.draw.circle( image, self.attr["color_center"], (self.attr["radius_outer"],self.attr["radius_outer"]), self.attr["radius_center"], 0 ) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def __init__(self, pos_init, speed, SCREEN_WIDTH, SCREEN_HEIGHT): pygame.sprite.Sprite.__init__(self) self.pos = vec2d(pos_init) self.speed = speed self.width = int(SCREEN_WIDTH*0.1) image = pygame.Surface((self.width, SCREEN_HEIGHT*1.5)) image.fill((0, 0, 0, 0)) image.set_colorkey((0,0,0)) color = (120, 240, 80) #top rect pygame.draw.rect( image, color, (0, 0, self.width, SCREEN_HEIGHT*0.5), 0 ) #bot rect pygame.draw.rect( image, color, (0, SCREEN_HEIGHT*1.05, self.width, SCREEN_HEIGHT*0.5), 0 ) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def update(self, dt): for i in range(self.length - 1, 0, -1): self.body[i].pos = vec2d( ((0.9 * self.body[i - 1].pos.x + 0.1 * self.body[i].pos.x), (0.9 * self.body[i - 1].pos.y + 0.1 * self.body[i].pos.y))) self.head.pos.x += self.dir.x * self.speed * dt self.head.pos.y += self.dir.y * self.speed * dt
def update(self, dt): for i in range(self.length-1, 0, -1): self.body[i].pos = vec2d(( (0.9*self.body[i-1].pos.x+0.1*self.body[i].pos.x), (0.9*self.body[i-1].pos.y+0.1*self.body[i].pos.y) )) self.head.pos.x += self.dir.x*self.speed*dt self.head.pos.y += self.dir.y*self.speed*dt
def __init__(self, color, radius, pos_init, dir_init, speed, reward, TYPE, SCREEN_WIDTH, SCREEN_HEIGHT, jitter_speed): pygame.sprite.Sprite.__init__(self) self.SCREEN_WIDTH = SCREEN_WIDTH self.SCREEN_HEIGHT = SCREEN_HEIGHT self.TYPE = TYPE self.jitter_speed = jitter_speed self.speed = speed self.reward = reward self.radius = radius self.pos = vec2d(pos_init) self.direction = vec2d(dir_init) self.direction.normalize() # normalized image = pygame.Surface((radius * 2, radius * 2)) image.fill((0, 0, 0)) image.set_colorkey((0, 0, 0)) pygame.draw.circle( image, color, (radius, radius), radius, 0 ) self.image = image.convert() self.rect = self.image.get_rect() self.rect.center = pos_init
def __init__(self, speed, length, pos_init, width, color, SCREEN_WIDTH, SCREEN_HEIGHT): self.dir = vec2d((1, 0)) self.speed = speed self.pos = vec2d(pos_init) self.color = color self.width = width self.length = length self.body = [] #build our body up for i in range(self.length): self.body.append( #makes a neat "zapping" in effect SnakeSegment( (self.pos.x - (width) * i, self.pos.y), self.width, tuple([c - 100 for c in self.color]) if i == 0 else self.color)) #we dont add the first few because it cause never actually hit it self.body_group = pygame.sprite.Group() self.head = self.body[0]
def __init__(self, pos, w, h): pygame.sprite.Sprite.__init__(self) self.pos = vec2d(pos) self.w = w self.h = h image = pygame.Surface([w, h]) image.fill((10, 10, 10)) self.image = image.convert() self.rect = self.image.get_rect() self.rect.center = pos
def update(self, dt): for i in range(self.length-1, 0, -1): scale = 0.1 self.body[i].pos = vec2d(( ((1.0-scale)*self.body[i-1].pos.x+scale*self.body[i].pos.x), ((1.0-scale)*self.body[i-1].pos.y+scale*self.body[i].pos.y) )) self.body[i].rect.center = (self.body[i].pos.x, self.body[i].pos.y) self.head.pos.x += self.dir.x*self.speed*dt self.head.pos.y += self.dir.y*self.speed*dt self.update_hitbox()
def update(self, dt): for i in range(self.length - 1, 0, -1): scale = 0.1 self.body[i].pos = vec2d((((1.0 - scale) * self.body[i - 1].pos.x + scale * self.body[i].pos.x), ((1.0 - scale) * self.body[i - 1].pos.y + scale * self.body[i].pos.y))) self.body[i].rect.center = (self.body[i].pos.x, self.body[i].pos.y) self.head.pos.x += self.dir.x * self.speed * dt self.head.pos.y += self.dir.y * self.speed * dt self.update_hitbox()
def new_position(self, snake): new_pos = snake.body[0].pos snake_body = [s.pos for s in snake.body] while (new_pos in snake_body): _x = random.randrange(self.width * 2, self.SCREEN_WIDTH - self.width * 2, self.width) _y = random.randrange(self.width * 2, self.SCREEN_HEIGHT - self.width * 2, self.width) new_pos = vec2d((_x, _y)) self.pos = new_pos self.rect.center = (self.pos.x, self.pos.y)
def __init__(self, pos_init, width, color): pygame.sprite.Sprite.__init__(self) self.pos = vec2d(pos_init) self.color = color self.width = width image = pygame.Surface((width, width)) image.fill((0, 0, 0)) image.set_colorkey((0, 0, 0)) pygame.draw.rect(image, color, (0, 0, self.width, self.width), 0) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def new_position(self, snake): new_pos = snake.body[0].pos snake_body = [s.pos for s in snake.body] while (new_pos in snake_body): _x = self.rng.choice(range( self.width * 2, self.SCREEN_WIDTH - self.width * 2, self.width )) _y = self.rng.choice(range( self.width * 2, self.SCREEN_HEIGHT - self.width * 2, self.width )) new_pos = vec2d((_x, _y)) self.pos = new_pos self.rect.center = (self.pos.x, self.pos.y)
def update(self, dt): jitter_x, jitter_y = 0, 0 if self.pos.x - self.radius > self.SCREEN_WIDTH - 1 or self.pos.x <= self.radius * 3: self.direction.x *= -1 jitter_x = self.jitter_speed * self.direction.x * random() if self.pos.y - self.radius > self.SCREEN_HEIGHT - 1 or self.pos.y <= self.radius * 3: self.direction.y *= -1 jitter_y = self.jitter_speed * self.direction.y * random() displacement = vec2d((self.direction.x * self.speed * dt, self.direction.y * self.speed * dt)) self.pos += displacement self.rect.center = ((self.pos.x - self.image.get_size()[0], self.pos.y - self.image.get_size()[1]))
def __init__(self, pos_init, width, height, color): pygame.sprite.Sprite.__init__(self) self.pos = vec2d(pos_init) self.color = color self.width = width self.height = height image = pygame.Surface((width, height)) image.fill((0, 0, 0)) image.set_colorkey((0, 0, 0)) pygame.draw.rect(image, color, (0, 0, self.width, self.height), 0) self.image = image # use half the size self.rect = pygame.Rect(pos_init, (self.width / 2, self.height / 2)) self.rect.center = pos_init
def update(self, dt): jitter_x, jitter_y = 0, 0 if self.pos.x-self.radius > self.SCREEN_WIDTH-1 or self.pos.x <= self.radius*3: self.direction.x *= -1 jitter_x = self.jitter_speed*self.direction.x*random() if self.pos.y-self.radius > self.SCREEN_HEIGHT-1 or self.pos.y <= self.radius*3: self.direction.y *= -1 jitter_y = self.jitter_speed*self.direction.y*random() displacement = vec2d(( self.direction.x * self.speed * dt, self.direction.y * self.speed * dt)) self.pos += displacement self.rect.center = (( self.pos.x - self.image.get_size()[0], self.pos.y - self.image.get_size()[1]))
def __init__(self, pos_init, width, color, SCREEN_WIDTH, SCREEN_HEIGHT, rng): pygame.sprite.Sprite.__init__(self) self.pos = vec2d(pos_init) self.color = color self.SCREEN_WIDTH = SCREEN_WIDTH self.SCREEN_HEIGHT = SCREEN_HEIGHT self.width = width self.rng = rng image = pygame.Surface((width, width)) image.fill((0, 0, 0, 0)) image.set_colorkey((0, 0, 0)) pygame.draw.rect(image, color, (0, 0, self.width, self.width), 0) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def __init__(self, pos_init, width, color): pygame.sprite.Sprite.__init__(self) self.pos = vec2d(pos_init) self.color = color self.width = width image = pygame.Surface((width, width)) image.fill((0,0,0)) image.set_colorkey((0,0,0)) pygame.draw.rect( image, color, (0, 0, self.width, self.width), 0 ) self.image = image self.rect = self.image.get_rect() self.rect.center = pos_init
def __init__(self, pos_init, width, height, color): pygame.sprite.Sprite.__init__(self) self.pos = vec2d(pos_init) self.color = color self.width = width self.height = height image = pygame.Surface((width, height)) image.fill((0, 0, 0)) image.set_colorkey((0, 0, 0)) pygame.draw.rect( image, color, (0, 0, self.width, self.height), 0 ) self.image = image # use half the size self.rect = pygame.Rect(pos_init, (self.width / 2, self.height / 2)) self.rect.center = pos_init