Skip to content

Commit ab7c06b

Browse files
Merge pull request #13 from AaronMathew11/master
Added spacewars
2 parents 0fba7a9 + deb7763 commit ab7c06b

File tree

8 files changed

+380
-0
lines changed

8 files changed

+380
-0
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Loading
Loading
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
# THE DUALITY SHOOTER GAME --> first pygame proj.
2+
3+
import os # to access the files
4+
import pygame
5+
pygame.font.init() # To use the fonts lib
6+
pygame.mixer.init() # To be able to use music
7+
8+
9+
WIDTH,HEIGHT = 900, 500 # vars for window width and height
10+
WIN = pygame.display.set_mode((WIDTH,HEIGHT)) # set up the window
11+
pygame.display.set_caption("First Game") # set the title bar caption
12+
13+
# defining the colors
14+
WHITE = (255,255,255)
15+
BLACK = (0,0,0)
16+
RED = (255,0,0)
17+
YELLOW = (255,255,0)
18+
19+
BORDER = pygame.Rect(WIDTH//2-5,0,10,HEIGHT) # the rect for the center line
20+
21+
# Moosick
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+
BACKROUND_MUSIC = pygame.mixer.Sound(os.path.join('Assets','bensound-epic.mp3'))
25+
26+
# Setting up fonts
27+
HEALTH_FONT = pygame.font.SysFont('comicsans',40)
28+
WINNER_FONT = pygame.font.SysFont('comicsans',100)
29+
30+
FPS = 60
31+
VEL=5
32+
BULLET_VEL = 7
33+
MAX_BULLETS = 3
34+
SPACESHIP_WIDTH,SPACESHIP_HEIGHT = 55,40
35+
36+
# creating user events
37+
YELLOW_HIT = pygame.USEREVENT + 1
38+
RED_HIT = pygame.USEREVENT + 2
39+
40+
# loading the images
41+
YELLOW_S = pygame.image.load(os.path.join('Assets','spaceship_yellow.png'))
42+
YELLOW_S = pygame.transform.rotate(pygame.transform.scale(YELLOW_S,(SPACESHIP_WIDTH,SPACESHIP_HEIGHT)), 90) # rotating by 90 and rescaling
43+
44+
45+
RED_S = pygame.image.load(os.path.join('Assets','spaceship_red.png'))
46+
RED_S = pygame.transform.rotate(pygame.transform.scale(RED_S,(SPACESHIP_WIDTH,SPACESHIP_HEIGHT)), 270)
47+
48+
SPACE = pygame.image.load(os.path.join('Assets','space.png'))
49+
SPACE = pygame.transform.scale(SPACE,(WIDTH,HEIGHT))
50+
51+
# The drawing func.
52+
def draw_window(red,yellow,red_bullets,yellow_bullets,red_health,yellow_health):
53+
WIN.blit(SPACE,(0,0)) # drawing the bg
54+
pygame.draw.rect(WIN,BLACK,BORDER) # drawing the deviding line
55+
56+
red_health_text = HEALTH_FONT.render("Health: "+ str(red_health),1,WHITE)
57+
yellow_health_text = HEALTH_FONT.render("Health: "+ str(yellow_health),1,WHITE)
58+
WIN.blit(red_health_text,(WIDTH-red_health_text.get_width()-10,10))
59+
WIN.blit(yellow_health_text,(10,10))
60+
61+
# drawing the two spaceships
62+
WIN.blit(YELLOW_S, (yellow.x,yellow.y))
63+
WIN.blit(RED_S, (red.x,red.y))
64+
65+
# drawing the bullets
66+
for bullet in red_bullets:
67+
pygame.draw.rect(WIN,RED,bullet)
68+
69+
for bullet in yellow_bullets:
70+
pygame.draw.rect(WIN,YELLOW,bullet)
71+
72+
pygame.display.update() # IMP.... display needs to be updated
73+
74+
75+
def yellow_handle_movement(keys_pressed,yellow):
76+
if keys_pressed[pygame.K_a] and yellow.x-VEL>0: #LEFT
77+
yellow.x-=VEL
78+
if keys_pressed[pygame.K_d] and yellow.x+VEL+yellow.width<BORDER.x: #RIGHT
79+
yellow.x+=VEL
80+
if keys_pressed[pygame.K_w] and yellow.y-VEL>0: #UP
81+
yellow.y-=VEL
82+
if keys_pressed[pygame.K_s] and yellow.y+VEL+yellow.height<HEIGHT-15: #DOWN
83+
yellow.y+=VEL
84+
85+
def red_handle_movement(keys_pressed,red):
86+
if keys_pressed[pygame.K_LEFT] and red.x-VEL>BORDER.width+ BORDER.x: #LEFT
87+
red.x-=VEL
88+
if keys_pressed[pygame.K_RIGHT] and red.x+VEL+red.width<WIDTH: #RIGHT
89+
red.x+=VEL
90+
if keys_pressed[pygame.K_UP] and red.y-VEL>0: #UP
91+
red.y-=VEL
92+
if keys_pressed[pygame.K_DOWN] and red.y+VEL+red.height<HEIGHT-15: #DOWN
93+
red.y+=VEL
94+
95+
# Func to handle bullet movements and collisions
96+
def handle_bullets(yellow_bullets,red_bullets,yellow,red):
97+
for bullet in yellow_bullets:
98+
bullet.x+=BULLET_VEL
99+
if red.colliderect(bullet):
100+
pygame.event.post(pygame.event.Event(RED_HIT))
101+
yellow_bullets.remove(bullet)
102+
if(bullet.x>WIDTH):
103+
yellow_bullets.remove(bullet)
104+
elif bullet.x<0:
105+
red_bullets.remove(bullet)
106+
107+
for bullet in red_bullets:
108+
bullet.x-=BULLET_VEL
109+
if yellow.colliderect(bullet):
110+
pygame.event.post(pygame.event.Event(YELLOW_HIT))
111+
red_bullets.remove(bullet)
112+
if(bullet.x>WIDTH):
113+
yellow_bullets.remove(bullet)
114+
elif bullet.x<0:
115+
red_bullets.remove(bullet)
116+
117+
# Func to get winner text
118+
def draw_winner(text):
119+
draw_text = WINNER_FONT.render(text,1,WHITE)
120+
WIN.blit(draw_text,(WIDTH//2 - draw_text.get_width()//2,HEIGHT//2 - draw_text.get_height()//2))
121+
pygame.display.update()
122+
pygame.time.delay(5000)
123+
124+
125+
# CODE DRIVING FUNCTION
126+
def main():
127+
red = pygame.Rect(700,300,SPACESHIP_WIDTH,SPACESHIP_HEIGHT)
128+
yellow = pygame.Rect(100,300,SPACESHIP_WIDTH,SPACESHIP_HEIGHT)
129+
130+
red_bullets= []
131+
yellow_bullets =[]
132+
133+
red_health = 10
134+
yellow_health =10
135+
136+
winner_text=""
137+
138+
clock = pygame.time.Clock()
139+
run = True
140+
while run:
141+
clock.tick(FPS)
142+
for event in pygame.event.get():
143+
144+
#pygame.mixer.music.set_volume(0.1)
145+
#ACKROUND_MUSIC.play()
146+
147+
if event.type == pygame.QUIT:
148+
run= False
149+
pygame.quit()
150+
151+
if event.type == pygame.KEYDOWN:
152+
if event.key == pygame.K_LCTRL and len(yellow_bullets)<MAX_BULLETS:
153+
bullet = pygame.Rect(yellow.x + yellow.width, yellow.y + yellow.height//2 - 2,10,5)
154+
yellow_bullets.append(bullet)
155+
BULLET_FIRE_SOUND.play()
156+
157+
if event.key == pygame.K_RCTRL and len(red_bullets)<MAX_BULLETS:
158+
bullet = pygame.Rect(red.x , red.y + red.height//2 - 2,10,5)
159+
red_bullets.append(bullet)
160+
BULLET_FIRE_SOUND.play()
161+
162+
if event.type == RED_HIT:
163+
red_health-=1
164+
BULLET_HIT_SOUND.play()
165+
166+
if event.type == YELLOW_HIT:
167+
yellow_health-=1
168+
BULLET_HIT_SOUND.play()
169+
170+
if red_health<=0:
171+
winner_text="Yellow Wins!"
172+
173+
if yellow_health<=0:
174+
winner_text="Red Wins!"
175+
176+
if winner_text!= "":
177+
draw_winner(winner_text)
178+
break
179+
180+
keys_pressed= pygame.key.get_pressed()
181+
yellow_handle_movement(keys_pressed,yellow)
182+
red_handle_movement(keys_pressed,red)
183+
handle_bullets(yellow_bullets,red_bullets,yellow,red)
184+
draw_window(red,yellow,red_bullets,yellow_bullets,red_health,yellow_health)
185+
186+
main() # Rerun on completion
187+
188+
189+
if __name__=="__main__":
190+
main()
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
# THE DUALITY SHOOTER GAME --> first pygame proj.
2+
3+
import os # to access the files
4+
import pygame
5+
pygame.font.init() # To use the fonts lib
6+
pygame.mixer.init() # To be able to use music
7+
8+
9+
WIDTH,HEIGHT = 900, 500 # vars for window width and height
10+
WIN = pygame.display.set_mode((WIDTH,HEIGHT)) # set up the window
11+
pygame.display.set_caption("First Game") # set the title bar caption
12+
13+
# defining the colors
14+
WHITE = (255,255,255)
15+
BLACK = (0,0,0)
16+
RED = (255,0,0)
17+
YELLOW = (255,255,0)
18+
19+
BORDER = pygame.Rect(WIDTH//2-5,0,10,HEIGHT) # the rect for the center line
20+
21+
# Moosick
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+
BACKROUND_MUSIC = pygame.mixer.Sound(os.path.join('Assets','bensound-epic.mp3'))
25+
26+
# Setting up fonts
27+
HEALTH_FONT = pygame.font.SysFont('comicsans',40)
28+
WINNER_FONT = pygame.font.SysFont('comicsans',100)
29+
30+
FPS = 60
31+
VEL=5
32+
BULLET_VEL = 7
33+
MAX_BULLETS = 3
34+
SPACESHIP_WIDTH,SPACESHIP_HEIGHT = 55,40
35+
36+
# creating user events
37+
YELLOW_HIT = pygame.USEREVENT + 1
38+
RED_HIT = pygame.USEREVENT + 2
39+
40+
# loading the images
41+
YELLOW_S = pygame.image.load(os.path.join('Assets','spaceship_yellow.png'))
42+
YELLOW_S = pygame.transform.rotate(pygame.transform.scale(YELLOW_S,(SPACESHIP_WIDTH,SPACESHIP_HEIGHT)), 90) # rotating by 90 and rescaling
43+
44+
45+
RED_S = pygame.image.load(os.path.join('Assets','spaceship_red.png'))
46+
RED_S = pygame.transform.rotate(pygame.transform.scale(RED_S,(SPACESHIP_WIDTH,SPACESHIP_HEIGHT)), 270)
47+
48+
SPACE = pygame.image.load(os.path.join('Assets','space.png'))
49+
SPACE = pygame.transform.scale(SPACE,(WIDTH,HEIGHT))
50+
51+
# The drawing func.
52+
def draw_window(red,yellow,red_bullets,yellow_bullets,red_health,yellow_health):
53+
WIN.blit(SPACE,(0,0)) # drawing the bg
54+
pygame.draw.rect(WIN,BLACK,BORDER) # drawing the deviding line
55+
56+
red_health_text = HEALTH_FONT.render("Health: "+ str(red_health),1,WHITE)
57+
yellow_health_text = HEALTH_FONT.render("Health: "+ str(yellow_health),1,WHITE)
58+
WIN.blit(red_health_text,(WIDTH-red_health_text.get_width()-10,10))
59+
WIN.blit(yellow_health_text,(10,10))
60+
61+
# drawing the two spaceships
62+
WIN.blit(YELLOW_S, (yellow.x,yellow.y))
63+
WIN.blit(RED_S, (red.x,red.y))
64+
65+
# drawing the bullets
66+
for bullet in red_bullets:
67+
pygame.draw.rect(WIN,RED,bullet)
68+
69+
for bullet in yellow_bullets:
70+
pygame.draw.rect(WIN,YELLOW,bullet)
71+
72+
pygame.display.update() # IMP.... display needs to be updated
73+
74+
75+
def yellow_handle_movement(keys_pressed,yellow):
76+
if keys_pressed[pygame.K_a] and yellow.x-VEL>0: #LEFT
77+
yellow.x-=VEL
78+
if keys_pressed[pygame.K_d] and yellow.x+VEL+yellow.width<BORDER.x: #RIGHT
79+
yellow.x+=VEL
80+
if keys_pressed[pygame.K_w] and yellow.y-VEL>0: #UP
81+
yellow.y-=VEL
82+
if keys_pressed[pygame.K_s] and yellow.y+VEL+yellow.height<HEIGHT-15: #DOWN
83+
yellow.y+=VEL
84+
85+
def red_handle_movement(keys_pressed,red):
86+
if keys_pressed[pygame.K_LEFT] and red.x-VEL>BORDER.width+ BORDER.x: #LEFT
87+
red.x-=VEL
88+
if keys_pressed[pygame.K_RIGHT] and red.x+VEL+red.width<WIDTH: #RIGHT
89+
red.x+=VEL
90+
if keys_pressed[pygame.K_UP] and red.y-VEL>0: #UP
91+
red.y-=VEL
92+
if keys_pressed[pygame.K_DOWN] and red.y+VEL+red.height<HEIGHT-15: #DOWN
93+
red.y+=VEL
94+
95+
# Func to handle bullet movements and collisions
96+
def handle_bullets(yellow_bullets,red_bullets,yellow,red):
97+
for bullet in yellow_bullets:
98+
bullet.x+=BULLET_VEL
99+
if red.colliderect(bullet):
100+
pygame.event.post(pygame.event.Event(RED_HIT))
101+
yellow_bullets.remove(bullet)
102+
if(bullet.x>WIDTH):
103+
yellow_bullets.remove(bullet)
104+
elif bullet.x<0:
105+
red_bullets.remove(bullet)
106+
107+
for bullet in red_bullets:
108+
bullet.x-=BULLET_VEL
109+
if yellow.colliderect(bullet):
110+
pygame.event.post(pygame.event.Event(YELLOW_HIT))
111+
red_bullets.remove(bullet)
112+
if(bullet.x>WIDTH):
113+
yellow_bullets.remove(bullet)
114+
elif bullet.x<0:
115+
red_bullets.remove(bullet)
116+
117+
# Func to get winner text
118+
def draw_winner(text):
119+
draw_text = WINNER_FONT.render(text,1,WHITE)
120+
WIN.blit(draw_text,(WIDTH//2 - draw_text.get_width()//2,HEIGHT//2 - draw_text.get_height()//2))
121+
pygame.display.update()
122+
pygame.time.delay(5000)
123+
124+
125+
# CODE DRIVING FUNCTION
126+
def main():
127+
red = pygame.Rect(700,300,SPACESHIP_WIDTH,SPACESHIP_HEIGHT)
128+
yellow = pygame.Rect(100,300,SPACESHIP_WIDTH,SPACESHIP_HEIGHT)
129+
130+
red_bullets= []
131+
yellow_bullets =[]
132+
133+
red_health = 10
134+
yellow_health =10
135+
136+
winner_text=""
137+
138+
clock = pygame.time.Clock()
139+
run = True
140+
while run:
141+
clock.tick(FPS)
142+
for event in pygame.event.get():
143+
144+
#pygame.mixer.music.set_volume(0.1)
145+
#ACKROUND_MUSIC.play()
146+
147+
if event.type == pygame.QUIT:
148+
run= False
149+
pygame.quit()
150+
151+
if event.type == pygame.KEYDOWN:
152+
if event.key == pygame.K_LCTRL and len(yellow_bullets)<MAX_BULLETS:
153+
bullet = pygame.Rect(yellow.x + yellow.width, yellow.y + yellow.height//2 - 2,10,5)
154+
yellow_bullets.append(bullet)
155+
BULLET_FIRE_SOUND.play()
156+
157+
if event.key == pygame.K_RCTRL and len(red_bullets)<MAX_BULLETS:
158+
bullet = pygame.Rect(red.x , red.y + red.height//2 - 2,10,5)
159+
red_bullets.append(bullet)
160+
BULLET_FIRE_SOUND.play()
161+
162+
if event.type == RED_HIT:
163+
red_health-=1
164+
BULLET_HIT_SOUND.play()
165+
166+
if event.type == YELLOW_HIT:
167+
yellow_health-=1
168+
BULLET_HIT_SOUND.play()
169+
170+
if red_health<=0:
171+
winner_text="Yellow Wins!"
172+
173+
if yellow_health<=0:
174+
winner_text="Red Wins!"
175+
176+
if winner_text!= "":
177+
draw_winner(winner_text)
178+
break
179+
180+
keys_pressed= pygame.key.get_pressed()
181+
yellow_handle_movement(keys_pressed,yellow)
182+
red_handle_movement(keys_pressed,red)
183+
handle_bullets(yellow_bullets,red_bullets,yellow,red)
184+
draw_window(red,yellow,red_bullets,yellow_bullets,red_health,yellow_health)
185+
186+
main() # Rerun on completion
187+
188+
189+
if __name__=="__main__":
190+
main()

0 commit comments

Comments
 (0)