def renderControls(self): img = self.registry.get('control_images') surface = self.registry.get('surface') round = self.registry.get('round') control_images = self.registry.get('control_images') # Show round number font = pygame.font.Font(FONT, adjheight(20)) text = font.render(("R= %d" % round), True, FONT_GREEN, FONT_BLACK) surface.blit(text, ROUND_POSITION) # Show the bullets startingX, startingY = SHOT_POSITION surface.blit(control_images, SHOT_POSITION, SHOT_RECT) for i in range(self.gun.rounds): x = startingX + adjwidth(10) + adjwidth(i * 18) y = startingY + adjheight(5) surface.blit(control_images, (x, y), BULLET_RECT) # display the rectangle with green rounded edges. surface.blit(control_images, BUTTON_POSITION, SCORE_RECT) # cover up the SCORE with a black rectangle pygame.draw.rect(surface, FONT_BLACK, BUTTON_RECT) controls = font.render(self.selection, True, FONT_GREEN) surface.blit(controls, self.selection_position) # Show the hit counter surface.blit(control_images, HIT_POSITION, HIT_RECT) startingX, startingY = HIT_DUCK_POSITION for i in range(10): x = startingX + adjwidth(i * 18) y = startingY if self.hitDucks[i]: surface.blit(img, (x, y), HIT_DUCK_RED_RECT) else: surface.blit(img, (x, y), HIT_DUCK_WHITE_RECT) # Show the score surface.blit(img, SCORE_POSITION, SCORE_RECT) font = pygame.font.Font(FONT, adjheight(20)) text = font.render(str(self.registry.get('score')), True, FONT_WHITE) x, y = FONT_STARTING_POSITION x -= text.get_width() surface.blit(text, (x, y))
def render(self): timer = int(time.time()) surface = self.registry.get('surface') sprites = self.registry.get('sprites') x, y = self.dogPosition width, height = DOG_FRAME # Always have round + controls self.render_notices() self.renderControls() # Update animation frame if (self.frame % 15) == 0: self.animationFrame += 1 # First the dog walks/sniffs if self.animationFrame < 5: x += 1 self.dogPosition = (x, y) rect = ((width * self.animationFrame), 0, width, height) # Then the dog jumps else: self.animationDelay = 16 animationFrame = self.animationFrame % 5 rect = ((width * animationFrame), height, width, height) # Trigger barking if (self.barkCount < 2) and not pygame.mixer.get_busy(): self.registry.get('soundHandler').enqueue('bark') self.barkCount += 1 # First Jump frame if animationFrame == 1: self.dogPosition = (x + adjwidth(5)), (y - adjheight(10)) # Second jump frame elif animationFrame == 2: self.dogPosition = (x + adjwidth(5)), (y + adjheight(5)) elif animationFrame > 2: return # Animation is over # Add the dog surface.blit(sprites, self.dogPosition, rect)
def game_intro(self): intro = True while intro: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() self.surface.blit(self.background, (0, 0)) large_text = pygame.font.Font(FONT, adjheight(20)) my_title = large_text.render('DUCKHUNT', True, FONT)
def render_notices(self): if len(self.notices) is 0: return elif len(self.notices) is 1: self.notices.add("") surface = self.registry.get('surface') control_images = self.registry.get('control_images') font = pygame.font.Font(FONT, adjheight(20)) line1 = font.render(str(self.notices[0]), True, (255, 255, 255)) line2 = font.render(str(self.notices[1]), True, (255, 255, 255)) x, y = NOTICE_POSITION x1 = x + (NOTICE_WIDTH - line1.get_width()) / 2 x2 = x + (NOTICE_WIDTH - line2.get_width()) / 2 surface.blit(control_images, NOTICE_POSITION, NOTICE_RECT) surface.blit(line1, (x1, NOTICE_LINE_1_HEIGHT)) surface.blit(line2, (x2, NOTICE_LINE_2_HEIGHT))
import random from game.registry import adjpos, adjheight FRAME_SIZE = adjpos (81, 75) XOFFSET, YOFFSET = adjpos (250, 225) FLYOFF_YOFFSET = YOFFSET + adjheight (155) FALL_YOFFSET = YOFFSET + adjheight (235) class Duck(object): def __init__(self, registry): self.registry = registry self.imageReversed = False self.isDead = False self.isFinished = False self.flyOff = False self.sprites = registry.get('sprites') self.rsprites = registry.get('rsprites') # Animation self.animationDelay = 8 self.frame = 0 self.animationFrame = 0 self.justShot = False # Find a starting position surface = registry.get('surface') x = random.choice([0, surface.get_width()]) y = random.randint(0, surface.get_height() / 2) self.position = x, y
RECTANGLES['BUTTON_RECT'].get('width'), RECTANGLES['BUTTON_RECT'].get('height')) FONT = os.path.join('media', 'arcadeclassic.ttf') FONT_GREEN = (COLORS['FONT_GREEN'].get('R'), COLORS['FONT_GREEN'].get('G'), COLORS['FONT_GREEN'].get('B')) FONT_BLACK = (COLORS['FONT_BLACK'].get('R'), COLORS['FONT_BLACK'].get('G'), COLORS['FONT_BLACK'].get('B')) FONT_WHITE = (COLORS['FONT_WHITE'].get('R'), COLORS['FONT_WHITE'].get('G'), COLORS['FONT_WHITE'].get('B')) NOTICE_WIDTH = adjwidth(128) NOTICE_LINE_1_HEIGHT = adjheight(408) NOTICE_LINE_2_HEIGHT = adjwidth(430) registry = None class BaseState(object): def __init__(self): global registry self.registry = registry self.timer = int(time.time()) self.notices = set() self.gun = Gun(self.registry) self.hitDucks = [False for i in range(10)] self.hitDuckIndex = 0 self.mouse_text_position = (