def display(self, window): window = pygame.surface([window.get_size()[i] / self.size[i] for i in range(2)]) window.fill((0, 0, 0)) for x in range(self.size[0]): for y in range(self.size[1]): if self[x, y]: pygame.draw.rect(window, (255, 255, 255), pygame.Rect((x * window[1], y * window[1]), window))
def __init__(self, image, width, height): super().__init__() self.image = pygame.surface([width, height]) self.rect = self.image.get_rect() self.rect.x = width self.rect.y = height
def __init__(self, alto, largo): super().__init__() self.image = pygame.surface((largo, alto)) self.image.fill((0,255,0)) self.rect = self.image.get_rect()
def fade(width, height): fade = pygame.surface((width, height)) fade.fill((0, 0, 0)) for alpha in range(0, 300): fade.set_alpha(alpha) redrawwindow() win.bilt(fade, (0, 0)) pygame.display.update() pygame.time.delay(5)
def __init__(self, x, y, vx, vy, size = 40, color = C_RED): self.x=x self.y=y self.vx=vx self.vy=vy self.size = size self.color = color self.image = pygame.surface((size,size)) draw_tie(self.image,color,size) self.rect=pygame.Rect(x,y,size,size)
def svg_image_load(src, pth=None): svgsrf = pygame.surface(64, 64) ctx = cairo.Context(svgsrf) fn = pth / src svgsrc = open(fn, 'r') svg = svgsrc.read() svgsrc.close() handle = rsvg.Handle(svg) handle.render(ctx) return ctx
def main(): background = pygame.surface((640,480)) background.fill((0,0,0)) for i in range (1,320,3): pygame.draw.circle(background,(0xFF,0x00,0x00),(i,i),i,1) pygame.draw.circle(background,(0x00,0x00,0xFF),(640-,i),i,1) pygame.draw.circle(background,(0x00,0x00,0xFF),(i,480-i),i,1) pygame.draw.circle(background,(0xFF,0x00,0x00),(640-1,480-i),i,1) pygame.draw.circle(background,(0xFF,0xFF,0xFF),(320,240),i,1) pygame.image.save(background,"circles.bmp") print "look in current directory"
def load(self,size): self.surfaces = [] if not self.filename: surface = pygame.surface() self.surfaces.append(surface) file_surface = pygame.image.load(self.filename) if not self.orig_rect: self.orig_rect = file_surface.get_rect() # w,h for tile image including possible overlay over tile to North w = size.x h = size.y * self.orig_rect.h / self.orig_rect.w if isinstance(self.stride,str) and self.stride[-1]=='x': stride = Pos(int(self.stride[:-1]) * self.orig_rect.w,0) if isinstance(self.stride,str) and self.stride[-1]=='y': stride = Pos(0,int(self.stride[:-1]) * self.orig_rect.h) elif isinstance(self.stride,Number): stride = Pos(self.stride,0) else: stride = stride assert isinstance(stride[1],Number) for rep in range(self.reps): surface = file_surface if self.first_subrect is not None: surface = surface.subsurface(self.first_subrect.move(stride*rep)) surface = pygame.transform.scale(surface,(w,h)) if self.colorkey is not None: colorkey = self.colorkey if colorkey == 'topleft': colorkey = surface.get_at( (0,0) ) elif colorkey == 'topright': colorkey = surface.get_at( (w-1,0) ) surface.set_colorkey(colorkey, RLEACCEL) surface = surface.convert() self.surfaces.append(surface) self.cur_dst_size = size self.transparent_surfaces = None
def cardsinHand(self): self.card1 = pygame.surface((120, 168)) self.gameDisplay.blit(self.card1, (400, 600))
def __init__(self): super().__init__() self.image = pygame.surface((40,40)) self.rect = self.image.get_rect()
def __init__(self, x, y): super().__init__() self.image = pygame.surface([2, 2]) self.imgage.fill(RED)
from neat import config import pygame import neat import numpy as np import time import os import random from .car import Car from .road import Road from .world import World, world from .DrawNeuralNetwork import NeuralNetwork from .config_variables import * pygame.font.init() background = pygame.surface(WINDOW_WIDTH, WINDOW_HEIGHT) background.fill(BLUE) def draw_Window(cars,road, world,GENENRATION): road.draw(world) for car in cars: car.draw(world) Scoretext = STAT_FONT.render("Best Score: " + str(int( world.getscore())), 1, BLACK ) world.window.blit(Scoretext, (world.win_width-Scoretext.get_width() - 10, 10)) Generationtext = STAT_FONT.render("Generation: " + str(GENENRATION), 1, BLACK) world.win.blit(Generationtext,(world.win_width-Generationtext.get_width()-10,50)) world.BestNeuralNetwork.draw(world) pygame.display.update()
import pygame from pygame.locals import * UP = 0 RIGHT = 1 DOWN = 2 LEFT = 3 pygame.init() screen = pygame.display.set_mode((600, 600)) pygame.display.set_caption('Snake') snake = [(200, 200, 210, 200, 220, 200)] snake_skin = pygame.surface((10,10)) snake_skin.fill(0,100,0) my_direction = LEFT while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() screen.fill((0,0,0)) for pos in snake: screen.blit(snake_skin,pos) pygame.display.update()
# pygame.transform ----》缩放和移动图像 # pygame.locals ---->> pygame中各种常量,包括事件类型、按键和视频模式等的名字 """ 检测模块是否正常 """ # 由于硬件和游戏的兼容新或是请求的驱动没有安装,有些模块可能在某些平台无法使用,可用None来测试 if pygame.font is None: print('The font module is not available!') # 若没有,则退出应用环境 pygame.quit() """ surface """ pygame.surface((width,height),flag=0,depth=0,masks=none) # 返回一个新surface对象。是一个有确定大小尺寸的空图像,用来进行图像绘制与移动 """ display """ # flip/update更新显示 # 一般来说,修改当前屏幕额时候要经过两步,首先需要对get_surface函数返回的surface对象进行修改,然后调用pygame.display.flip()更新显示以反映所做的修改。只更新屏幕一部分的时候调用update()函数,而不是flip()函数 # set_mode(resolution,flags,depth) # 建立游戏窗口,返回surface对象 #第一个参数是元组,指定窗口尺寸,第三参数为色深,指定窗口的色彩位数,第二参数是标志位,含义如下: # FULLSCREEN --->> 创建一个全屏窗口 # DOUBLEBUY --->> 创建一个'双缓冲'窗口,建议在HWSURFACE或者OPENGL时使用 # HWSURFACE --->> 创建一个硬件加速的窗口,必须和FULLSCREEN同时使用 # OPENGL --->> 创建一个OPENGL渲染的窗口
def get_image(sheet, x, y, width, height, colorkey, scale): image = pygame.surface((width,height)) image.blit(sheet, (0,0), (x, y, width, height)) image.set_corlorkey(colorkey) image = pygame.transform.scale(image, (int(width*scale),int(height*scale))) return image
def __init__(self, parentSurface, loc, size): self.parentSurface = parentSurface self.surface = pygame.surface(size, pygame.SRCALPHA)
# pygame.surfarray ----》管理点阵图像数据 # pygame.time ----》管理时间和帧信息 # pygame.transform ----》缩放和移动图像 # pygame.locals ---->> pygame中各种常量,包括事件类型、按键和视频模式等的名字 """ 检测模块是否正常 """ # 由于硬件和游戏的兼容新或是请求的驱动没有安装,有些模块可能在某些平台无法使用,可用None来测试 if pygame.font is None: print('The font module is not available!') # 若没有,则退出应用环境 pygame.quit() """ surface """ pygame.surface((width, height), flag=0, depth=0, masks=none) # 返回一个新surface对象。是一个有确定大小尺寸的空图像,用来进行图像绘制与移动 """ display """ # flip/update更新显示 # 一般来说,修改当前屏幕额时候要经过两步,首先需要对get_surface函数返回的surface对象进行修改,然后调用pygame.display.flip()更新显示以反映所做的修改。只更新屏幕一部分的时候调用update()函数,而不是flip()函数 # set_mode(resolution,flags,depth) # 建立游戏窗口,返回surface对象 #第一个参数是元组,指定窗口尺寸,第三参数为色深,指定窗口的色彩位数,第二参数是标志位,含义如下: # FULLSCREEN --->> 创建一个全屏窗口 # DOUBLEBUY --->> 创建一个'双缓冲'窗口,建议在HWSURFACE或者OPENGL时使用 # HWSURFACE --->> 创建一个硬件加速的窗口,必须和FULLSCREEN同时使用 # OPENGL --->> 创建一个OPENGL渲染的窗口 # RESIZABLE --->> 创建一个可以改变大小的窗口
def make_hole_alpha(self): hole = pg.surface(self.screen_rect.size).convert_alpha() hole.fill((255,255,200)) pg.draw.ellipse(hole, (0,0,0,0), self.ellipse_rect) return hole
def draw(self, screen): screen.blit( pygame.surface((800,600)), self.rect, self.rect )
-*- coding:utf-8 -*- import pygame pygame.init() screen = pygame.display.set_mode((640,480)) all_colors = pygame.surface((4096,4096),depth = 24) for r in xrange(256): print r+1,"out of 256" x = (r&15)*256 y = (r>>4)*256 for g in xrange(256): for b in xrange(256): all_colors.set_at((x+g,y+b),(r,g,b)) pygame.image.save(all_colors,"")
def add_to_spiral(self, word: str) -> None: # Render the raw text surface, srect = self.words_font.render(word, fgcolor=self.words_color) raw_text_height = srect.height + 2 # Determine the orientation orientation = ((len(self._spiral)) % 4) * 90 # Angles are the wrong way :'( o = orientation if orientation == 90: o = 270 elif orientation == 270: o = 90 surface = pygame.transform.rotate(surface, o) # Ensure the spiral gets larger req_dim = 0 if len(self._spiral) >= 2: if orientation == 0 or orientation == 180: # Need to be width of last + width of 2nd last + leeway required_size = self._spiral[-1].get_rect( ).width + self._spiral_smallgap + self._spiral[-2].get_rect( ).width + self._spiral_smallgap + self._spiral_leeway req_dim = 0 elif orientation == 90 or orientation == 270: # Need to be height of last + height of 2nd last + leeway required_size = self._spiral[-1].get_rect( ).height + self._spiral_smallgap + self._spiral[-2].get_rect( ).height + self._spiral_smallgap + self._spiral_leeway req_dim = 1 # Scale the surface to ensure the right size text aspect_ratio = surface.get_rect().width / surface.get_rect().height if req_dim == 0: # width surface = pygame.transform.smoothscale( surface, (required_size, int(required_size / aspect_ratio))) else: #height surface = pygame.transform.smoothscale( surface, (int(required_size * aspect_ratio), required_size)) # Find spiral position based on last text position cur_x, cur_y = self._spiral_curpos # Calculate the current position if orientation == 0: # Last one was orientation 270 # So just move up by the height of this text cur_y -= surface.get_rect().height - self._spiral_smallgap elif orientation == 90: # Last one was orientation 0 # So just move right by the width of the last text cur_x += self._spiral[-1].get_rect().width + self._spiral_smallgap elif orientation == 180: # Last one was orientation 90 # So move left by the width of the new text minus the width of the last text cur_x -= ( surface.get_rect().width - self._spiral[-1].get_rect().width) - self._spiral_smallgap # So move down by the height of the last text cur_y += self._spiral[-1].get_rect().height + self._spiral_smallgap pass elif orientation == 270: # Last one was orientation 180 # So move left by the width of this text cur_x -= surface.get_rect().width - self._spiral_smallgap # So move up by the height of this text minus the height of the last text cur_y -= ( surface.get_rect().height - self._spiral[-1].get_rect().height) - self._spiral_smallgap # Special case to start with (to ensure centering) if len(self._spiral) == 0: cur_x -= surface.get_rect().width / 2 cur_y -= surface.get_rect().height - 40 # Add the drawn surface to the spiral list self._spiral_curpos = cur_x, cur_y self._spiral.append(surface) # Calculate the new size of the whole spiral (total_x, total_y, total_width, total_height) = self._spiral_rect if orientation == 0: total_x = cur_x total_y = cur_y total_width = surface.get_rect().width total_height = max([total_height, surface.get_rect().height]) elif orientation == 90: total_width += surface.get_rect().width total_height = surface.get_rect().height elif orientation == 180: total_x = cur_x total_width = surface.get_rect().width total_height += surface.get_rect().height elif orientation == 270: total_x = cur_x total_y = cur_y total_width += surface.get_rect().width total_height = surface.get_rect().height self._spiral_rect = (total_x, total_y, total_width, total_height) print("CURPOS: " + str(self._spiral_curpos)) # Resize the spiral surface if needed print("RECT: " + str(self._spiral_rect)) if total_x < 0 or total_y < 0 or total_x + total_width > self._spiral_surface.get_rect( ).width or total_y + total_height > self._spiral_surface.get_rect( ).height: new_surface = pygame.surface((self._spiral_surface.get_width(), self._spiral_surface.get_height())) new_surface.fill((200, 200, 200)) scaled_down = pygame.transform.smoothscale(self._spiral_surface, (300, 300)) self._spiral_surface = new_surface() # if total_x <= self._spiral_surface.get_rect().width / 8 or total_y <= self._spiral_surface.get_rect().height / 8 or total_x + total_width >= 7 * (self._spiral_surface.get_rect().width / 8) or total_y + total_height >= 7 * (self._spiral_surface.get_rect().height / 8): # new_surface = pygame.Surface((total_width * 2, total_height * 2)) # new_surface.fill((0, 0, 0)) # # r = new_surface.blit(self._spiral_surface, (new_surface.get_rect().width/2 - total_width/2, new_surface.get_rect().height/2 - total_height/2)) # # print("AFFECTED: " + str(r)) # self._spiral_surface = new_surface # print(f"NEW SURFACE: ({self._spiral_surface.get_rect().width}, {self._spiral_surface.get_rect().height})") self._spiral_surface.blit(surface, self._spiral_curpos)
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> import pygame >>> window = pygame.display.set_mode((400, 400)) >>> pygame.display.set_caption("Hello pygame") >>> >>> screen = pygame.surface((400, 400)) Traceback (most recent call last): File "<pyshell#4>", line 1, in <module> screen = pygame.surface((400, 400)) TypeError: 'module' object is not callable >>> screen = pygame.Surface((400, 400)) >>> >>> done = True >>> while done: for e in pygame.event.get(): if e.type == pygame.QUIT: done = False window.blit(screen, (0, 0)) pygame.display.flip()