Friday, February 16, 2024

Defiance

 We've reached the final touches on Codemy's Aspen game. The player

either clicks to leave the game, or presses enter for a reset.


                                                             



I  cannot tell a lie: I had to set the speed to 1 or 2 for the vilains in order to

show a win for myself. Just too fast!! I've reset to (1, 5) before closing...


import pygame
import random

# Pygame Setup Stuff
pygame.init()
WINDOW_WIDTH = 900
WINDOW_HEIGHT = 600
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption('Codemy.com - Aspen Classes')
clock = pygame.time.Clock()
running = True

dt = 0
player_pos = pygame.Vector2(screen.get_width() / 2, screen.get_height() / 2)


# Define a Game Class
class Game():
def __init__(self, aspen_group, food_group):
self.aspen_group = aspen_group
self.food_group = food_group
self.score = 0
self.lives = 5
# Define Fonts
self.small_font = pygame.font.SysFont("impact", 24)
self.big_font = pygame.font.SysFont("impact", 60)

# Define our food images
self.blue_food = pygame.image.load("images/En2.png")
self.red_food = pygame.image.load("images/Enemy1.png")

# Add food to our food group
# Food Type: 0=blue, 1=red
self.food_group.add(Food((random.randint(0, 800)), (random.randint(100, 200)), self.red_food, 1))
for i in range(7):
self.food_group.add(Food(i * 100, 200, self.blue_food, 0))

# Define our sounds
self.score_sound = pygame.mixer.Sound('sounds/dog.mp3')
self.die_sound = pygame.mixer.Sound('sounds/aww.mp3')
self.game_over_sound = pygame.mixer.Sound('sounds/game_over.mp3')

def update(self):
self.check_collisions()
self.draw()

keys = pygame.key.get_pressed()
if keys[pygame.K_p]:
self.pause_game()

def draw(self):
pygame.draw.rect(screen, "#003660", (0, 100, WINDOW_WIDTH, WINDOW_HEIGHT - 200), 4)

# Text
title_text = self.big_font.render("FEED ASPEN!", True, "#003660")
title_rect = title_text.get_rect()
title_rect.centerx = WINDOW_WIDTH / 2
title_rect.top = 5

win_text = self.big_font.render("YOU WIN!", True, "red")
win_rect = win_text.get_rect()
win_rect.centerx = WINDOW_WIDTH / 2
win_rect.centery = WINDOW_HEIGHT / 2 - 100

lose_text = self.big_font.render("YOU LOSE!", True, "red")
lose_rect = lose_text.get_rect()
lose_rect.centerx = WINDOW_WIDTH / 2
lose_rect.centery = WINDOW_HEIGHT / 2 - 100

restart_text = self.big_font.render("Press Enter To Play Again", True, "red")
restart_rect = restart_text.get_rect()
restart_rect.centerx = WINDOW_WIDTH / 2
restart_rect.centery = WINDOW_HEIGHT / 2

score_text = self.small_font.render("Score: " + str(self.score), True, "#003660")
score_rect = score_text.get_rect()
score_rect.topleft = (5, 5)

lives_text = self.small_font.render("Lives: " + str(self.lives), True, "#003660")
lives_rect = lives_text.get_rect()
lives_rect.topright = (WINDOW_WIDTH - 5, 5)

# Blit The Text
screen.blit(title_text, title_rect)
screen.blit(score_text, score_rect)
screen.blit(lives_text, lives_rect)

if self.score == 7:
# Add Game over Text
screen.blit(win_text, win_rect)
screen.blit(restart_text, restart_rect)
# Restart Game
self.game_over()

if self.lives == 0:
# Add Game over Text
screen.blit(lose_text, lose_rect)
screen.blit(restart_text, restart_rect)
# Remove Any remaining Food
self.food_group.remove(food_group)
# Restart Game
self.game_over()

def game_over(self):
self.aspen_group.reset()
# Check For Restart Enter
keys = pygame.key.get_pressed()
if keys[pygame.K_RETURN]:
# Reset numbers
self.score = 0
self.lives = 5
# Add new food to the screen
self.food_group.add(Food((random.randint(0, 800)), (random.randint(100, 200)), self.red_food, 1))
for i in range(7):
self.food_group.add(Food(i * 100, 200, self.blue_food, 0))

def pause_game(self):
global running

is_paused = True
# Create pause loop
while is_paused:
# Account For Hitting Enter to unPause
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
is_paused = False
# Account for clicking the X to quit
if event.type == pygame.QUIT:
is_paused = False
running = False
# pygame.quit()

