Untitled
unknown
plain_text
2 years ago
4.4 kB
15
Indexable
import pygame
import sys
import random
import time
WHITE=(255, 255, 255)
BLACK = (0, 0, 0)
RED=(255, 0, 0)
YELLOW=(255, 255, 0)
BLUE=(0,0,255)
SURVIVOR_SPEED = 0.5
ZOMBIE_SPEED = 0.5
BOUNDARY_WIDTH = 1000
BOUNDARY_HEIGHT = 400
pygame.init()
game_window = pygame.display.set_mode((1000,400))
class Survivor(pygame.sprite.Sprite):
def __init__(self):
super().__init__() # Neccessary line when making a Sprite class
self.image = pygame.image.load('Survivor.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 += SURVIVOR_SPEED
if keys[pygame.K_LEFT]: #and self.rect.x >= 0:
print("here")
self.rect.x -= SURVIVOR_SPEED
if keys[pygame.K_UP]: #and self.rect.y >= 0:
self.rect.y -= SURVIVOR_SPEED
if keys[pygame.K_DOWN] and self.rect.y <= BOUNDARY_HEIGHT:
self.rect.y += SURVIVOR_SPEED
def get_pos_x(self):
return self.rect.x
def get_pos_y(self):
return self.rect.y
class Coin(pygame.sprite.Sprite):
def __init__(self):
super().__init__() # Neccessary line when making a Sprite class
self.image = pygame.image.load('Coin.png')
x_pos = random.randint(2, 30) * 10 #Set X coordinate (random number)
y_pos = random.randint(2, 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, survivor):
if pygame.sprite.collide_rect(self, survivor):
x_pos = random.randint(2, 80) * 10 #Set X coordinate (random number)
y_pos = random.randint(2, 30) * 10 #Set Y coordinate (random number)
self.rect = pygame.Rect(x_pos, y_pos, self.image.get_width(), self.image.get_height())
print("collision")
return True
return False
class Zombie(pygame.sprite.Sprite):
def __init__(self):
self.image = pygame.image.load('Zombie.png')
x_pos = 900 #Set X coordinate (random number)
y_pos = 300 #Set Y coordinate (random number)
self.rect = pygame.Rect(x_pos, y_pos, self.image.get_width(), self.image.get_height())
def move(self, survivor):
#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 += ZOMBIE_SPEED
#if keys[pygame.K_LEFT] and self.rect.x >= 0:
# self.rect.x -= ZOMBIE_SPEED
#if keys[pygame.K_UP] and self.rect.y >= 0:
##self.rect.y -= ZOMBIE_SPEED
#if keys[pygame.K_DOWN] and self.rect.y <= BOUNDARY_HEIGHT:
#self.rect.y += ZOMBIE_SPEED
distance_x = survivor.get_pos_x() - self.rect.x
distance_y = survivor.get_pos_y() - self.rect.y
distance = (distance_x ** 2 + distance_y ** 2) ** 0.5
if distance != 0:
self.rect.x += ZOMBIE_SPEED * distance_x / distance
self.rect.y += ZOMBIE_SPEED * distance_y / distance
def detect_collision(self, survivor):
self.rect = pygame.Rect(x_pos, y_pos, self.image.get_width(), self.image.get_height())
survivor=Survivor()
coin=Coin()
zombie=Zombie()
while True:
game_window.fill(WHITE)
game_window.blit(survivor.image, survivor.rect) # Draw the first sprite
game_window.blit(coin.image, coin.rect) # Draw the second sprite
game_window.blit(zombie.image, zombie.rect) # Draw the second sprite
# 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
survivor.move()
zombie.move(survivor)
# Event handler for alice-poison collision
if pygame.sprite.collide_rect(survivor, zombie): # Alice touches the Poison
game_window.fill(BLACK)
over_font = pygame.font.SysFont('times new roman', 50)
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