Python pygame.Rect() Examples
The following are 30
code examples of pygame.Rect().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
pygame
, or try the search function
.

Example #1
Source File: polygon.py From angry-birds-python with MIT License | 9 votes |
def __init__(self, pos, length, height, space, mass=5.0): moment = 1000 body = pm.Body(mass, moment) body.position = Vec2d(pos) shape = pm.Poly.create_box(body, (length, height)) shape.color = (0, 0, 255) shape.friction = 0.5 shape.collision_type = 2 space.add(body, shape) self.body = body self.shape = shape wood = pygame.image.load("../resources/images/wood.png").convert_alpha() wood2 = pygame.image.load("../resources/images/wood2.png").convert_alpha() rect = pygame.Rect(251, 357, 86, 22) self.beam_image = wood.subsurface(rect).copy() rect = pygame.Rect(16, 252, 22, 84) self.column_image = wood2.subsurface(rect).copy()
Example #2
Source File: usb_drive_copymode.py From pi_video_looper with GNU General Public License v2.0 | 7 votes |
def _pygame_init(self, config): self._bgcolor = (52,52,52) self._fgcolor = (149,193,26) self._bordercolor = (255,255,255) self._fontcolor = (255,255,255) self._font = pygame.font.Font(None, 40) #positions and sizes: self.screenwidth = pygame.display.Info().current_w self.screenheight = pygame.display.Info().current_h self.pwidth=0.8*self.screenwidth self.pheight=0.05*self.screenheight self.borderthickness = 2 #create rects: self.borderrect = pygame.Rect((self.screenwidth / 2) - (self.pwidth / 2), (self.screenheight / 2) - (self.pheight / 2), self.pwidth, self.pheight)
Example #3
Source File: usb_drive_copymode.py From pi_video_looper with GNU General Public License v2.0 | 7 votes |
def draw_copy_progress(self, copied, total): perc = 100 * copied / total assert (isinstance(perc, float)) assert (0. <= perc <= 100.) progressrect = pygame.Rect((self.screenwidth / 2) - (self.pwidth / 2) + self.borderthickness, (self.screenheight / 2) - (self.pheight / 2) + self.borderthickness, (self.pwidth-(2*self.borderthickness))*(perc/100), self.pheight - (2*self.borderthickness)) #border pygame.draw.rect(self._screen, self._bordercolor, self.borderrect, self.borderthickness) #progress pygame.draw.rect(self._screen, self._fgcolor, progressrect) #progress_text self.draw_progress_text(str(int(round(perc)))+"%") pygame.display.update(self.borderrect)
Example #4
Source File: scrview.py From zxbasic with GNU General Public License v3.0 | 6 votes |
def plot_byte(screen, data: List[int], offset: int): """ Draws a pixel at the given X, Y coordinate """ global TABLE byte_ = TABLE[data[offset]] attr = get_attr(data, offset) ink_ = attr & 0x7 paper_ = (attr >> 3) & 0x7 bright = (attr >> 6) & 0x1 paper = tuple(x + bright for x in PALETTE[paper_]) ink = tuple(x + bright for x in PALETTE[ink_]) palette = [paper, ink] x0, y0 = get_xy_coord(offset) for x, bit_ in enumerate(byte_): screen.fill(palette[bit_], pygame.Rect(x0 + x * SCALE, y0, SCALE, SCALE))
Example #5
Source File: battle_city_utils.py From AirGesture with MIT License | 6 votes |
def __init__(self, level): global sprites # to know where to place self.level = level # bonus lives only for a limited period of time self.active = True # blinking state self.visible = True self.rect = pygame.Rect(random.randint(0, 416 - 32), random.randint(0, 416 - 32), 32, 32) self.bonus = random.choice([ self.BONUS_GRENADE, self.BONUS_HELMET, self.BONUS_SHOVEL, self.BONUS_STAR, self.BONUS_TANK, self.BONUS_TIMER ]) self.image = sprites.subsurface(16 * 2 * self.bonus, 32 * 2, 16 * 2, 15 * 2)
Example #6
Source File: snake.py From PyGame-Learning-Environment with MIT License | 6 votes |
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
Example #7
Source File: pyos.py From PythonOS with MIT License | 6 votes |
def getRenderedText(self): fits = False surf = None while not fits: d = GUI.MultiLineText.render_textrect(self.text, self.font.get(self.size), pygame.Rect(self.computedPosition[0], self.computedPosition[1], self.computedWidth, self.height), self.color, (0, 0, 0, 0), self.justification, self.use_freetype) surf = d[0] fits = d[1] != 1 self.textLines = d[2] if not fits: self.height += self.lineHeight self.computedHeight = self.height self.setDimensions() #if self.linkedScroller != None: # self.linkedScroller.refresh(False) return surf
Example #8
Source File: pygame.py From snake-ai-reinforcement with MIT License | 6 votes |
def render_cell(self, x, y): """ Draw the cell specified by the field coordinates. """ cell_coords = pygame.Rect( x * self.CELL_SIZE, y * self.CELL_SIZE, self.CELL_SIZE, self.CELL_SIZE, ) if self.env.field[x, y] == CellType.EMPTY: pygame.draw.rect(self.screen, Colors.SCREEN_BACKGROUND, cell_coords) else: color = Colors.CELL_TYPE[self.env.field[x, y]] pygame.draw.rect(self.screen, color, cell_coords, 1) internal_padding = self.CELL_SIZE // 6 * 2 internal_square_coords = cell_coords.inflate((-internal_padding, -internal_padding)) pygame.draw.rect(self.screen, color, internal_square_coords)
Example #9
Source File: part08.py From pygame_tutorials with MIT License | 6 votes |
def draw(self): show_arrow = True show_dist = False for pos, v in self.field.items(): if v.length_squared() > 0: if show_dist: start_color = (200, 50, 0) end_color = DARKGRAY p_rect = pg.Rect((p.pos.x // TILESIZE) * TILESIZE, (p.pos.y // TILESIZE) * TILESIZE, TILESIZE, TILESIZE) pg.draw.rect(screen, start_color, p_rect) rect = pg.Rect(pos[0] * TILESIZE, pos[1] * TILESIZE, TILESIZE, TILESIZE) r = self.distance[pos] / self.max_distance col = color_gradient(start_color, end_color, r) pg.draw.rect(screen, col, rect) if show_arrow: rot = v.angle_to(vec(1, 0)) img = self.vec_images[rot] rect = img.get_rect() rect.center = (pos[0] * TILESIZE + TILESIZE / 2, pos[1] * TILESIZE + TILESIZE / 2) screen.blit(img, rect) # draw_text(str(self.distance[pos]), 12, WHITE, rect.centerx, rect.centery, align="center")
Example #10
Source File: flappy.py From flappybird-qlearning-bot with MIT License | 5 votes |
def checkCrash(player, upperPipes, lowerPipes): """returns True if player collders with base or pipes.""" pi = player["index"] player["w"] = IMAGES["player"][0].get_width() player["h"] = IMAGES["player"][0].get_height() # if player crashes into ground if (player["y"] + player["h"] >= BASEY - 1) or (player["y"] + player["h"] <= 0): return [True, True] else: playerRect = pygame.Rect(player["x"], player["y"], player["w"], player["h"]) pipeW = IMAGES["pipe"][0].get_width() pipeH = IMAGES["pipe"][0].get_height() for uPipe, lPipe in zip(upperPipes, lowerPipes): # upper and lower pipe rects uPipeRect = pygame.Rect(uPipe["x"], uPipe["y"], pipeW, pipeH) lPipeRect = pygame.Rect(lPipe["x"], lPipe["y"], pipeW, pipeH) # player and upper/lower pipe hitmasks pHitMask = HITMASKS["player"][pi] uHitmask = HITMASKS["pipe"][0] lHitmask = HITMASKS["pipe"][1] # if bird collided with upipe or lpipe uCollide = pixelCollision(playerRect, uPipeRect, pHitMask, uHitmask) lCollide = pixelCollision(playerRect, lPipeRect, pHitMask, lHitmask) if uCollide or lCollide: return [True, False] return [False, False]
Example #11
Source File: learn.py From flappybird-qlearning-bot with MIT License | 5 votes |
def checkCrash(player, upperPipes, lowerPipes): """returns True if player collders with base or pipes.""" pi = player["index"] player["w"] = PLAYER[IM_WIDTH] player["h"] = PLAYER[IM_HEIGTH] # if player crashes into ground if (player["y"] + player["h"] >= BASEY - 1) or (player["y"] + player["h"] <= 0): return [True, True] else: playerRect = pygame.Rect(player["x"], player["y"], player["w"], player["h"]) pipeW = PIPE[IM_WIDTH] pipeH = PIPE[IM_HEIGTH] for uPipe, lPipe in zip(upperPipes, lowerPipes): # upper and lower pipe rects uPipeRect = pygame.Rect(uPipe["x"], uPipe["y"], pipeW, pipeH) lPipeRect = pygame.Rect(lPipe["x"], lPipe["y"], pipeW, pipeH) # player and upper/lower pipe hitmasks pHitMask = HITMASKS["player"][pi] uHitmask = HITMASKS["pipe"][0] lHitmask = HITMASKS["pipe"][1] # if bird collided with upipe or lpipe uCollide = pixelCollision(playerRect, uPipeRect, pHitMask, uHitmask) lCollide = pixelCollision(playerRect, lPipeRect, pHitMask, lHitmask) if uCollide or lCollide: return [True, False] return [False, False]
Example #12
Source File: Sprites.py From Games with MIT License | 5 votes |
def __init__(self, x, y, width, height, SCREENWIDTH, SCREENHEIGHT, **kwargs): pygame.sprite.Sprite.__init__(self) self.init_state = [x, y, width, height] self.rect = pygame.Rect(x, y, width, height) self.base_speed = 10 self.SCREENWIDTH = SCREENWIDTH self.SCREENHEIGHT = SCREENHEIGHT
Example #13
Source File: Sprites.py From Games with MIT License | 5 votes |
def reset(self): self.rect = pygame.Rect(self.init_state[0], self.init_state[1], self.init_state[2], self.init_state[3]) return True
Example #14
Source File: Sprites.py From Games with MIT License | 5 votes |
def __init__(self, x, y, radius, SCREENWIDTH, SCREENHEIGHT, **kwargs): pygame.sprite.Sprite.__init__(self) self.init_state = [x, y, radius*2, radius*2] self.rect = pygame.Rect(x, y, radius*2, radius*2) self.base_speed = [5, 5] self.direction = [random.choice([1, -1]), -1] self.radius = radius self.SCREENWIDTH = SCREENWIDTH self.SCREENHEIGHT = SCREENHEIGHT
Example #15
Source File: Sprites.py From Games with MIT License | 5 votes |
def reset(self): self.rect = pygame.Rect(self.init_state[0], self.init_state[1], self.init_state[2], self.init_state[3]) return True
Example #16
Source File: Sprites.py From Games with MIT License | 5 votes |
def __init__(self, x, y, width, height, **kwargs): pygame.sprite.Sprite.__init__(self) self.init_state = [x, y, width, height] self.rect = pygame.Rect(x, y, width, height)
Example #17
Source File: utils.py From Games with MIT License | 5 votes |
def drawGrids(self): for x in range(NUMGRID): for y in range(NUMGRID): rect = pygame.Rect((XMARGIN+x*GRIDSIZE, YMARGIN+y*GRIDSIZE, GRIDSIZE, GRIDSIZE)) self.drawBlock(rect, color=(0, 0, 255), size=1)
Example #18
Source File: Game12.py From Games with MIT License | 5 votes |
def isValidPos(self, col, row): if col >= 0 and row >= 0 and col < self.num_cols and row < self.num_rows: block_size = Config.get('block_size') temp1 = self.walls + self.boxes temp2 = pygame.Rect(col * block_size, row * block_size, block_size, block_size) return temp2.collidelist(temp1) == -1 else: return False
Example #19
Source File: snake.py From Games with MIT License | 5 votes |
def draw(self, screen): head_x, head_y = self.head_coord[0] * self.cfg.BLOCK_SIZE, self.head_coord[1] * self.cfg.BLOCK_SIZE rect = pygame.Rect(head_x, head_y, self.cfg.BLOCK_SIZE, self.cfg.BLOCK_SIZE) pygame.draw.rect(screen, self.head_colors[0], rect) rect = pygame.Rect(head_x+4, head_y+4, self.cfg.BLOCK_SIZE-8, self.cfg.BLOCK_SIZE-8) pygame.draw.rect(screen, self.head_colors[1], rect) for coord in self.tail_coords: x, y = coord[0] * self.cfg.BLOCK_SIZE, coord[1] * self.cfg.BLOCK_SIZE rect = pygame.Rect(x, y, self.cfg.BLOCK_SIZE, self.cfg.BLOCK_SIZE) pygame.draw.rect(screen, self.tail_colors[0], rect) rect = pygame.Rect(x+4, y+4, self.cfg.BLOCK_SIZE-8, self.cfg.BLOCK_SIZE-8) pygame.draw.rect(screen, self.tail_colors[1], rect)
Example #20
Source File: utils.py From Games with MIT License | 5 votes |
def __init__(self, color, bullet_color, **kwargs): pygame.sprite.Sprite.__init__(self) # 生命值 self.num_life = 3 self.max_num_life = 5 # 最小单元 self.cell = [3, 3] self.num_cols = 15 self.num_rows = 8 # 用于碰撞检测 self.rect = pygame.Rect(0, 550, self.cell[0]*self.num_cols, self.cell[0]*self.num_rows) # 填充颜色区域 self.filled_cells = [7,21,22,23,36,37,38,46,47,48,49,50,51,52,53,54,55,56,57,58,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119] # 飞船颜色 self.color = color # 飞船子弹颜色 self.bullet_color = bullet_color # 子弹是否在冷却中 self.is_cooling = False self.init_count = 35 self.cooling_count = self.init_count # 得分 self.score = 0 # 避免重复增加生命值 self.old_score = -1 self.resetBoom()
Example #21
Source File: utils.py From Games with MIT License | 5 votes |
def resetBoom(self): # 被击中爆炸时用 # 死一条命的时候需要播放一次死亡特效 self.one_dead = False self.boomed_filled_cells = [3,7,12,15,17,20,24,30,36,40,44,45,53,54,58,62,68,74,78,81,83,86,91,95] self.boomed_cell = [3, 3] self.boomed_num_cols = 11 self.boomed_num_rows = 9 self.boomed_rect = pygame.Rect(0, 0, self.boomed_num_cols*self.boomed_cell[0], self.boomed_num_rows*self.boomed_cell[1]) # 控制每帧的时间 self.boomed_count = 0 # 爆炸特效当前帧 self.boomed_frame = 0
Example #22
Source File: utils.py From Games with MIT License | 5 votes |
def __init__(self, category, number, color, bullet_color, **kwargs): pygame.sprite.Sprite.__init__(self) self.cell = [3, 3] # 编号 self.number = number # 种类 self.category = category if category == 'small': self.reward = 20 self.num_cols = 8 self.num_rows = 8 self.rect = pygame.Rect(0, 0, self.num_cols*self.cell[0], self.num_rows*self.cell[1]) self.filled_cells = [[3,4,10,11,12,13,17,18,19,20,21,22,24,25,27,28,30,31,32,33,34,35,36,37,38,39,42,45,49,51,52,54,56,58,61,63], [3,4,10,11,12,13,17,18,19,20,21,22,24,25,27,28,30,31,32,33,34,35,36,37,38,39,41,43,44,46,48,55,57,62]] elif category == 'medium': self.reward = 15 self.num_cols = 11 self.num_rows = 8 self.rect = pygame.Rect(0, 0, self.num_cols*self.cell[0], self.num_rows*self.cell[1]) self.filled_cells = [[2,8,11,14,18,21,22,24,25,26,27,28,29,30,32,33,34,35,37,38,39,41,42,43,44,45,46,47,48,49,50,51,52,53,54,56,57,58,59,60,61,62,63,64,68,74,78,86], [2,8,14,18,24,25,26,27,28,29,30,34,35,37,38,39,41,42,44,45,46,47,48,49,50,51,52,53,54,55,57,58,59,60,61,62,63,65,66,68,74,76,80,81,83,84]] elif category == 'large': self.reward = 10 self.num_cols = 12 self.num_rows = 8 self.rect = pygame.Rect(0, 0, self.num_cols*self.cell[0], self.num_rows*self.cell[1]) self.filled_cells = [[4,5,6,7,13,14,15,16,17,18,19,20,21,22,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,41,42,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,62,63,64,67,68,69,73,74,77,78,81,82,86,87,92,93], [4,5,6,7,13,14,15,16,17,18,19,20,21,22,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,41,42,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,63,64,67,68,74,75,77,78,80,81,84,85,94,95]] self.color = color self.bullet_color = bullet_color self.speed = [8, 20] self.change_count = 0 self.change_flag = False # 被击中爆炸时用 self.boomed_filled_cells = [3,7,12,15,17,20,24,30,36,40,44,45,53,54,58,62,68,74,78,81,83,86,91,95] self.boomed_cell = [3, 3] self.boomed_num_cols = 11 self.boomed_num_rows = 9 self.boomed_rect = pygame.Rect(0, 0, self.boomed_num_cols*self.boomed_cell[0], self.boomed_num_rows*self.boomed_cell[1]) self.boomed_count = 0 self.boomed_frame = 0
Example #23
Source File: utils.py From Games with MIT License | 5 votes |
def __init__(self, x, y, color, **kwargs): pygame.sprite.Sprite.__init__(self) self.cell = [2, 2] self.num_cols = 1 self.num_rows = 4 self.rect = pygame.Rect(x, y, self.num_cols*self.cell[0], self.num_rows*self.cell[1]) self.filled_cells = [0,1,2,3] self.speed = 8 self.color = color
Example #24
Source File: utils.py From Games with MIT License | 5 votes |
def __init__(self, x, y, color): pygame.sprite.Sprite.__init__(self) self.cell = [3, 3] self.num_cols = 3 self.num_rows = 7 self.rect = pygame.Rect(x, y, self.num_cols*self.cell[0], self.num_rows*self.cell[1]) self.filled_cells = [[0,4,8,10,12,16,20], [2,4,6,10,14,16,18]] self.change_count = 0 self.change_flag = False self.speed = 4 self.color = color
Example #25
Source File: modules.py From Games with MIT License | 5 votes |
def __init__(self, x, y, width, height, text, font, font_colors, bg_colors, attribute, **kwargs): pygame.sprite.Sprite.__init__(self) self.rect = pygame.Rect(x, y, width, height) self.text = text self.attribute = attribute self.font_info = font self.font = pygame.font.Font(font[0], font[1]) self.font_colors = font_colors self.is_selected = False self.select_order = None self.bg_colors = bg_colors
Example #26
Source File: Game16.py From Games with MIT License | 5 votes |
def showInfo(text, screen): rect = pygame.Rect(200, 180, 400, 200) pygame.draw.rect(screen, PAPAYAWHIP, rect) font = pygame.font.Font(FONTPATH, 40) text_render = font.render(text, True, BLACK) font_size = font.size(text) screen.blit(text_render, (rect.x+(rect.width-font_size[0])/2, rect.y+(rect.height-font_size[1])/2))
Example #27
Source File: main.py From angry-birds-python with MIT License | 5 votes |
def draw_level_cleared(): """Draw level cleared""" global game_state global bonus_score_once global score level_cleared = bold_font3.render("Level Cleared!", 1, WHITE) score_level_cleared = bold_font2.render(str(score), 1, WHITE) if level.number_of_birds >= 0 and len(pigs) == 0: if bonus_score_once: score += (level.number_of_birds-1) * 10000 bonus_score_once = False game_state = 4 rect = pygame.Rect(300, 0, 600, 800) pygame.draw.rect(screen, BLACK, rect) screen.blit(level_cleared, (450, 90)) if score >= level.one_star and score <= level.two_star: screen.blit(star1, (310, 190)) if score >= level.two_star and score <= level.three_star: screen.blit(star1, (310, 190)) screen.blit(star2, (500, 170)) if score >= level.three_star: screen.blit(star1, (310, 190)) screen.blit(star2, (500, 170)) screen.blit(star3, (700, 200)) screen.blit(score_level_cleared, (550, 400)) screen.blit(replay_button, (510, 480)) screen.blit(next_button, (620, 480))
Example #28
Source File: main.py From angry-birds-python with MIT License | 5 votes |
def draw_level_failed(): """Draw level failed""" global game_state failed = bold_font3.render("Level Failed", 1, WHITE) if level.number_of_birds <= 0 and time.time() - t2 > 5 and len(pigs) > 0: game_state = 3 rect = pygame.Rect(300, 0, 600, 800) pygame.draw.rect(screen, BLACK, rect) screen.blit(failed, (450, 90)) screen.blit(pig_happy, (380, 120)) screen.blit(replay_button, (520, 460))
Example #29
Source File: wrapped_flappy_bird.py From A3C_Keras_FlappyBird with MIT License | 5 votes |
def checkCrash(player, upperPipes, lowerPipes): """returns True if player collders with base or pipes.""" pi = player['index'] player['w'] = IMAGES['player'][0].get_width() player['h'] = IMAGES['player'][0].get_height() if player['y'] <= 1: return True # if player crashes into ground if player['y'] + player['h'] >= BASEY - 1: return True else: playerRect = pygame.Rect(player['x'], player['y'], player['w'], player['h']) for uPipe, lPipe in zip(upperPipes, lowerPipes): # upper and lower pipe rects uPipeRect = pygame.Rect(uPipe['x'], uPipe['y'], PIPE_WIDTH, PIPE_HEIGHT) lPipeRect = pygame.Rect(lPipe['x'], lPipe['y'], PIPE_WIDTH, PIPE_HEIGHT) # player and upper/lower pipe hitmasks pHitMask = HITMASKS['player'][pi] uHitmask = HITMASKS['pipe'][0] lHitmask = HITMASKS['pipe'][1] # if bird collided with upipe or lpipe uCollide = pixelCollision(playerRect, uPipeRect, pHitMask, uHitmask) lCollide = pixelCollision(playerRect, lPipeRect, pHitMask, lHitmask) if uCollide or lCollide: return True return False
Example #30
Source File: utils.py From gaige with MIT License | 5 votes |
def rayTraceWorldCustom(p1, p2, world, rayFunc): x = 0 y = 0 w = 0 h = 0 if p1[0] < p2[0]: x = p1[0] w = p2[0] - x else: x = p2[0] w = p1[0] - x if p1[1] < p2[1]: y = p1[1] h = p2[1] - y else: y = p2[1] h = p1[1] - y rect = pygame.Rect(x, y, w, h) for o in world.quadTree.hit(rect): for l in o.getLines(): hit = rayFunc(p1, p2, l) if hit != None: return hit for l in world.getOtherLines(): hit = rayFunc(p1, p2, l) if hit != None: return hit return None # Check whether the line between p1 and p2 intersects with line anywhere except an endpoint.