-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEcoli.py
103 lines (80 loc) · 2.59 KB
/
Ecoli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import pygame
import random
'''
import os
current_directory = os.getcwd()
print(current_directory)
directory_path = 'C:\\Users\\Danie\\Documents\\GitHub\\Python\\Pygame'
os.chdir(directory_path)
'''
player = 'C:\\Users\\Danie\\Documents\\GitHub\\Python\\Pygame\\player.png'
enemy = 'C:\\Users\\Danie\\Documents\\GitHub\\Python\\Pygame\\enemy.png'
import pygame
import random
# Initialize Pygame
pygame.init()
# Set the size of the game window
size = (800, 600)
screen = pygame.display.set_mode(size)
# Set the title of the game window
pygame.display.set_caption("E. coli Game")
# Load the player sprite
player_image = pygame.image.load(player)
# Create a rect for the player's position and size
player_rect = player_image.get_rect()
# Set the initial size of the player
player_size = 1
# Set the initial position of the player
player_rect.x = size[0] // 2
player_rect.y = size[1] // 2
# Load the enemy sprite
enemy_image = pygame.image.load(enemy)
# Create a list to store the enemies
enemies_list = []
# Spawn 10 enemies per level at random positions and varying sizes
for i in range(10):
enemy_rect = enemy_image.get_rect()
enemy_rect.x = random.randint(0, size[0] - enemy_rect.width)
enemy_rect.y = random.randint(0, size[1] - enemy_rect.height)
enemy_size = random.randint(10, 50)
enemy = {"rect": enemy_rect, "size": enemy_size}
enemies_list.append(enemy)
# Create a variable to store the player's score
score = 0
# Create a variable to store the game's running status
running = True
# Main game loop
while running:
# Handle events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Move the player based on user input
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player_rect.x -= 5
if keys[pygame.K_RIGHT]:
player_rect.x += 5
if keys[pygame.K_UP]:
player_rect.y -= 5
if keys[pygame.K_DOWN]:
player_rect.y += 5
# Check if the player is colliding with any enemies
for enemy in enemies_list:
if player_rect.colliderect(enemy["rect"]):
# Increase the player's size
player_size += enemy["size"]
# Increase the player's score
score += 1
# Remove the enemy from the list
enemies_list.remove(enemy)
screen.fill((0, 0, 0))
# Draw the player on the screen
screen.blit(player_image, player_rect)
# Draw the enemies on the screen
for enemy in enemies_list:
screen.blit(enemy_image, enemy["rect"])
# Update the screen
pygame.display.flip()
# Exit Pygame
pygame.quit()