Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 460c03d

Browse files
Added files for Spaceship Game GUI (avinashkranjan#965)
1 parent e5d156e commit 460c03d

File tree

7 files changed

+319
-0
lines changed

7 files changed

+319
-0
lines changed

‎Spaceship_Game/Assets/Red_Spaceship.png

2.11 KB
Loading[フレーム]
2.13 KB
Loading[フレーム]

‎Spaceship_Game/Assets/space.jpg

111 KB
Loading[フレーム]

‎Spaceship_Game/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# SPACESHIP GAME
2+
3+
- The python script makes use of Pygame, a popular GUI module, to develop an interactive multiplayer Spaceship Game.
4+
- The 2 players compete to aim bullets at each other and the first player to lose their health, loses.
5+
6+
## Requirements:
7+
8+
All the packages essential for running the script can be installed as follows:
9+
10+
``` sh
11+
$ pip install -r requirements.txt
12+
```
13+
14+
## Working:
15+
16+
![GIF](https://media.giphy.com/media/gRjmH1VPUBZrfYV0RH/giphy.gif)
17+

‎Spaceship_Game/main.py

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
import pygame
2+
import utility as util
3+
4+
# for displaying health we need text
5+
pygame.font.init()
6+
7+
# width, height = 900, 500
8+
WINDOW = pygame.display.set_mode((util.width, util.height))
9+
pygame.display.set_caption("Spaceship War Game")
10+
11+
# Creating user event so that we can came to know if the bullet collides, diff number indicates diff events
12+
YELLOW_HIT = pygame.USEREVENT + 1
13+
RED_HIT = pygame.USEREVENT + 2
14+
15+
16+
def drawWindow(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health,
17+
YELLOW_SPACESHIP, RED_SPACESHIP, SPACE, BORDER):
18+
"""
19+
This functions gives displays the graphics of the Game which includes
20+
both the spaceship, background space image, bullets, and health of players
21+
with 60 Frames per second.
22+
:param red: Bounding rectangle of Red Spaceship
23+
:param yellow: Bounding rectangle of Yellow Spaceship
24+
:param red_bullets: Bullets fired by red spaceship
25+
:param yellow_bullets: Bullets fired by Yellow spaceship
26+
:param red_health: Health of Red Player
27+
:param yellow_health: Health of Yellow Player
28+
:param YELLOW_SPACESHIP: Red Spaceship Image
29+
:param RED_SPACESHIP: Yellow Spaceship Image
30+
:param SPACE: Space Background Image
31+
:param BORDER: Center border
32+
"""
33+
# ORDER IN WHICH DRAW THINGS MATTER REMEMBER
34+
WINDOW.blit(SPACE, (0, 0))
35+
36+
# Adding border created above
37+
pygame.draw.rect(WINDOW, (255, 255, 255), BORDER)
38+
39+
# to indicate health
40+
HEALTH_FONT = pygame.font.SysFont('comicsans', 40)
41+
42+
# Displaying Health by font
43+
red_health_text = HEALTH_FONT.render("Health: " + str(red_health), True, (255, 255, 255))
44+
yellow_health_text = HEALTH_FONT.render("Health: " + str(yellow_health), True, (255, 255, 255))
45+
WINDOW.blit(red_health_text, (util.width - red_health_text.get_width() - 10, 10))
46+
WINDOW.blit(yellow_health_text, (10, 10))
47+
48+
# to load surfaces we use blit
49+
WINDOW.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
50+
51+
WINDOW.blit(RED_SPACESHIP, (red.x, red.y))
52+
53+
# Drawing bullets
54+
for bullet in red_bullets:
55+
pygame.draw.rect(WINDOW, (255, 0, 0), bullet)
56+
57+
for bullet in yellow_bullets:
58+
pygame.draw.rect(WINDOW, (255, 255, 0), bullet)
59+
60+
pygame.display.update()
61+
62+
63+
def handle_bullets(yellow_bullets, red_bullets, yellow, red):
64+
"""
65+
This function moves the bullet forward with a specific Bullet velocity
66+
and at a time only 3 bullets can be fired by the user.
67+
It also checks weather the bullet has hit the spaceship by using collidirect
68+
function, so that we can decrease the health of that player.
69+
:param yellow_bullets: Bullets fired by red spaceship
70+
:param red_bullets: Bullets fired by red spaceship
71+
:param yellow: Bounding rectangle of Yellow Spaceship
72+
:param red: Bounding rectangle of Red Spaceship
73+
"""
74+
# Bullet velocity
75+
BULLET_VEL = 7
76+
77+
# To check weather the bullet hit red or yellow spaceship or they fly through the skin
78+
for bullet in yellow_bullets:
79+
bullet.x += BULLET_VEL
80+
81+
# colliddirect only works if both are rectangle
82+
if red.colliderect(bullet):
83+
# Now we are going to post a event then check in the main function for the event
84+
# it will indicate us that the bullet hit the spaceship
85+
pygame.event.post(pygame.event.Event(RED_HIT))
86+
yellow_bullets.remove(bullet)
87+
elif bullet.x > util.width:
88+
yellow_bullets.remove(bullet)
89+
90+
for bullet in red_bullets:
91+
bullet.x -= BULLET_VEL
92+
93+
# colliddirect only works if both are rectangle
94+
if yellow.colliderect(bullet):
95+
# Now we are going to post a event then check in the main function for the event
96+
# it will indicate us that the bullet hit the spaceship
97+
pygame.event.post(pygame.event.Event(YELLOW_HIT))
98+
red_bullets.remove(bullet)
99+
elif bullet.x < 0:
100+
red_bullets.remove(bullet)
101+
102+
103+
def main():
104+
"""
105+
Main logic of the game, This function makes everything work together.
106+
1. Load the assets
107+
2. Reads input from the keyboard
108+
3. Making bullets fire
109+
4. Handling bullet movements
110+
5. Displaying everything together on the screen.
111+
6. Showing the winner
112+
7. Again restarting the game after 5 sec.
113+
"""
114+
# Loading Assets
115+
YELLOW_SPACESHIP, RED_SPACESHIP, SPACE, BORDER = util.load_assests()
116+
117+
# Making two rectangles so that we can control where our spaceship are moving
118+
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = (50, 40)
119+
red = pygame.Rect(700, 250, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
120+
yellow = pygame.Rect(100, 250, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
121+
122+
# To making our game refresh at a constant interval
123+
clock = pygame.time.Clock()
124+
125+
# To storing our bullet location in pixels so that we can move it
126+
yellow_bullets = []
127+
red_bullets = []
128+
129+
# Healths of our spaceships
130+
red_health = 10
131+
yellow_health = 10
132+
133+
run = True
134+
135+
while run:
136+
# Capped frame rate so it remains consistent on diff computers
137+
clock.tick(60)
138+
139+
for event in pygame.event.get():
140+
if event.type == pygame.QUIT:
141+
run = False
142+
pygame.quit()
143+
144+
# checking if key pressed for firing bullet
145+
if event.type == pygame.KEYDOWN:
146+
147+
# maximum amount of bullets a spaceship can shoot at a time
148+
MAX_BULLETS = 3
149+
# CHECKING if we press LCTRL and we have 3 bullets at a time on a screen
150+
if event.key == pygame.K_LCTRL and len(yellow_bullets) < MAX_BULLETS:
151+
# 10, 5 width, height of bullet and others are location
152+
bullet = pygame.Rect(yellow.x + yellow.width, yellow.y + yellow.height // 2 - 2, 10, 5)
153+
yellow_bullets.append(bullet)
154+
155+
if event.key == pygame.K_RCTRL and len(red_bullets) < MAX_BULLETS:
156+
bullet = pygame.Rect(red.x, red.y + red.height // 2 - 2, 10, 5)
157+
red_bullets.append(bullet)
158+
159+
# If bullets hit red spaceship then decrease health
160+
if event.type == RED_HIT:
161+
red_health -= 1
162+
# If bullets hit yellow spaceship then decrease health
163+
if event.type == YELLOW_HIT:
164+
yellow_health -= 1
165+
166+
winner_text = ""
167+
if red_health <= 0:
168+
winner_text = "Yellow Wins!!"
169+
if yellow_health <= 0:
170+
winner_text = "Red Wins!!"
171+
if winner_text != "":
172+
util.winner(winner_text, WINDOW)
173+
break
174+
175+
# Checking which keys are pressed while the game is running it also checks if the
176+
# keys are pressed and remain down
177+
keys_pressed = pygame.key.get_pressed()
178+
# Spaceship velocity
179+
VELOCITY = 5
180+
# Function that handle key movements of yellow and red spaceship and bullets
181+
util.yellow_handle_movement(keys_pressed, yellow, VELOCITY, BORDER)
182+
util.red_handle_movement(keys_pressed, red, VELOCITY, BORDER)
183+
184+
handle_bullets(yellow_bullets, red_bullets, yellow, red)
185+
186+
# Displaying everything on the screen.
187+
drawWindow(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health,
188+
YELLOW_SPACESHIP, RED_SPACESHIP, SPACE, BORDER)
189+
190+
main()
191+
192+
193+
if __name__ == "__main__":
194+
main()

‎Spaceship_Game/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pygame==2.0.1

‎Spaceship_Game/utility.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import pygame
2+
import os
3+
4+
width, height = 900, 500
5+
6+
7+
def load_assests():
8+
"""
9+
Loading the spaceship images and manipulating them.
10+
and creating a centre border.
11+
:return: YELLOW_SPACESHIP, RED_SPACESHIP, SPACE, BORDER
12+
"""
13+
# Loading spaceship images into our file known as surfaces as we use this above background
14+
spaceshipImageYellow = pygame.image.load(os.path.join("Assets", "Yellow_Spaceship.png"))
15+
spaceshipImageRed = pygame.image.load(os.path.join("Assets", "Red_Spaceship.png"))
16+
SPACE = pygame.image.load(os.path.join("Assets", "space.jpg"))
17+
18+
# SCALING down the images
19+
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = (50, 40)
20+
YELLOW_SPACESHIP = pygame.transform.scale(spaceshipImageYellow, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT))
21+
RED_SPACESHIP = pygame.transform.scale(spaceshipImageRed, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT))
22+
SPACE = pygame.transform.scale(SPACE, (900, 500))
23+
24+
# ROTATING the images
25+
YELLOW_SPACESHIP = pygame.transform.rotate(YELLOW_SPACESHIP, 90)
26+
RED_SPACESHIP = pygame.transform.rotate(RED_SPACESHIP, -90)
27+
28+
# BORDER in the middle of the window
29+
# starting coordinates then width and height of the border
30+
BORDER = pygame.Rect(width / 2 - 5, 0, 10, height)
31+
32+
return YELLOW_SPACESHIP, RED_SPACESHIP, SPACE, BORDER
33+
34+
35+
def yellow_handle_movement(keys_pressed, yellow, VELOCITY, BORDER):
36+
"""
37+
This function takes the Bounding box of spaceship and with the keys pressed down on
38+
keyboard it moves the spaceship with a velocity. And by keeping in mind that it should not
39+
cross the border and should not go out of the frame.
40+
+------------------------------+
41+
| KEYS | ACTION |
42+
+----------------+-------------+
43+
| A | LEFT |
44+
| D | RIGHT |
45+
| W | UP |
46+
| S | DOWN |
47+
| Left CTRL | FIRE | in handle_bullets function
48+
+----------------+-------------+
49+
:param keys_pressed: Gives which keys are pressed on keyboard.
50+
:param yellow: Bounding rectangle of yellow Spaceship.
51+
:param VELOCITY: Spaceship Velocity.
52+
:param BORDER: Center Border.
53+
"""
54+
# and checking that it remains in the screen and dont cross the border
55+
if keys_pressed[pygame.K_a] and yellow.x - VELOCITY > 0: # LEFT
56+
yellow.x -= VELOCITY
57+
elif keys_pressed[pygame.K_d] and yellow.x + VELOCITY + yellow.width < BORDER.x: # RIGHT
58+
yellow.x += VELOCITY
59+
elif keys_pressed[pygame.K_w] and yellow.y - VELOCITY > 0: # UP
60+
yellow.y -= VELOCITY
61+
elif keys_pressed[pygame.K_s] and yellow.y + VELOCITY + yellow.height < height - 15: # DOWN
62+
yellow.y += VELOCITY
63+
64+
65+
def red_handle_movement(keys_pressed, red, VELOCITY, BORDER):
66+
"""
67+
This function takes the Bounding box of spaceship and with the keys pressed down on
68+
keyboard it moves the spaceship with a velocity. And by keeping in mind that it should not
69+
cross the border and should not go out of the frame.
70+
+------------------------------+
71+
| KEYS | ACTION |
72+
+----------------+-------------+
73+
| Left Arrow | LEFT |
74+
| Right Arrow | RIGHT |
75+
| Up Arrow | UP |
76+
| Down Arrow | DOWN |
77+
| Right CTRL | FIRE | in handle_bullets function
78+
+----------------+-------------+
79+
:param keys_pressed: Gives which keys are pressed on keyboard.
80+
:param red: Bounding rectangle of Red Spaceship.
81+
:param VELOCITY: Spaceship Velocity.
82+
:param BORDER: Center Border.
83+
"""
84+
if keys_pressed[pygame.K_LEFT] and red.x - VELOCITY > BORDER.x + BORDER.width + 8: # LEFT
85+
red.x -= VELOCITY
86+
elif keys_pressed[pygame.K_RIGHT] and red.x + VELOCITY + red.width < width: # RIGHT
87+
red.x += VELOCITY
88+
elif keys_pressed[pygame.K_UP] and red.y - VELOCITY > 0: # UP
89+
red.y -= VELOCITY
90+
elif keys_pressed[pygame.K_DOWN] and red.y + VELOCITY + red.height < height - 15: # DOWN
91+
red.y += VELOCITY
92+
93+
94+
def winner(text, WIN):
95+
"""
96+
# Displaying the winner on screen.
97+
:param text: Player name
98+
:param WIN: GUI Window to display text on
99+
"""
100+
# Font
101+
FONT = pygame.font.SysFont('comicsans', 100)
102+
# Displaying the winner font on the screen.
103+
draw_text = FONT.render(text, 1, (255, 255, 255))
104+
WIN.blit(draw_text, (width / 2 - draw_text.get_width() / 2, height / 2 - draw_text.get_height() / 2))
105+
# Updating the display
106+
pygame.display.update()
107+
pygame.time.delay(5000)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /