forked from y8tireu/My-Python-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
126 lines (105 loc) · 3.88 KB
/
game.py
File metadata and controls
126 lines (105 loc) · 3.88 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import pygame
import random
# Initialize pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("2 Player Fighting Game")
# Define Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
# Clock and FPS
clock = pygame.time.Clock()
FPS = 60
# Define Player Class
class Player:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.width = 50
self.height = 60
self.color = color
self.velocity = 5
self.health = 100
def draw(self, screen):
pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
def attack(self, other_player):
if self.x < other_player.x + other_player.width and \
self.x + self.width > other_player.x and \
self.y < other_player.y + other_player.height and \
self.y + self.height > other_player.y:
other_player.health -= 5
# Create Players
player1 = Player(100, 300, RED)
player2 = Player(600, 300, BLUE)
# Define AI behavior
def ai_movement(player, opponent):
if player.x < opponent.x:
player.x += player.velocity
elif player.x > opponent.x:
player.x -= player.velocity
if player.y < opponent.y:
player.y += player.velocity
elif player.y > opponent.y:
player.y -= player.velocity
# Main Game Loop
def main():
run_game = True
two_player_mode = True # Change this to False to play against AI
while run_game:
clock.tick(FPS)
screen.fill(WHITE)
# Event Handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
run_game = False
# Player Movements (Local Play or Player vs AI)
keys = pygame.key.get_pressed()
# Player 1 Controls
if keys[pygame.K_a] and player1.x - player1.velocity > 0: # Left
player1.x -= player1.velocity
if keys[pygame.K_d] and player1.x + player1.width + player1.velocity < WIDTH: # Right
player1.x += player1.velocity
if keys[pygame.K_w] and player1.y - player1.velocity > 0: # Up
player1.y -= player1.velocity
if keys[pygame.K_s] and player1.y + player1.height + player1.velocity < HEIGHT: # Down
player1.y += player1.velocity
if keys[pygame.K_SPACE]: # Attack
player1.attack(player2)
# Player 2 Controls or AI
if two_player_mode:
if keys[pygame.K_LEFT] and player2.x - player2.velocity > 0: # Left
player2.x -= player2.velocity
if keys[pygame.K_RIGHT] and player2.x + player2.width + player2.velocity < WIDTH: # Right
player2.x += player2.velocity
if keys[pygame.K_UP] and player2.y - player2.velocity > 0: # Up
player2.y -= player2.velocity
if keys[pygame.K_DOWN] and player2.y + player2.height + player2.velocity < HEIGHT: # Down
player2.y += player2.velocity
if keys[pygame.K_RETURN]: # Attack
player2.attack(player1)
else:
ai_movement(player2, player1)
if random.randint(0, 60) == 0: # AI Random Attack
player2.attack(player1)
# Draw Players
player1.draw(screen)
player2.draw(screen)
# Display Health
pygame.draw.rect(screen, RED, (50, 50, player1.health * 2, 20))
pygame.draw.rect(screen, BLUE, (WIDTH - 250, 50, player2.health * 2, 20))
# Check for Winner
if player1.health <= 0:
print("Player 2 Wins!" if two_player_mode else "AI Wins!")
run_game = False
if player2.health <= 0:
print("Player 1 Wins!")
run_game = False
# Update Display
pygame.display.update()
pygame.quit()
# Run the Game
if __name__ == "__main__":
main()