def add_sprite(game_state, num, mirror = False): splice_canvas = game_state.get('splice_canvas') screen_size = game_state.get('screen_size') location_x = 0 if num == "1": location_x = 0.4 * splice_canvas.w elif num == "2": location_x = 0.8 * splice_canvas.w else: return game_state tempsprite = ImageSprite(location_x, (0.5 * splice_canvas.h), game_state.get('active_sprite' + num)) if mirror == True: tempsprite.image = pygame.transform.flip(tempsprite.image, True, False) tempsprite.origimage = pygame.transform.flip(tempsprite.origimage, True, False) factor = 0.8 * splice_canvas.y / tempsprite.orig_height tempsprite.scale = 1000 * factor tempsprite.rect.center = (location_x, (0.5 * splice_canvas.h)) tempsprite.update_sprite() splice_sprites.add(tempsprite) return game_state
def workshop_loop(game_state): """The workshop screen loop. """ built_sprites = game_state.get('built_sprites') if len(built_sprites) > 2: # should be > 2, 0 for testing end game screen game_state = end_game(game_state) return game_state #remove all sprites from previous time screen was open general_sprites.empty() scrollable_sprites.empty() left_sprite.empty() right_sprite.empty() game_state.update({'active_sprite1': None, 'active_sprite2': None}) game_surface = game_state.get('game_surface') clock = game_state.get('clock') fps = game_state.get('fps') click = game_state.get('click_sound') size = game_state.get('screen_size') screen_width = size[0] screen_height = size[1] toast_stack = game_state.get('toast_stack') available_funds = game_state.get('available_funds') company = game_state.get('company_name') held_down = False scroll_surface = pygame.surface.Surface( (screen_width * 0.2, screen_height * 0.8)) scroll_rect = scroll_surface.get_rect(x=50, y=50) background_image = ImageSprite(0, 0, os.getcwd() + '/data/imgbase/workshop.png') general_sprites.add(background_image) #little hacky f = pygame.font.SysFont(None, 30) rendered_company_name_width = f.render(company, True, (0, 0, 0)).get_size()[0] general_sprites.add( TextSprite((screen_width * 0.31) + (250 * 0.5) - (rendered_company_name_width * 0.5), screen_height * 0.32, rendered_company_name_width, screen_height * 0.2, company)) general_sprites.add( ButtonSprite(screen_width * 0.8, screen_height * 0.05, 'QUIT', quit_game, []), ) splice_button.add( ButtonSprite(screen_width * 0.4, screen_height * 0.5, 'Splice!', start_splicer, [], color=(0, 255, 0), text_color=(0, 0, 0))) items = os.listdir(os.getcwd() + '/data/pixel-components') x = 10 y = 10 general_sprites.add( ButtonSprite(50, 50 - 20, 'Up', scroll_up, [scroll_surface], w=screen_width * 0.2)) general_sprites.add( ButtonSprite(50, screen_height * 0.8 + 50, 'Down', scroll_down, [scroll_surface], screen_width * 0.2)) for item in sorted(items): item_file = os.getcwd() + '/data/pixel-components/' + item temp_item = ButtonImageSprite(x, y, item_file, add_to_workbench, [item_file], w=100, h=100) temp_item.rect.centerx = x + (temp_item.w / 2) scrollable_sprites.add(temp_item) item_text = item[6:-4] scrollable_sprites.add( ButtonSprite(x + 110, y + 40, item_text, add_to_workbench, [item_file], w=100)) y += 125 frame_x = screen_width - 356 frame_y = screen_height - 155 for keepsake_entry in built_sprites: keepsake = keepsake_entry.get('sprite') keepsake.rect.x = frame_x keepsake.rect.y = frame_y - keepsake.rect.h general_sprites.add(keepsake) keepsake_name = keepsake_entry.get('name') general_sprites.add( TextSprite(frame_x, frame_y, screen_width * 0.1, screen_height * 0.05, keepsake_name)) frame_x += screen_width * 0.1 frame_y += screen_height * 0.05 # Want to refactor this body into seperate functions. while not game_state.get('screen_done'): # Handle events. for event in pygame.event.get(): if event.type == pygame.QUIT: quit_game(game_state) elif event.type == pygame.MOUSEBUTTONDOWN: if scroll_rect.collidepoint(event.pos) and event.button == 4: scroll_up(game_state, scroll_surface) elif scroll_rect.collidepoint(event.pos) and event.button == 5: scroll_down(game_state, scroll_surface) elif (event.button == 1): held_down = True b = button_at_point(general_sprites, event.pos) c = button_at_point(scrollable_sprites, (event.pos[0] - 50, event.pos[1] - 50)) if b: click.play() game_state = b.on_click(game_state) if c and scroll_rect.collidepoint(event.pos): click.play() game_state = c.on_click(game_state) elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1: held_down = False # Needed to hold down up and down scroll buttons if held_down: b = button_at_point(general_sprites, pygame.mouse.get_pos()) b2 = button_at_point(splice_button, pygame.mouse.get_pos()) if b: game_state = b.on_click(game_state) elif b2: game_state = b2.on_click(game_state) # Update. toast_stack.update() # Display. game_surface.fill((255, 0, 0)) scroll_surface.fill((200, 200, 200)) general_sprites.draw(game_surface) # only draw splice button if both sprites present if len(left_sprite.sprites()) and len(right_sprite.sprites()): splice_button.draw(game_surface) # draw scrollable items (hacky, obvs) offset = scrollable_sprites.sprites()[0].rect.y - 10 for i, s in enumerate(scrollable_sprites.sprites()): bg = pygame.Surface((screen_width * 0.2, 125)) if i % 2: bg.fill((150, 150, 150)) else: bg.fill((50, 50, 50)) scroll_surface.blit(bg, (0, (i * 125) + offset)) scroll_surface.blit(s.image, s.rect) left_sprite.draw(game_surface) right_sprite.draw(game_surface) toast_stack.draw(game_surface) game_surface.blit(scroll_surface, (50, 50)) funds_string = "Lifetime earnings: £{0:.2f}".format(available_funds) rendered_text = pygame.font.SysFont(None, 25).render(funds_string, True, (0, 0, 0)) game_surface.blit(rendered_text, (screen_width * 0.7, screen_height * 0.1)) pygame.display.update() clock.tick(fps) return game_state
def main_menu_loop(game_state): """The main menu screen loop. """ game_surface = game_state.get('game_surface') clock = game_state.get('clock') fps = game_state.get('fps') screen_size = game_state.get('screen_size') screen_width = screen_size[0] screen_height = screen_size[1] framecount = 1 #toast_stack = game_state.get('toast_stack') logo_sprites = pygame.sprite.OrderedUpdates() logo = ImageSprite(screen_width * 0.315, screen_height * 0.15, os.getcwd() + "/images/Tank.png") logo.rect.centerx = (screen_width / 2) logo_sprites.add(logo) # company_name = game_state.get('company_name') # input_font = pygame.font.Font("ARCADECLASSIC.TTF", 40) # input_width, input_height = 0.1* screen_width, 0.0625*screen_height # company_name_input = InputBox( # (0.5*screen_width) - (0.5*input_width), # 0.68*screen_height, # input_width + 10, # input_height + 10, # input_font, # (0, 0, 255), # (255, 255, 0), # center_x=0.5*screen_width, # text=company_name, # max_width=500 # ) # company_name_input.active = True # Main group of sprites to display. all_sprites = pygame.sprite.OrderedUpdates() all_sprites.add( ButtonSprite((screen_width * 0.455), (screen_height * 0.8), 'Play!', start_game, []), ButtonSprite( (screen_width * 0.455), (screen_height * 0.9), 'Quit', quit_game, [], ), ) # prompt = TextSprite((0.43*screen_width) , 0.62 *screen_height, 400, 30, "Enter Company Name", text_color=(255,255,255), arcade_font=True) # prompt.rect.centerx = (screen_width/2) # name_prompt = pygame.sprite.Group() # name_prompt.add(prompt) # Want to refactor this body into seperate functions. while not game_state.get('screen_done'): # Handle events. for event in pygame.event.get(): if event.type == pygame.QUIT: quit_game(game_state) elif event.type == pygame.MOUSEBUTTONDOWN: b = button_at_point(all_sprites, event.pos) if b: game_state = b.on_click(game_state) # elif event.type == pygame.KEYDOWN: # if event.key == pygame.K_RETURN: # game_state.update({'company_name': company_name_input.text}) # start_game(game_state) # else: # company_name_input.event_handle(event) #Input Box Class has inbuilt event handling function for key down events. # Update. all_sprites.update() #toast_stack.update() # Display. game_surface.fill((0, 0, 0)) logo_sprites.draw(game_surface) all_sprites.draw(game_surface) pygame.display.update() clock.tick(fps) return game_state
def arena_loop(game_state): """The arena screen loop.""" game_surface = game_state.get('game_surface') clock = game_state.get('clock') fps = game_state.get('fps') size = game_state.get('screen_size') screen_width = size[0] screen_height = size[1] background_image = ImageSprite(0, 0, os.getcwd() + '/images/Background.png') general_sprites.add(background_image) general_sprites.add( ButtonSprite(screen_width * 0.8, screen_height * 0.05, 'QUIT', quit_game, []), ) tank1 = ImageSprite(0, 0, os.getcwd() + '/images/Tank.png') tank2 = ImageSprite(300, 0, os.getcwd() + '/images/Tank.png') tank_sprites.add(tank1, tank2) while not game_state.get('screen_done'): # Handle events. for event in pygame.event.get(): if event.type == pygame.QUIT: quit_game(game_state) elif event.type == pygame.MOUSEBUTTONDOWN: b = button_at_point(general_sprites, event.pos) if b: game_state = b.on_click(game_state) elif event.type == pygame.KEYDOWN: keys = pygame.key.get_pressed() if keys[pygame.K_RIGHT]: tank1.move((10, 0)) elif keys[pygame.K_LEFT]: tank1.move((-10, 0)) elif keys[pygame.K_UP]: tank1.move((0, -10)) elif keys[pygame.K_DOWN]: tank1.move((0, 10)) elif keys[pygame.K_d]: tank2.move((10, 0)) elif keys[pygame.K_a]: tank2.move((-10, 0)) elif keys[pygame.K_w]: tank2.move((0, -10)) elif keys[pygame.K_s]: tank2.move((0, 10)) # Display. game_surface.fill((255, 0, 0)) general_sprites.draw(game_surface) tank_sprites.draw(game_surface) pygame.display.update() clock.tick(fps) return game_state
def init_image(self): self.image = pygame.Surface((self.w, self.h)) self.image.fill((150, 150, 150)) loaded_img = pygame.image.load(self.img_path) self.image.blit(loaded_img, (0, 0)) name = self.product.get('name') # Display the article title. title = self.title_font.render(name, True, self.text_color) pos = ((self.w * 0.25) - (title.get_size()[0] * 0.5), (self.h * 0.2)) self.image.blit(title, pos) # Generate a tagline. tagline_template = random.choice(tagline_templates) tagline_text = tagline_template.format(self.company, name) tagline_text_box = TextSprite(self.x, self.y, (self.w * 0.6), (self.h * 0.9), tagline_text) self.image.blit(tagline_text_box.image, ((self.w * 0.05), (self.h * 0.3))) # Display the product image product_image = ImageSprite(0, 0, self.product.get('img')) scaled_image = aspect_scale(product_image.image, ((self.w * 0.4), (self.h * 0.4))) self.image.blit(scaled_image, ((self.w * 0.65), (self.h * 0.3))) scaled_w, scaled_h = scaled_image.get_size() box_rect = pygame.Rect((self.w * 0.65), (self.h * 0.3), scaled_w, scaled_h) pygame.draw.rect(self.image, (50, 50, 50), box_rect, 2) # Choose a type of review # suuuper quick hacky way of percentage choices choice_list = [ 'good', 'good', 'good', 'medium', 'medium', 'medium', 'medium', 'medium', 'bad', ] review_type = random.choice(choice_list) if (random.random() < 0.05): review_type = 'very_bad' self.review_type = review_type # Display the reviews with their scores. reviews = get_reviews(review_type) y_offset = self.h * 0.45 for review in reviews: score = review.get('score') text = review.get('text') + ' ' + str(score) + '/10' review_text_box = TextSprite(self.x, self.y, (self.w * 0.5), (self.h * 0.5), text) self.image.blit(review_text_box.image, ((self.w * 0.075), y_offset)) y_offset += 50 self.original_image = self.image self.original_scale = (self.w, self.h) self.current_modifier = 0.004 self.image = pygame.transform.scale(self.image, self.get_current_scale())