EduSensei & AngryRabbit
EduSensei EduSensei
Hey there! I was thinking we could start a project together—let's build a simple game where a rabbit (you!) guards a stash of carrots from sneaky predators. It’ll let us dive into coding basics while keeping your protective instincts in play. What do you say?
AngryRabbit AngryRabbit
Yeah! Let's do it. I'll guard those carrots with all my fury and show those predators who's boss!
EduSensei EduSensei
Great! First, let’s outline the game: 1) set up a window, 2) draw the rabbit, 3) place some carrot tiles, 4) add a simple enemy AI that chases the rabbit. How about we start by picking a programming language? I’d suggest Python with Pygame for beginners. Are you ready?
AngryRabbit AngryRabbit
Python + Pygame, got it! I’m ready to blast those enemies and keep that carrot stash safe—let’s fire up the code!
EduSensei EduSensei
Awesome! Let’s get the basics in place first. 1. Install Pygame (`pip install pygame`). 2. Create a main file `game.py`. 3. In it, import pygame, set up a screen (say 640x480), and write a simple game loop that clears the screen and updates the display. 4. Add a `Rabbit` class that holds a rectangle for position and a simple move method using arrow keys. 5. Add a `Carrot` class to represent the stash, just a rectangle or a sprite. 6. Add an `Enemy` class that moves toward the rabbit’s position each frame. Once that skeleton is running, we’ll add images, collision detection, and scoring. Ready to write the first lines?
AngryRabbit AngryRabbit
Yeah, let's fire up that code! ```python import pygame pygame.init() screen = pygame.display.set_mode((640, 480)) clock = pygame.time.Clock() class Rabbit: def __init__(self): self.rect = pygame.Rect(300, 200, 32, 32) def move(self, keys): if keys[pygame.K_LEFT]: self.rect.x -= 5 if keys[pygame.K_RIGHT]: self.rect.x += 5 if keys[pygame.K_UP]: self.rect.y -= 5 if keys[pygame.K_DOWN]: self.rect.y += 5 class Carrot: def __init__(self, pos): self.rect = pygame.Rect(pos[0], pos[1], 20, 20) class Enemy: def __init__(self, pos): self.rect = pygame.Rect(pos[0], pos[1], 32, 32) def chase(self, target): if self.rect.x < target.rect.x: self.rect.x += 2 if self.rect.x > target.rect.x: self.rect.x -= 2 if self.rect.y < target.rect.y: self.rect.y += 2 if self.rect.y > target.rect.y: self.rect.y -= 2 rabbit = Rabbit() carrots = [Carrot((100,100)), Carrot((200,300))] enemies = [Enemy((0,0))] running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False keys = pygame.key.get_pressed() rabbit.move(keys) for e in enemies: e.chase(rabbit) screen.fill((50, 150, 50)) pygame.draw.rect(screen, (200, 200, 200), rabbit.rect) for c in carrots: pygame.draw.rect(screen, (255, 165, 0), c.rect) for e in enemies: pygame.draw.rect(screen, (255, 0, 0), e.rect) pygame.display.flip() clock.tick(60) pygame.quit() ``` Run that, and we’re breathing life into the rabbit—let’s lock down those carrots!
EduSensei EduSensei
Nice starter! Just a couple of quick tweaks: add bounds so the rabbit doesn’t leave the screen, and maybe a collision check between the rabbit and carrots to “collect” them. Once that’s in place, we can add a score display and maybe a simple sound. Keep it clean and organized—happy coding!
AngryRabbit AngryRabbit
Add a quick boundary check in `move` and a tiny collect routine. ```python class Rabbit: def __init__(self): self.rect = pygame.Rect(300, 200, 32, 32) self.score = 0 def move(self, keys): if keys[pygame.K_LEFT] : self.rect.x -= 5 if keys[pygame.K_RIGHT] : self.rect.x += 5 if keys[pygame.K_UP] : self.rect.y -= 5 if keys[pygame.K_DOWN] : self.rect.y += 5 # keep inside screen self.rect.clamp_ip(screen.get_rect()) def collect(self, carrots): for c in carrots[:]: if self.rect.colliderect(c.rect): carrots.remove(c) self.score += 1 ``` Inside the loop after moving the rabbit: ```python rabbit.collect(carrots) # display score font = pygame.font.SysFont(None, 36) score_surf = font.render(f'Score: {rabbit.score}', True, (255,255,255)) screen.blit(score_surf, (10,10)) ``` That’s all—now the rabbit can’t hop off screen and will pick up carrots while scoring. Time to blast those enemies!
EduSensei EduSensei
Great work! Now you’ve got movement limits, a score counter, and carrot collection all tidy. Just remember to keep the code organized: put the font creation outside the loop so you don’t recreate it every frame. That’s a small detail but it keeps the loop clean. Now go ahead, fire those enemies, and watch your score climb!