def check_collisions(self):
caught_food = pygame.sprite.spritecollideany(self.aspen_group, self.food_group)
if caught_food:
# Check type of food, red/blue
if caught_food.type == 0:
# Blue
# Play Die Sound
self.die_sound.play()
# Lose a life
self.lives -= 1
# Move Aspen Back Under Box
self.aspen_group.reset()
# Play Game Over Sound
if self.lives == 0:
# Play sound
self.game_over_sound.play()
else:

# Play Score sound
self.score_sound.play()
caught_food.remove(self.food_group)
# Increase the score
self.score += 1

# Logic to remove blue and add red
if len(self.food_group) > 0:
# Randomly Remove Blue Food From Sprites in Food Group
random.choice(self.food_group.sprites()).kill()

if len(self.food_group) >= 1:
# Add a new red food
self.food_group.add(
Food((random.randint(0, 800)), (random.randint(100, 200)), self.red_food, 1))
else:
self.aspen_group.reset()
self.game_over_sound.play()


# Define an Aspen Class
class Aspen(pygame.sprite.Sprite):
def __init__(self, x, y):
super().__init__()
# Define our image
self.image = pygame.image.load("images/PlayerS.png")
# Get Rect
self.rect = self.image.get_rect()
# Position the image
self.rect.topleft = (x, y)
# Move the image
self.velocity = 5

# Add food group to aspen class
# self.food_group = food_group

def update(self):
self.move()

# self.check_collisions()

def move(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and self.rect.x >= 10:
self.rect.x -= self.velocity
if keys[pygame.K_RIGHT] and self.rect.x <= WINDOW_WIDTH - 95:
self.rect.x += self.velocity
if keys[pygame.K_UP] and self.rect.y >= 110:
self.rect.y -= self.velocity
if keys[pygame.K_DOWN] and self.rect.y <= WINDOW_HEIGHT - 95:
self.rect.y += self.velocity

# Reset Aspen back below the box
def reset(self):
self.rect.topleft = (200, 510)


# def check_collisions(self):
# if pygame.sprite.spritecollide(self, self.food_group, True):
# print(len(self.food_group))


# Define an Food Class
class Food(pygame.sprite.Sprite):
def __init__(self, x, y, image, food_type):
super().__init__()
# Define our image
self.image = image
# Get Rect
self.rect = self.image.get_rect()
# Position the image
self.rect.topleft = (x, y)
# Move the image
self.velocity = random.randint(1, 5)

# Food Type: 0=blue, 1=red
self.type = food_type

# Create Random Motion
self.dx = random.choice([-1, 1])
self.dy = random.choice([-1, 1])

def update(self):
# self.rect.y += self.velocity
self.rect.x += self.dx * self.velocity
self.rect.y += self.dy * self.velocity

# Keep from leaving the screen
if self.rect.left <= 0 or self.rect.right >= WINDOW_WIDTH:
self.dx = -1 * self.dx
if self.rect.top <= 100 or self.rect.bottom >= 500:
self.dy = -1 * self.dy


# Create an food group
food_group = pygame.sprite.Group()

# Create 5 aspens
# for i in range(8):
# food = Food(i*100, 200)
# food_group.add(food)

# Create aspen group
aspen_group = pygame.sprite.Group()
# Create and position aspen
aspen = Aspen(200, 510)
# Add aspen to the group
aspen_group.add(aspen)

# Create Game Object
our_game = Game(aspen, food_group)

while running:
# poll for events
# pygame.QUIT event means that the user clicked the X to close the window
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# Pick the screen color
screen.fill("silver")

# Blit (copy) screen object at a given coordinates
# screen.blit(aspen, aspen_rect)

# Draw and Move Food and Aspen sprite
food_group.update()
food_group.draw(screen)

aspen_group.update()
aspen_group.draw(screen)

# Update Game Instance
our_game.update()

# flip the display to output our work to the screen
pygame.display.flip()

# Set the clock stuff / delta time in seconds since the last frame
# used for framerate independent physics
dt = clock.tick(60) / 1000

pygame.quit()

         

                                                                  *     *     *

Just learned this morning that Russian lawyer-activist Alexei Navalny has died, in Siberia. He 

will be remembered for his defiance. Whatever the rights and wrongs of the situation, he was a very 

good-looking man.


One thing I didn't talk about in my reaction to the Putin interview, was that Stalin himself

had been sentenced to prison in Siberia more than once. And had managed to escape, although

the first time was unsuccesful because of frostbite. He seemed to be able to walk out of the place. 

Something on my list of odd things to unravel...


Clearly A. Navalny was in a different situation. May he rest in peace.


From the internet:


No comments: