def collision(self, speedX, speedY, platform): for n in platform: if sprite.collide_rect(self, n): if isinstance(n, block.Spike): self.death() if isinstance(n, block.Trophey): win = True if win == if cond is True: t = pygame.font.Font(None, 80) text = t.render('You Win!!!', 3, (0, 0, 0)) screen.blit(text, (250, 250)) if speedX > 0: self.rect.right = n.rect.left if speedX < 0: self.rect.left = n.rect.right if speedY > 0: self.rect.bottom = n.rect.top self.naZemle = True self.speedY = 0 if speedY < 0: self.rect.top = n.rect.bottom self.speedY = 0
def score(self): self.flag = False a = name[name.index('\\') + 1:] con = sqlite3.connect('data\\records.db') cur = con.cursor() result = cur.execute( '''SELECT * from records where map="{}"'''.format(a)).fetchall() result = list( map(lambda x: x[1] + ' = ' + str(x[0]), sorted(result, key=lambda x: x[0])))[:10] self.render(result[::-1]) font = pygame.font.Font(None, 30) screen.blit(font.render('назад', 1, (255, 255, 255)), (10, 30)) pygame.display.flip() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: terminate() elif event.type == pygame.MOUSEBUTTONDOWN: p = event.pos if 10 <= p[0] <= font.size('назад')[0] + 10 and 30 <= p[ 1] <= font.size('назад')[1] + 30: self.flag = True screen.fill((0, 0, 0)), screen.blit(FON, (0, 0)) return
def add_unit(forces_num): par = params[forces_num] units = par['forces'] par['num'] = len(units.list) num = par['num'] position = par['position'] img = par['image_side'] font = pygame.font.Font("font.ttf", 24) count = "%s" % ('Count: ' + str(num)) map_ = pygame.image.load(img) screen.blit(map_, (0, 0)) text = font.render(count, True, (255, 150, 0, 255)) screen.blit(text, position)
def buy_unit(price): """Allows buying units""" global total_gold font = pygame.font.Font("font.ttf", 24) if total_gold - price < 0: total_gold = 0 else: total_gold -= price gold = "%s" % ('Gold: ' + str(total_gold)) map_ = pygame.image.load('game/images/tab.png') screen.blit(map_, (0, 0)) text = font.render(gold, True, (255, 255, 0, 255)) screen.blit(text, (10, 10)) return total_gold
def update(self, pos, flag=False): font = pygame.font.Font(None, 30) x, y = pos if 20 <= x <= 30 + self.play[0] + 10 and 300 <= y <= 300 + self.play[ 1] and self.flag: screen.blit(font.render(self.word[0], 1, (0, 0, 0)), (30, 300)) if flag: return True elif 20 <= x <= 30 + self.new_map[ 0] + 10 and 340 <= y <= 340 + self.new_map[1] and self.flag: screen.blit(font.render(self.word[1], 1, (0, 0, 0)), (30, 340)) if flag: self.map() elif 20 <= x <= 30 + self.records[ 0] + 10 and 380 <= y <= 380 + self.records[1] and self.flag: screen.blit(font.render(self.word[2], 1, (0, 0, 0)), (30, 380)) if flag: self.score() elif 20 <= x <= 30 + self.bye[0] + 10 and 420 <= y <= 420 + self.bye[ 1] and self.flag: screen.blit(font.render(self.word[3], 1, (0, 0, 0)), (30, 420)) if flag: terminate() elif self.flag: self.draw()
def draw(self): """ Draw the game object on the screen by copying its image onto the Surface. """ screen_size = screen.get_size() off_screen = (self.location[0] > screen_size[0] or self.location[0] + self.size[0] < 0 or self.location[1] > screen_size[1] or self.location[1] + self.size[1] < 0) if off_screen: # No point in drawing it if it isn't visible return if self.orientation == "Right": screen.blit(self.surface, self.location) else: screen.blit(pygame.transform.flip(self.surface, True, False), self.location)
def draw(self): font = pygame.font.Font(None, 30) play = font.render(self.word[0], 1, (255, 204, 0)) self.play = font.size(self.word[0]) new_map = font.render(self.word[1], 1, (255, 204, 0)) self.new_map = font.size(self.word[1]) records = font.render(self.word[2], 1, (255, 204, 0)) self.records = font.size(self.word[2]) bye = font.render(self.word[3], 1, (255, 204, 0)) self.bye = font.size(self.word[3]) screen.blit(play, (30, 300)) screen.blit(new_map, (30, 340)) screen.blit(records, (30, 380)) screen.blit(bye, (30, 420))
def blitToScreen(self, x_loc, y_loc): from game import screen, screen_grid screen_grid = screen_grid() screen_rows = screen_grid.rows screen_cols = screen_grid.cols rows_to_top = screen_rows // 2 cols_to_left = screen_cols // 2 top_left_x = x_loc - cols_to_left top_left_y = y_loc - rows_to_top image_x = 0 image_y = 0 screen = screen() for x_index in range(screen_cols): image_y = 0 for y_index in range(screen_rows): image = self.imageAt(top_left_x + x_index, top_left_y + y_index) if image != None: screen.blit(image, (image_x, image_y)) image_y += screen_grid.tile_height() image_x += screen_grid.tile_width() return
def load_screen(): if True: p = (-1, -1) f = False group = pygame.sprite.Group() b = Button() screen.blit(FON, (0, 0)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: terminate() elif event.type == pygame.MOUSEMOTION: p = event.pos group.update(event.pos, False) b.update(event.pos) elif event.type == pygame.MOUSEBUTTONDOWN: if b.update(event.pos, True): game() return group.update(p, f) group.draw(screen) pygame.display.flip() clock.tick(FPS)
def draw_forces(forces_num): par = params[forces_num] num = par['num'] if num != 0: pos = par['unit_position'] path = par['path'] x = pos[0] y = pos[1] zoom = par['zoom_config'] img = pygame.image.load(path) img = pygame.transform.scale(img, zoom) map_ = pygame.image.load('game/images/map.png') map_ = pygame.transform.scale(map_, display_settings) while x < castle_position: x += 3 screen.blit(map_, (0, 0)) screen.blit(img, (x, y)) pygame.display.flip() screen.blit(map_, (0, 0))
def deal_damage(num): """Deal damage to the castle""" par = params[num] units = par['forces'] damage = units.list[0].total_dmg() global HP if HP - damage <= 0: HP = 0 font = pygame.font.Font("font.ttf", 154) text = font.render('VICTORY!', True, (255, 255, 0, 255)) screen.blit(text, (400, 300)) else: HP -= damage health = "%s" % ('HP: ' + str(HP)) map_ = pygame.image.load('game/images/hp.png') screen.blit(map_, (0, 0)) font = pygame.font.Font("font.ttf", 34) text = font.render(health, True, (0, 0, 0, 255)) screen.blit(text, (1145, 53)) return health
def map(self): self.flag = False screen.fill((0, 0, 0)) path = r'map' files = os.listdir(path) font = pygame.font.Font(None, 30) lst = self.chunks(files, 20) i = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: terminate() elif event.type == pygame.MOUSEBUTTONDOWN: p = event.pos if 10 <= p[0] <= font.size('назад')[0] + 10 and 30 <= p[ 1] <= font.size('назад')[1] + 30: if i: i -= 1 else: self.flag = True screen.fill((0, 0, 0)), screen.blit(FON, (0, 0)) return elif 920 <= p[0] <= font.size( 'далее')[0] + 920 and 30 <= p[1] <= font.size( 'далее')[1] + 30: if i + 1 < len(lst): i += 1 for _ in range(len(lst[i])): if 400 <= p[0] <= font.size(lst[i][_])[0] + 400 and \ 30 * _ + 30 <= p[1] <= font.size(lst[i][_])[1] + 30 + 30 * _: name, self.flag = 'map\\' + lst[i][_], True screen.fill((0, 0, 0)), screen.blit(FON, (0, 0)) return self.render(lst[i]) screen.blit(font.render('назад', 1, (255, 255, 255)), (10, 30)) screen.blit(font.render('далее', 1, (255, 255, 255)), (920, 30)) pygame.display.flip()
def out_of_gold(): if total_gold == 0 and forces_members() == 0: font = pygame.font.Font("font.ttf", 154) text = font.render('WASTED', True, (255, 0, 0, 255)) screen.blit(text, (400, 300))
def main(): global surfaces scene = 6 while True: x = redhair.hitbox.rect.x for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() if event.type == pygame.KEYDOWN: if len(game.images) != 0: del (game.images[-1]) if event.type == pygame.KEYDOWN: if event.key == pygame.K_d: redhair.velocity += 4 elif event.key == pygame.K_a: redhair.velocity -= 4 elif event.key == pygame.K_w: if len(game.indices_of_hit_list) != 0: for index in game.indices_of_hit_list: if redhair.hitbox.bottom_rect.colliderect( surfaces[index]): redhair.is_jumping = True elif event.type == pygame.KEYUP: if event.key == pygame.K_d: redhair.velocity -= 4 elif event.key == pygame.K_a: redhair.velocity += 4 if len(game.images) == 0: game.hit_game_boundary(redhair) redhair.jumping_system() draw_backgrounds(game.backgrounds, screen) # draw_hitboxes() redhair.move_horizontale() # if redhair.velocity < 0: # # game.scene_move_backgrounds('right') # surfaces = game.move_world(surfaces, 9, 'right') # elif redhair.velocity > 0: # # game.scene_move_backgrounds('left') # surfaces = game.move_world(surfaces, -9, 'left') start, direction, speed = game.start_scrolling_world(redhair) if start: game.move_world(surfaces, -speed, direction) # put to three rects around every object that way you know which side the player is hitting by testing which rects of the three he's colliding with the top one or the left on or the right one # if redhair.hitbox.rect.colliderect(surfaces[0]): # redhair.hitbox.rect.bottom = surfaces[0].rect.top # if redhair.hitbox.rect.collidelist(surfaces): if len(redhair.hitbox.rect.collidelistall(surfaces)) != 0: game.indices_of_hit_list = redhair.hitbox.rect.collidelistall( surfaces) for index in game.indices_of_hit_list: if redhair.hitbox.rect.colliderect( surfaces[index].right_rect): redhair.hitbox.rect.left = surfaces[ index].right_rect.left elif redhair.hitbox.rect.colliderect( surfaces[index].left_rect): redhair.hitbox.rect.right = surfaces[ index].left_rect.left reset(redhair.hitbox, [100, 100]) elif redhair.hitbox.rect.colliderect( surfaces[index].top_rect): redhair.hitbox.rect.bottom = surfaces[ index].top_rect.top elif redhair.hitbox.rect.colliderect( surfaces[index].bottom_rect): redhair.hitbox.rect.top = surfaces[ index].bottom_rect.top # pygame.draw.rect(screen, (255, 255, 0), surfaces[0].top_rect, 2) # pygame.draw.rect(screen, (255, 255, 0), redhair.hitbox.rect, 2) # pygame.draw.rect(screen, (255, 255, 0), redhair.hitbox.top_rect, 2) # pygame.draw.rect(screen, (255, 255, 0), redhair.hitbox.bottom_rect, 2) # pygame.draw.rect(screen, (255, 255, 0), redhair.hitbox.right_rect, 2) # pygame.draw.rect(screen, (255, 255, 0), redhair.hitbox.left_rect, 2) # screen.blit(surfaces[1].load_image, (surfaces[1].rect.x, surfaces[1].rect.y)) for surface in surfaces: if surface.image: screen.blit(surface.load_image, (surface.rect.x, surface.rect.y)) # screen.blit(redhair.loadedImage, (redhair.hitbox.rect.x + redhair.xoffset, redhair.hitbox.rect.y + redhair.yoffset)) screen.blit(redhair.loadedImage, (redhair.hitbox.rect.x + redhair.xoffset, redhair.hitbox.rect.y + redhair.yoffset)) # # pygame.draw.rect(screen, (255, 255, 0), surfaces[1], 2) # pygame.draw.rect(screen, (255, 255, 0), surfaces[1].bottom_rect, 2) # pygame.draw.rect(screen, (255, 255, 0), game.game_boundaries[0], 2) # pygame.draw.rect(screen, (255, 255, 0), game.game_boundaries[1], 2) # pygame.draw.rect(screen, (255, 255, 0), game.boundaries[0], 2) # pygame.draw.rect(screen, (255, 255, 0), game.boundaries[1], 2) # pygame.draw.rect(screen, (255, 255, 0), surfaces[1].right_rect, 2) # pygame.draw.rect(screen, (255, 255, 0), surfaces[1].left_rect, 2) # pygame.draw.rect(screen, (255, 255, 0), surfaces[1].top_rect, 2) else: screen.blit(game.load_image(-1), (0, 0)) pygame.display.update() clock.tick(100) # x,y # top, left, bottom, right # topleft, bottomleft, topright, bottomright # midtop, midleft, midbottom, midright # center, centerx, centery # size, width, height # w,h # rect1.right = 10 # rect2.center = (20,30) # hero = hero.move(10, 0) # if redhair.rect.right > surfaces[1].rect.left and redhair.rect.left < surfaces[1].rect.left: # it works الحمد لله # redhair.rect.right = surfaces[1].rect.left # elif redhair.rect.left < surfaces[1].rect.right and redhair.rect.right > surfaces[1].rect.right: # redhair.rect.left = surfaces[1].rect.right # elif redhair.rect.bottom > surfaces[1].rect.top and redhair.rect.top < surfaces[1].rect.top: # redhair.rect.bottom = surfaces[1].rect.top # elif redhair.rect.top > surfaces[1].rect.bottom and redhair.rect.bottom < surfaces[1].rect.bottom: # redhair.rect.top = surfaces[1].rect.bottom
def render(self, lst): screen.fill((0, 0, 0)) for i in range(len(lst)): font = pygame.font.Font(None, 30) screen.blit(font.render(lst[i], 1, (255, 255, 255)), (400, 30 * i + 30))
def init(): load('game/images/map.png') load('game/images/tab.png') load('game/images/troop.png') load('game/images/gen.png') load('game/images/squad.png') load('game/images/army.png') load('game/images/hp.png') font = pygame.font.Font("font.ttf", 24) text = font.render('Gold: 1000', True, (255, 255, 0, 255)) screen.blit(text, (10, 10)) text = font.render('HP: 1000', True, (0, 0, 0, 255)) screen.blit(text, (1145, 53)) text = font.render('Count: 0', True, (255, 150, 0, 255)) screen.blit(text, (30, 235)) screen.blit(text, (30, 365)) screen.blit(text, (30, 490)) screen.blit(text, (30, 630)) font = pygame.font.Font("font.ttf", 34) text = font.render('Click on unit\'s image to attack the castle!', True, (0, 0, 50, 255)) screen.blit(text, (300, 660)) pygame.display.set_caption("A Strategy Game")
def load(path): map_ = pygame.image.load(path) screen.blit(map_, (0, 0))