|
| 1 | +import pygame,random,sys,time |
| 2 | +pygame.init() |
| 3 | +WIDTH, HEIGHT = 600, 400 |
| 4 | +DOG_SPEED = 8 |
| 5 | +BONE_FALL_SPEED = 5 |
| 6 | +TIME_LIMIT = 60 # seconds for no-score timeout |
| 7 | +WHITE = (255, 255, 255) # Background color |
| 8 | +BROWN = (139, 69, 19) # Dog body color |
| 9 | +DARK_BROWN = (100, 50, 20) # Dog ear and tail color |
| 10 | +BLACK = (0, 0, 0) # Dog eye and nose color |
| 11 | +BONE_LIGHT = (255, 255, 200) # Bone light color |
| 12 | +BONE_DARK = (240, 220, 170) # Bone dark color |
| 13 | + |
| 14 | +screen = pygame.display.set_mode((WIDTH, HEIGHT)) |
| 15 | +pygame.display.set_caption("🐶 Hungry Dog") |
| 16 | +clock = pygame.time.Clock() |
| 17 | +font = pygame.font.SysFont("Arial", 28) |
| 18 | +dog = pygame.Rect(WIDTH // 2 - 25, HEIGHT - 70, 60, 40) |
| 19 | +bones = [] |
| 20 | +score = 0 |
| 21 | +start_time = None # Timer starts only if score = 0 |
| 22 | +def draw_bone(bone_rect): |
| 23 | + """Draws a bone using simple circles and rectangles.""" |
| 24 | + pygame.draw.rect( |
| 25 | + screen, BONE_DARK, (bone_rect.x + 4, bone_rect.y + 4, bone_rect.width - 8, bone_rect.height - 8) |
| 26 | + ) |
| 27 | + pygame.draw.circle(screen, BONE_LIGHT, (bone_rect.x + 5, bone_rect.y + 5), 8) |
| 28 | + pygame.draw.circle(screen, BONE_LIGHT, (bone_rect.x + bone_rect.width - 5, bone_rect.y + 5), 8) |
| 29 | + pygame.draw.circle(screen, BONE_LIGHT, (bone_rect.x + 5, bone_rect.y + bone_rect.height - 5), 8) |
| 30 | + pygame.draw.circle(screen, BONE_LIGHT, (bone_rect.x + bone_rect.width - 5, bone_rect.y + bone_rect.height - 5), 8) |
| 31 | + |
| 32 | +def draw_dog(): |
| 33 | + """Draws the cartoon dog.""" |
| 34 | + # Body |
| 35 | + pygame.draw.rect(screen, BROWN, dog) |
| 36 | + # Head |
| 37 | + pygame.draw.circle(screen, BROWN, (dog.x + 55, dog.y + 15), 15) |
| 38 | + # Ear |
| 39 | + pygame.draw.polygon(screen, DARK_BROWN, [ |
| 40 | + (dog.x + 60, dog.y), |
| 41 | + (dog.x + 70, dog.y - 10), |
| 42 | + (dog.x + 50, dog.y - 5) |
| 43 | + ]) |
| 44 | + # Tail |
| 45 | + pygame.draw.polygon(screen, DARK_BROWN, [ |
| 46 | + (dog.x, dog.y + 10), |
| 47 | + (dog.x - 10, dog.y), |
| 48 | + (dog.x, dog.y + 20) |
| 49 | + ]) |
| 50 | + # Eye & Nose |
| 51 | + pygame.draw.circle(screen, BLACK, (dog.x + 60, dog.y + 10), 3) |
| 52 | + pygame.draw.circle(screen, BLACK, (dog.x + 68, dog.y + 15), 3) |
| 53 | + |
| 54 | +def draw_game(): |
| 55 | + """Renders the dog, bones, score, and timer.""" |
| 56 | + screen.fill(WHITE) |
| 57 | + draw_dog() |
| 58 | + for bone in bones: # Draw all bones |
| 59 | + draw_bone(bone) |
| 60 | + score_text = font.render(f"Score: {score}", True, BLACK) # Score display |
| 61 | + screen.blit(score_text, (10, 10)) |
| 62 | + if start_time and score == 0: # Timer display |
| 63 | + time_left = max(0, TIME_LIMIT - int(time.time() - start_time)) |
| 64 | + timer_text = font.render(f"Time Left: {time_left}s", True, BLACK) |
| 65 | + screen.blit(timer_text, (WIDTH - 200, 10)) |
| 66 | + pygame.display.flip() |
| 67 | +while True: |
| 68 | + clock.tick(30) |
| 69 | + for event in pygame.event.get(): |
| 70 | + if event.type == pygame.QUIT: |
| 71 | + pygame.quit() |
| 72 | + sys.exit() |
| 73 | + keys = pygame.key.get_pressed() # Dog movement |
| 74 | + if keys[pygame.K_LEFT] and dog.x > 0: |
| 75 | + dog.x -= DOG_SPEED |
| 76 | + if keys[pygame.K_RIGHT] and dog.x < WIDTH - dog.width: |
| 77 | + dog.x += DOG_SPEED |
| 78 | + if score == 0 and start_time is None: # Start timer if score is 0 |
| 79 | + start_time = time.time() |
| 80 | + if score == 0 and time.time() - start_time >= TIME_LIMIT: # Check for timeout |
| 81 | + screen.fill(WHITE) |
| 82 | + msg = font.render("⏰ Time's up! Dog is hungry 😢", True, (255, 0, 0)) |
| 83 | + screen.blit(msg, (WIDTH // 2 - 180, HEIGHT // 2 - 20)) |
| 84 | + pygame.display.flip() |
| 85 | + pygame.time.wait(2500) |
| 86 | + pygame.quit() |
| 87 | + sys.exit() |
| 88 | + if random.randint(1, 30) == 1: # Randomly generate bones |
| 89 | + x_pos = random.randint(0, WIDTH - 40) |
| 90 | + bones.append(pygame.Rect(x_pos, 0, 40, 20)) |
| 91 | + for bone in list(bones): # Move bones and check for collisions |
| 92 | + bone.y += BONE_FALL_SPEED |
| 93 | + if bone.y > HEIGHT: |
| 94 | + bones.remove(bone) |
| 95 | + elif dog.colliderect(bone): |
| 96 | + bones.remove(bone) |
| 97 | + score += 1 |
| 98 | + start_time = None # stop timer once score > 0 |
| 99 | + draw_game() |
0 commit comments