Skip to content

Commit bbd6bb4

Browse files
authored
Add files via upload
1 parent 3aa6b1c commit bbd6bb4

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

SPACE_WARS.py

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import pygame
2+
import os
3+
pygame.init()
4+
pygame.font.init()
5+
pygame.mixer.init()
6+
7+
WIDTH, HEIGHT = 900, 500
8+
9+
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
10+
pygame.display.set_caption("First Game!!!")
11+
12+
WHITE = (255, 255, 255)
13+
BLACK = (0, 0, 0)
14+
YELLOW = (255, 255, 0)
15+
RED = (255, 0, 0)
16+
17+
VEL = 5
18+
19+
BULLET_VEL = 7
20+
MAX_BULLETS = 10
21+
22+
BULLET_HIT_SOUND = pygame.mixer.Sound(os.path.join('Assets', 'Grenade+1.mp3'))
23+
BULLET_FIRE_SOUND = pygame.mixer.Sound(os.path.join('Assets', 'Gun+Silencer.mp3'))
24+
25+
YELLOW_HIT = pygame.USEREVENT + 1
26+
RED_HIT = pygame.USEREVENT + 2
27+
28+
FPS = 60
29+
30+
BORDER = pygame.Rect(WIDTH/2 - 5, 0, 10, HEIGHT)
31+
32+
HEALTH_FONT = pygame.font.SysFont('century gothic', 40)
33+
WINNER_FONT = pygame.font.SysFont('century gothic', 40)
34+
35+
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 40
36+
37+
YELLOW_SPACESHIP_IMAGE = pygame.image.load(os.path.join('Assets', 'spaceship_yellow.png'))
38+
YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)
39+
40+
RED_SPACESHIP_IMAGE = pygame.image.load(os.path.join('Assets', 'spaceship_red.png'))
41+
RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270)
42+
43+
SPACE = pygame.transform.scale(pygame.image.load(os.path.join('Assets', 'space.png')), (WIDTH, HEIGHT))
44+
45+
def yellow_handle_movement(keys_pressed, yellow):
46+
if keys_pressed[pygame.K_a] and yellow.x - VEL > 0:
47+
yellow.x -= VEL
48+
if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width - 10 < BORDER.x:
49+
yellow.x += VEL
50+
if keys_pressed[pygame.K_w] and yellow.y - VEL > 0:
51+
yellow.y -= VEL
52+
if keys_pressed[pygame.K_s] and yellow.y + yellow.height + VEL < HEIGHT - 15:
53+
yellow.y += VEL
54+
55+
def red_handle_movement(keys_pressed, red):
56+
if keys_pressed[pygame.K_LEFT] and red.x - VEL > BORDER.x + BORDER.width:
57+
red.x -= VEL
58+
if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < WIDTH + 10:
59+
red.x += VEL
60+
if keys_pressed[pygame.K_UP] and red.y - VEL > 0:
61+
red.y -= VEL
62+
if keys_pressed[pygame.K_DOWN] and red.y + red.height + VEL < HEIGHT - 15:
63+
red.y += VEL
64+
65+
def handle_bullets(yellow_bullets, red_bullets, yellow, red):
66+
for bullet in yellow_bullets:
67+
bullet.x += BULLET_VEL
68+
if red.colliderect(bullet):
69+
pygame.event.post(pygame.event.Event(RED_HIT))
70+
yellow_bullets.remove(bullet)
71+
elif bullet.x > WIDTH:
72+
yellow_bullets.remove(bullet)
73+
74+
for bullet in red_bullets:
75+
bullet.x -= BULLET_VEL
76+
if yellow.colliderect(bullet):
77+
pygame.event.post(pygame.event.Event(YELLOW_HIT))
78+
red_bullets.remove(bullet)
79+
elif bullet.x < 0:
80+
red_bullets.remove(bullet)
81+
82+
83+
84+
def draw_window(yellow, red, yellow_bullets, red_bullets, red_health, yellow_health):
85+
WIN.blit(SPACE, (0, 0))
86+
pygame.draw.rect(WIN, BLACK, BORDER)
87+
88+
WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
89+
WIN.blit(RED_SPACESHIP, (red.x, red.y))
90+
91+
red_health_text = HEALTH_FONT.render("Health: " + str(red_health), 1, WHITE)
92+
yellow_health_text = HEALTH_FONT.render('Health: ' + str(yellow_health), 1, WHITE)
93+
94+
WIN.blit(yellow_health_text, (WIDTH - red_health_text.get_width() - 10,10))
95+
WIN.blit(red_health_text, (10, 10))
96+
97+
for bullet in yellow_bullets:
98+
pygame.draw.rect(WIN, YELLOW, bullet)
99+
for bullet in red_bullets:
100+
pygame.draw.rect(WIN, RED, bullet)
101+
102+
pygame.display.update()
103+
104+
105+
106+
def draw_winner(text):
107+
draw_text = WINNER_FONT.render(text, 1, WHITE)
108+
WIN.blit(draw_text, (WIDTH//2 - draw_text.get_width()//2, HEIGHT//2 - draw_text.get_height()//2))
109+
pygame.display.update()
110+
pygame.time.delay(5000)
111+
112+
113+
114+
def main():
115+
116+
yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
117+
red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
118+
119+
yellow_bullets = []
120+
red_bullets = []
121+
122+
red_health = 3
123+
yellow_health = 3
124+
125+
clock = pygame.time.Clock()
126+
127+
run = True
128+
129+
while run:
130+
131+
clock.tick(FPS)
132+
133+
for event in pygame.event.get():
134+
if event.type == pygame.QUIT:
135+
run = False
136+
pygame.quit()
137+
138+
if event.type == pygame.KEYDOWN:
139+
if event.key == pygame.K_LCTRL and len(yellow_bullets) < MAX_BULLETS:
140+
bullet = pygame.Rect(yellow.x + yellow.width, yellow.y + yellow.height//2 - 2, 10, 5)
141+
yellow_bullets.append(bullet)
142+
BULLET_FIRE_SOUND.play()
143+
144+
if event.key == pygame.K_RCTRL and len(red_bullets) <= MAX_BULLETS:
145+
bullet = pygame.Rect(red.x, red.y + red.height//2 - 2, 10, 5)
146+
red_bullets.append(bullet)
147+
BULLET_FIRE_SOUND.play()
148+
149+
if event.type == RED_HIT:
150+
red_health -= 1
151+
BULLET_HIT_SOUND.play()
152+
if event.type == YELLOW_HIT:
153+
yellow_health -= 1
154+
BULLET_HIT_SOUND.play()
155+
156+
winner_text = ''
157+
if red_health <= 0:
158+
winner_text = 'YELLOW WINS !!!'
159+
160+
if yellow_health <= 0:
161+
winner_text = 'RED WINS !!!'
162+
163+
if winner_text != '':
164+
draw_winner(winner_text)
165+
break
166+
167+
keys_pressed = pygame.key.get_pressed()
168+
yellow_handle_movement(keys_pressed,yellow)
169+
red_handle_movement(keys_pressed, red)
170+
171+
172+
handle_bullets(yellow_bullets, red_bullets, yellow, red)
173+
draw_window(yellow, red, yellow_bullets, red_bullets, yellow_health, red_health)
174+
175+
176+
main()
177+
178+
179+
if __name__ == '__main__':
180+
main()

pr.exe

21.7 MB
Binary file not shown.

0 commit comments

Comments
 (0)