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 aab966c

Browse files
Space Shooter Game Using Python PyGame
Space Shooter Game Using Python PyGame
1 parent 07b78d9 commit aab966c

File tree

8 files changed

+184
-0
lines changed

8 files changed

+184
-0
lines changed

‎Assets/Grenade+1.mp3

9.38 KB
Binary file not shown.

‎Assets/Gun+Silencer.mp3

5.63 KB
Binary file not shown.

‎Assets/space.png

4.69 MB
Loading[フレーム]

‎Assets/spaceship_red.png

2.11 KB
Loading[フレーム]

‎Assets/spaceship_yellow.png

2.13 KB
Loading[フレーム]

‎README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# PygameForBeginners
2+
A simple 2D python game designed to teach you the pygame module.

‎main.py

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

‎requirements.txt

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

0 commit comments

Comments
(0)

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