def __init__(self, numero, numeroPlayer, initialPos, vector, dmg, sizeMultiplier, nImagen): self.n = numero self.numeroPlayer = numeroPlayer self.vector = vector self.dmg = dmg image = pygame.image.load("graphics/shots/" + str(nImagen) + ".png") self.imageSize = [ image.get_size()[0] * sizeMultiplier, image.get_size()[1] * sizeMultiplier ] self.pos = [initialPos[0], initialPos[1]] self.rect = pygame.Rect(initialPos[0], initialPos[1], self.imageSize[0], self.imageSize[1]) self.eliminada = False
def ShowImageWithFitScale(screen, imageFilePath, angle=0): if (imageFilePath == None or imageFilePath == ""): return image = pygame.image.load(imageFilePath) scrWidth, scrHeight = screen.get_size() image = pygame.transform.rotate(image, angle) imgWidth, imgHeight = image.get_size() ratio = 1.0 * imgWidth / imgHeight if imgWidth > imgHeight: if imgWidth > scrWidth: imgWidth = scrWidth imgHeight = imgWidth / ratio if imgHeight > scrHeight: imgHeight = scrHeight imgWidth = imgHeight * ratio else: if imgHeight > scrHeight: imgHeight = scrHeight imgWidth = imgHeight * ratio if (imgWidth > scrWidth): imgWidth = scrWidth imgHeight = imgWidth / ratio image = pygame.transform.scale(image, (int(imgWidth), int(imgHeight))) posX = (scrWidth - imgWidth) / 2.0 posY = (scrHeight - imgHeight) / 2.0 screen.fill((0, 0, 0)) screen.blit(image, (posX, posY)) pygame.display.flip()
def __init__(self, nPlayer, n=1): self.n = n self.nPlayer = nPlayer self.lastPos = [1,1] self.Input = [0, 0] self.direction = [0, 0] self.points = 0 self.moving = False image = pygame.image.load("graphics/pjs/" + str(self.n) + "/StillRight1.png") self.imageSize = image.get_size() self.rect = pygame.Rect(50,50,self.imageSize[0]*0.7, self.imageSize[1]*0.7) #la colision es un poco mas pequeña que la imagen real para hacerla mas ajustada #self.lastUpdate = 16 #valor teorico que tendria que tener a 62.5fps: 1000 ms / 62.5 = 16 ms. este valor se cambia cada vez que el jugador se mueve para ajustarlo self.initialBulletPos = ((self.rect.centerx + ((self.rect.width-20)*self.direction[0])), (self.rect.centery+20)) self.couldBeInsideWall = True self.movementAvailable = True self.speed = 0.25 #0.0625 0.125 0.25 0.5 1 estaba en 0.25 self.bulletSpeed = 0.75 #ESTABA EN 0.75 self.shootSpeed = 200 #miliseconds estaba en 200 self.bulletSizeMultiplier = 1 self.maxHp = 100 self.hp = self.maxHp self.timeToRevive = 5000 #miliseconds self.alive = True self.bulletDmg = 5 self.lastTimeShot = 0 self.chargingAttack = False self.chargingAttackDmgMultiplier = 1 self.chargingAttackSizeMultiplier = 1 self.chargingAttackSpeedMultiplier = 1 self.balas = [] self.playerVarsBeenChanging = [] #la estructura sera [[VAR, CHANGEBY], [VAR, CHANGEBY], ...] self.cd2 = 26 #la ulti tiene 26 segundos de cd self.actualCd2 = 0 #al principio la ulti no esta en cd
def padded_image(image, margin): w, h = image.get_size() w1 = w + margin * 2 h1 = h + margin * 2 image1 = pg.Surface((w1, h1)).convert_alpha() image1.fill((255, 255, 255, 0)) image1.blit(image, (margin, margin)) return image1
def set_image(self, image): """Change the image without moving the center""" self.image = image oldrect = self.rect x, y = oldrect.center w, h = image.get_size() self.rect = Rect((x-w/2, y-h/2, w, h)) # Return the affected area return oldrect.union(self.rect)
def change_color(image, from_color, to_color): """Change the color of an image object. Return the recolored image""" width, height = image.get_size() for x in range(width): for y in range(height): if image.get_at((x, y)) == from_color: image.set_at((x, y), to_color) return image
def __init__(self,image): self.scr = display.get_surface() w,h = image.get_size() w //= 3 self.images = [image.subsurface(x,0,w,h).copy() for x in range(0,w*3,w)] Rect.__init__(self,0,0,w,h) self.ACTIV = True self.status = False self.over = False
def _shoot(self, filename): self.stop() self.start(image_size=self.target_resolution) image = self.camera.get_image() pygame.image.save(image, filename) self.image_resolution = image.get_size() self.stop() self.start(image_size=self.preview_resolution)
def main(self): displayservice = Display3D.getDisplayService() self.link((self,"display_signal"), displayservice) self.send(self.disprequest, "display_signal"); # load texture if self.tex is not None: # load image image = pygame.image.load(self.tex) # create power of 2 dimensioned surface pow2size = (int(2**(ceil(log(image.get_width(), 2)))), int(2**(ceil(log(image.get_height(), 2))))) if pow2size != image.get_size(): textureSurface = pygame.Surface(pow2size, pygame.SRCALPHA, 32) # determine texture coordinates self.tex_w = float(image.get_width())/pow2size[0] self.tex_h = float(image.get_height())/pow2size[1] # copy image data to pow2surface textureSurface.blit(image, (0,0)) else: textureSurface = image self.tex_w = 1.0 self.tex_h = 1.0 # set plane size self.size = Vector(float(image.get_width())/100.0, float(image.get_height())/100.0, 0) # prepare vertices for intersection test x = float(self.size.x/2) y = float(self.size.y/2) self.vertices = [ Vector(-x, y, 0.0), Vector(x, y, 0.0), Vector(x, -y, 0.0), Vector(-x, -y, 0.0) ] # read pixel data textureData = pygame.image.tostring(textureSurface, "RGBX", 1) # gen tex name self.texID = glGenTextures(1) # create texture glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, self.texID) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, textureSurface.get_width(), textureSurface.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData ); glDisable(GL_TEXTURE_2D) while 1: # for _ in self.waitBox("callback"): yield 1 # self.display = self.recv("callback") # There is no need for a callback yet yield 1 self.handleEvents() self.handleMovementCommands() self.applyTransforms() self.draw()
def __init__(self, position, image=None): self.dead = 0 # self.group = group self.image = image if len(position) == 4: self.rect = position else: if image is None: w, h = 0, 0 else: w, h = image.get_size() x, y = position self.rect = Rect(x-w/2, y-h/2, w, h)
def __init__(self, image): self.scr = display.get_surface() w, h = image.get_size() w //= 3 self.images = [ image.subsurface(x, 0, w, h).copy() for x in range(0, w * 3, w) ] Rect.__init__(self, 0, 0, w, h) self.ACTIV = True self.status = False self.over = False
def scale_image(image, x_scale, y_scale=None): """ Return a version of the image that's been scaled by a factor x_scale in the x direction and y_scale in the y direction. If y_scale is not given, scale uniformly. """ if y_scale is None: y_scale = x_scale (x_size, y_size) = image.get_size() x_size = x_size * x_scale y_size = y_size * y_scale return pygame.transform.scale (image, (x_size, y_size))
def draw_image(x, y, image, scale=1): global surfarr, changed changed = True del surfarr x = int(x) y = int(y) if scale == 1: background.blit(image, (x, y)) else: sz = image.get_size() sz = [int(sz[0] * scale), int(sz[1] * scale)] newImage = pygame.transform.scale(image, sz) background.blit(newImage, (x, y)) surfarr = pygame.PixelArray(background)
def draw_image(x, y, image, scale = 1): global surfarr, changed changed = True del surfarr x=int(x) y=int(y) if scale == 1: background.blit(image, (x,y)) else: sz = image.get_size() sz = [int(sz[0] * scale), int(sz[1] * scale)] newImage = pygame.transform.scale(image, sz) background.blit(newImage, (x,y)) surfarr = pygame.PixelArray(background)
def __init__(self, m_f, m_f_d, image): """ Custom init /!\ TODO : Some check """ image = pygame.image.load(image).convert_alpha() super(BaseAnimation,self).__init__(image.get_size(),pygame.SRCALPHA, 32) self.convert_alpha() self.blit( image, image.get_rect() ) self.max_frame = m_f self.max_frame_delay = m_f_d self.frame = 0 self.frame_delay = 0
def main(): # on charge nos images image = pygame.image.load("image.jpg") invaders = pygame.image.load("invaders.png") # on crée les couches screen, background = create_layers(image.get_size()) # on crée le canal de transparence de l'image png invaders.convert_alpha() running = True display(screen, background, image=image, invaders=invaders) while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_q: running = False display(screen, background, image=image, invaders=invaders)
def run(image, config): rects = config['rects'] centers = config['centers'] names = list(rects.keys()) pg.display.init() screen = pg.display.set_mode(image.get_size(), 0, 32) image = image.convert_alpha() R = 5 circle = pg.Surface((R * 2 + 1, R * 2 + 1)).convert_alpha() circle.fill((255, 255, 255, 0)) pg.draw.circle(circle, (0, 255, 0, 255), (R + 1, R + 1), R, 1) circle.set_at((R + 1, R + 1), (0, 255, 0, 255)) tm = pg.time.Clock() quit = False while not quit: for e in pg.event.get(): if e.type == pg.QUIT: quit = True elif e.type == pg.MOUSEBUTTONDOWN: x1, y1 = e.pos for name in names: x, y, w, h = rects[name] if x <= x1 < x + w and y <= y1 < y + h: centers[name] = x1 - x, y1 - y print(name, centers[name]) break screen.fill((255, 255, 255, 255)) screen.blit(image, (0, 0)) for name in names: x, y, w, h = rects[name] cx, cy = centers[name] screen.blit(circle, (x + cx - R - 1, y + cy - R - 1)) pg.display.update() tm.tick(30) return config
def pack_dir(dirPath, output, margin=10): import os import json pg.display.init() pg.display.set_mode((1, 1), 0, 32) images = [] names = [] for name in os.listdir(dirPath): if name.endswith('.png'): path = os.path.join(dirPath, name) images.append(padded_image(pygame.image.load(path).convert_alpha(), margin)) names.append(name[:-4]) imagePack = ImagePack(images) resultImage = imagePack.image pygame.image.save(resultImage, output + '.png') rects = {} centers = {} try: centers.update(json.load(open(output + '.json'))['centers']) except FileNotFoundError: pass except IsADirectoryError: pass config = {'rects': rects, 'centers': centers} for name, image, pos in zip(names, images, imagePack.positions): x, y = pos w, h = image.get_size() # x -= margin # y -= margin # w -= 2 * margin # h -= 2 * margin rects[name] = (x, y, w, h) if name not in centers: centers[name] = (w//2, h//2) json.dump(config, open(output + '.json', 'w')) print('saved to {}'.format(output + '.png'))
def scale_image(image, x_scale, y_scale=None): if y_scale is None: y_scale = x_scale (x_size, y_size) = image.get_size() x_size = x_size * x_scale y_size = y_size * y_scale return pygame.transform.scale(image, (x_size, y_size))
def main(self): displayservice = Display3D.getDisplayService() self.link((self, "display_signal"), displayservice) self.send(self.disprequest, "display_signal") # load texture if self.tex is not None: # load image image = pygame.image.load(self.tex) # create power of 2 dimensioned surface pow2size = (int(2**(ceil(log(image.get_width(), 2)))), int(2**(ceil(log(image.get_height(), 2))))) if pow2size != image.get_size(): textureSurface = pygame.Surface(pow2size, pygame.SRCALPHA, 32) # determine texture coordinates self.tex_w = float(image.get_width()) / pow2size[0] self.tex_h = float(image.get_height()) / pow2size[1] # copy image data to pow2surface textureSurface.blit(image, (0, 0)) else: textureSurface = image self.tex_w = 1.0 self.tex_h = 1.0 # set plane size self.size = Vector( float(image.get_width()) / 100.0, float(image.get_height()) / 100.0, 0) # prepare vertices for intersection test x = float(self.size.x / 2) y = float(self.size.y / 2) self.vertices = [ Vector(-x, y, 0.0), Vector(x, y, 0.0), Vector(x, -y, 0.0), Vector(-x, -y, 0.0) ] # read pixel data textureData = pygame.image.tostring(textureSurface, "RGBX", 1) # gen tex name self.texID = glGenTextures(1) # create texture glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, self.texID) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureSurface.get_width(), textureSurface.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData) glDisable(GL_TEXTURE_2D) while 1: # for _ in self.waitBox("callback"): yield 1 # self.display = self.recv("callback") # There is no need for a callback yet yield 1 self.handleEvents() self.handleMovementCommands() self.applyTransforms() self.draw()