def __init__(self, width, height, surface=None): self.width = width self.height = height if surface: self.pixels = surfarray.pixels2d(surface) else: self.pixels = surfarray.pixels2d(Surface((width, height)))
def do_display(cls, pixels, view, chart): center = view.center if isinstance(pixels, pygame.Surface): pixels = surfarray.pixels2d(pixels) x = y = 0 next_x = next_y = 0 origin = [center[i] - pixels.shape[i] // 2 for i in (0,1)] colors = view(chart) try: while x < pixels.shape[0]: y = next_y = 0 while y < pixels.shape[1]: view = chart.map_slice((origin[0] + x, origin[1] + y), colors) next_x = min(x + view.shape[0], pixels.shape[0]) next_y = min(y + view.shape[1], pixels.shape[1]) pixels[x:next_x, y:next_y] = view[:next_x-x,:next_y-y] if next_y <= y: break y = next_y if next_x <= x: break x = next_x except IndexError as ie: #Didn't have a palette entry for some value, figure it out. states = set() for x in range(chart.shape[0]): for y in range(chart.shape[1]): states.add(chart[x, y]) print states, palette raise ie pygame.display.flip()
def __init__(self,computer): self._screen = {} self._started = False self._screenHeight = 1000 self._screenWidth = 1000 self._origin = (self._screenHeight // 2, self._screenWidth // 2) self._robot = self._origin self._started = False self._computer = computer self._surface = None self._pixels = None self._direction = 0 for k in Game.table.keys(): a = np.zeros((Game.blockSize,Game.blockSize),np.uint32) item = Game.table[k] for y in range(0,Game.inputBlockSize): for x in range(0,Game.inputBlockSize): if item[y][x]== 1: a[ y*Game.scalingFactor:y*Game.scalingFactor+Game.scalingFactor, x*Game.scalingFactor:x*Game.scalingFactor+Game.scalingFactor ] = 0XFFFFFF Game.table[BT(k)] = a if (BT.GreenEmpty not in Game.table.keys()): block = Game.table[BT.Empty].copy() block.fill(0x00FF00) Game.table[BT.GreenEmpty] = block pygame.init() self._surface = pygame.display.set_mode((self._screenWidth, self._screenHeight)) self._surface.fill((128,128,128)) #black self._pixels = surfarray.pixels2d(self._surface) self.UpdateDisplay(-1)
def __init__(self, display): self.time = 0 self.running = True self.pixels = surfarray.pixels2d(display) self.clock = pygame.time.Clock() self.game = Game() self.screen = Screen(s.WIDTH, s.HEIGHT)
def render(self): pixel_array = surfarray.pixels2d(self.surface) changed_pixels = np.where(self.array != self.drawn_state) # A copy of the game map where each cell is True if it is different than the drawn state, and false otherwise # for y, x in self.changed_pixels: # for i in range(changed_pixels[0].shape[0]): # y = changed_pixels[0][i] # x = changed_pixels[1][i] # pixel_array[x*self.zoom_factor:( # x+1)*self.zoom_factor, y*self.zoom_factor:(y+1)*self.zoom_factor] = colors[self.array[y,x]] process_render_internal(changed_pixels, pixel_array, self.array, self.zoom_factor) self.drawn_state = self.array.copy()
def __init__(self, computer, minBlockTuple, maxBlockTuple, startData): self._positions = {} self._ball = None self._paddle = None self._started = False self._computerOutput = [] self._scores = [] self._computer = computer self._joyStick = 0 self._minBlockX = minBlockTuple[0] self._minBlockY = minBlockTuple[1] self._maxBlockX = maxBlockTuple[0] self._maxBlockY = maxBlockTuple[1] self._screenHeight = (self._maxBlockY + 1) * Game.blockSize self._screenWidth = (self._maxBlockX + 1) * Game.blockSize self._font = None self._surface = None self._pixels = None for k in Game.table.keys(): a = np.zeros((Game.blockSize, Game.blockSize), np.uint32) item = Game.table[k] for y in range(0, Game.inputBlockSize): for x in range(0, Game.inputBlockSize): if item[y][x] == 1: a[y * Game.scalingFactor:y * Game.scalingFactor + Game.scalingFactor, x * Game.scalingFactor:x * Game.scalingFactor + Game.scalingFactor] = 0XFFFFFF Game.table[k] = a pygame.init() self._surface = pygame.display.set_mode( (self._screenWidth, self._screenHeight)) self._surface.fill((0, 0, 0)) #black self._pixels = surfarray.pixels2d(self._surface) pygame.display.set_caption('Score: 0') self.UpdateDisplay(startData)
def stamp(self, sealevel, templevel, colormap, surface): """Writes the planet sprite onto the given surface. Sealevel and templevel have dynamic range from -1.0 to 1.0""" sealevel = self.dynamic_range(sealevel) templevel = self.dynamic_range(-templevel) # invert templevel data = surfarray.pixels2d(surface) np.copyto(data, self.alpha) # A clean slate # Temperature np.subtract(self.temperature, templevel, out=self.altered_temp) np.clip(self.altered_temp, 0, ColorMap.SHEET_SIZE - 1, out=self.altered_temp) np.subtract(self.elevation, sealevel, out=self.altered_elevation) np.clip(self.altered_elevation, 0, ColorMap.SHEET_SIZE - 1, out=self.altered_elevation) self.scratch[:] = colormap.colors[self.altered_temp, self.altered_elevation] np.bitwise_or(self.scratch.astype(dtype=np.uint32, casting="unsafe", copy=False), data, out=data)
else: slope = -1 * float( decimal.Decimal(random.randrange(25, 200)) / 100) slope = -slope intercept = current_y - slope * ballX if slope * ballX + intercept <= 0: intercept += 2 * slope * ballX slope = -slope if slope * ballX + intercept >= 500: intercept += 2 * slope * ballX slope = -slope if d == -1: ballX -= 5 if d == 1: ballX += 5 X1 = surfarray.pixels2d(pygame.display.get_surface()) # iska dekh le.. y ka and final trainingset ka jo bhi h clock.tick(100) DS.fill(BLACK) count += 1 if (count > 500): with open('trainX.pkl', 'wb') as f: pickle.dump(trainX, f) with open('trainY.pkl', 'wb') as f: pickle.dump(trainY, f) pygame.quit() sys.exit() pygame.draw.rect(DS, WHITE, (0, r1, 15, 100)) # pygame.draw.rect(DS, RED, (485, r2, 15, 100)) pygame.draw.circle(DS, WHITE, (ballX, int(slope * ballX + intercept)), 10)
def __init__(self, computer): self._screen = {} self._started = False self._screenHeight = 1275 self._screenWidth = 1275 self._origin = (self._screenHeight // 2, self._screenWidth // 2) self._robot = self._origin self._started = False self._computer = computer self._surface = None self._pixels = None self._direction = 0 self._textScreen = TextScreen() self._textScreenDraw = 0 for k in Game.table.keys(): if (k in [BT.Up, BT.Down, BT.Left, BT.Right, BT.Ball]): r, g, b, a = pygame.colordict.THECOLORS.get('yellow2') color = r << 16 | g << 8 | b elif (k == BT.BadRobot): r, g, b, a = pygame.colordict.THECOLORS.get('indianred3') color = r << 16 | g << 8 | b elif (k == BT.Junction): r, g, b, a = pygame.colordict.THECOLORS.get('cornflowerblue') color = r << 16 | g << 8 | b else: color = 0xFFFFFF a = np.zeros((Game.blockSize, Game.blockSize), np.uint32) item = Game.table[k] for y in range(0, Game.inputBlockSize): for x in range(0, Game.inputBlockSize): if item[y][x] == 1: a[y * Game.scalingFactor:y * Game.scalingFactor + Game.scalingFactor, x * Game.scalingFactor:x * Game.scalingFactor + Game.scalingFactor] = color if (k == BT.Up): #Fill in sides of arrow, so it looks smooth a[9, 1] = a[9, 2] = a[9, 3] = a[9, 4] = a[8, 2] = a[8, 3] = a[ 8, 4] = a[7, 3] = a[7, 4] = a[6, 4] = color a[15, 1] = a[15, 2] = a[15, 3] = a[15, 4] = a[16, 2] = a[ 16, 3] = a[16, 4] = a[17, 3] = a[17, 4] = a[18, 4] = color if (k == BT.Down): #Fill in sides of arrow, so it looks smooth a[9, 23] = a[9, 22] = a[9, 21] = a[9, 20] = a[8, 22] = a[ 8, 21] = a[8, 20] = a[7, 21] = a[7, 20] = a[6, 20] = color a[15, 23] = a[15, 22] = a[15, 21] = a[15, 20] = a[16, 22] = a[ 16, 21] = a[16, 20] = a[17, 21] = a[17, 20] = a[18, 20] = color if (k == BT.Left): #Fill in sides of arrow, so it looks smooth a[4, 6] = a[4, 7] = a[4, 8] = a[4, 9] = a[3, 7] = a[3, 8] = a[ 3, 9] = a[2, 8] = a[2, 9] = a[1, 9] = color a[4, 18] = a[4, 17] = a[4, 16] = a[4, 15] = a[3, 17] = a[ 3, 16] = a[3, 15] = a[2, 16] = a[2, 15] = a[1, 15] = color if (k == BT.Right): #Fill in sides of arrow, so it looks smooth a[20, 6] = a[20, 7] = a[20, 8] = a[20, 9] = a[21, 7] = a[ 21, 8] = a[21, 9] = a[22, 8] = a[22, 9] = a[23, 9] = color a[20, 18] = a[20, 17] = a[20, 16] = a[20, 15] = a[21, 17] = a[ 21, 16] = a[21, 15] = a[22, 16] = a[22, 15] = a[23, 15] = color Game.table[BT(k)] = a if (BT.GreenEmpty not in Game.table.keys()): block = Game.table[BT.Empty].copy() block.fill(0x00FF00) Game.table[BT.GreenEmpty] = block pygame.init() self._surface = pygame.Surface((self._screenWidth, self._screenHeight)) #we'll us a window that is scaled smaller than the actual game surface, to get everything on a 1080 height screen. self._window = pygame.display.set_mode((1000, 1000)) self._surface.fill((0, 0, 0)) self._pixels = surfarray.pixels2d(self._surface)
def __init__(self): self.screen = pygame.display.set_mode((800,800), pygame.DOUBLEBUF | pygame.HWSURFACE) self.size = self.screen.get_size() self.pixels = surfarray.pixels2d(self.screen) from gamelib.popup_menu import NonBlockingPopupMenu self.menu = NonBlockingPopupMenu(MENU)