Untitled
unknown
plain_text
2 years ago
4.0 kB
8
Indexable
import pygame
import sys
import random
import time
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255, 0, 0)
FOX_SPEED = 1
BOUNDARY_WIDTH = 600
BOUNDARY_HEIGHT = 400
pygame.init() # Initialize the background setting
game_window = pygame.display.set_mode((800, 600)) #Create a display window
score = 0 # Initialize the variable for player's score
class Fox(pygame.sprite.Sprite):
def __init__(self):
super().__init__() # Neccessary line when making a Sprite class
self.image = pygame.image.load('fox.png') # Load alice image
self.rect = pygame.Rect(100, 100, self.image.get_width(), self.image.get_height())
def move(self):
keys = pygame.key.get_pressed() # Get the list of pressed keys
if keys[pygame.K_RIGHT] and self.rect.x <= BOUNDARY_WIDTH: # If the player presses the right key and Alice is not out of bounds
self.rect.x += FOX_SPEED
if keys[pygame.K_LEFT] and self.rect.x >= 0:
self.rect.x -= FOX_SPEED
if keys[pygame.K_UP] and self.rect.y >= 0:
self.rect.y -= FOX_SPEED
if keys[pygame.K_DOWN] and self.rect.y <= BOUNDARY_HEIGHT:
self.rect.y += FOX_SPEED
class Fruit(pygame.sprite.Sprite):
def __init__(self, fruit_type):
super().__init__() # Neccessary line when making a Sprite class
self.fruit_type=fruit_type
if self.fruit_type=="apple":
self.image = pygame.image.load('apple.png') # Load star image
self.value=10
elif self.fruit_type=="orange":
self.image = pygame.image.load('orange.png') # Load star image
self.value=20
x_pos = random.randint(1, 30) * 10 #Set X coordinate (random number)
y_pos = random.randint(1, 20) * 10 #Set Y coordinate (random number)
self.rect = pygame.Rect(x_pos, y_pos, self.image.get_width(), self.image.get_height())
def detect_collision(self, fox):
global score
if pygame.sprite.collide_rect(self, fox):
score+=self.value
x_pos = random.randint(1, 30) * 10 #Set X coordinate (random number)
y_pos = random.randint(1, 20) * 10 #Set Y coordinate (random number)
return True
return False
class Trap(pygame.sprite.Sprite):
def __init__(self):
self.image= pygame.image.load('trap.png')
x_pos = random.randint(1, 30) * 10 #Set X coordinate (random number)
y_pos = random.randint(1, 20) * 10 #Set Y coordinate (random number)
self.rect = pygame.Rect(x_pos, y_pos, self.image.get_width(), self.image.get_height())
fox=Fox()
fruit1=Fruit("apple")
fruit2=Fruit("orange")
trap=Trap()
while True:
game_window.fill(WHITE)
game_window.blit(fox.image, fox.rect) # Draw the first sprite
game_window.blit(fruit1.image, fruit1.rect) # Draw the second sprite
game_window.blit(fruit2.image, fruit2.rect) # Draw the second sprite
game_window.blit(trap.image, trap.rect) #Draw the third sprite
score_font = pygame.font.SysFont('times new roman', 20) # Set the font and size for the text
score_surface = score_font.render('Score : ' + str(score), True, BLACK)
game_window.blit(score_surface, (200, 0)) # Display the text
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() # Quit Pygame
sys.exit() # Exit the program
# Event handler for movement
fox.move()
# Event handler for alice-star collision
fruit1.detect_collision(fox)
fruit2.detect_collision(fox)
# Event handler for alice-poison collision
if pygame.sprite.collide_rect(fox, trap): # Alice touches the Poison
game_window.fill(BLACK)
over_font = pygame.font.SysFont('times new roman', 50)
game_over_surface = over_font.render('Your Score is : ' + str(score), True, RED)
game_window.blit(game_over_surface, (150, 300)) #D isplay the text for final score
pygame.display.update()
time.sleep(2) # Wait for 2 seconds
pygame.quit() # Quit Pygame
sys.exit() # Exit the program
pygame.display.update() # Display the most recently drawn screen
Editor is loading...
Leave a Comment