Python Games
SKILLS USED: Python | Game Logic Development | Problem-Solving | Creativity | Graphics Programming |
SPACE WARS
Space Wars is an exciting and engaging two-player game that I developed using Python and the Pygame library. The game begins with an attractive start screen that displays the game's title and a 'Start' button to commence the action. Upon clicking the button, the players are presented with a thrilling challenge, where they control a spaceship each.The game's primary objective is to shoot down the opponent's spaceship while avoiding getting hit. The game features an immersive background music and sound effects that further enhance the gameplay experience. Players can move their spaceships using the arrow keys, which adds to the game's fluidity and allows for seamless navigation of the space terrain.
One of the standout features of the game is the ability to fire bullets that the spaceships can use to attack the enemy. The Pygame library is leveraged to create the game's graphics, giving it a stunning appearance that complements the game's dynamic nature. Additionally, the library is used to manage the game loop, ensuring smooth gameplay and seamless transitions from one level to another.
Overall, the Space Wars game is an excellent example of the potential of Python programming language and Pygame library in creating engaging and fun two-player games that can keep you entertained for hours.
Python Code used
As I was developing the game, I spent a significant amount of time perfecting the start screen of the game. I began by defining the width and height of the window and setting the caption to "SPACE WARS!". Then, I loaded the background image and scaled it to fit the window size. I chose a white and black color scheme and defined two fonts: one for the title and one for the start button. I created a rectangle to represent the start button and rendered the text for the button. Finally, I wrote a function to draw the start screen, including the background image, title, and start button, and called this function within the main game loop. The result was a sleek and visually appealing start screen that entices players to click the start button and begin the game.
Code Used
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("SPACE WARS!")
# Define colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
# Load background image
bg_image = pygame.image.load('Assets/homebackground.png').convert()
bg_image = pygame.transform.scale(bg_image, (WIDTH, HEIGHT))
# Define fonts
TITLE_FONT = pygame.font.SysFont('comicsans', 70, bold=True)
BUTTON_FONT = pygame.font.SysFont('comicsans', 40)
# Define buttons
START_BUTTON_WIDTH, START_BUTTON_HEIGHT = 200, 75
START_BUTTON_X, START_BUTTON_Y = WIDTH/2 - START_BUTTON_WIDTH/2, HEIGHT/2 - START_BUTTON_HEIGHT/2
START_BUTTON = pygame.Rect(START_BUTTON_X, START_BUTTON_Y, START_BUTTON_WIDTH, START_BUTTON_HEIGHT)
START_BUTTON_TEXT = BUTTON_FONT.render('Start', True, WHITE)
def draw_start_screen():
# Draw background image
WIN.blit(bg_image, (0, 0))
# Draw title
title_text = TITLE_FONT.render('SPACE WARS!', True, WHITE)
title_x, title_y = WIDTH/2 - title_text.get_width()/2, 100
WIN.blit(title_text, (title_x, title_y))
# Draw start button
pygame.draw.rect(WIN, WHITE, START_BUTTON, 2)
start_text_x, start_text_y = START_BUTTON_X + START_BUTTON_WIDTH/2 -
START_BUTTON_TEXT.get_width()/2, START_BUTTON_Y + START_BUTTON_HEIGHT/2 -
START_BUTTON_TEXT.get_height()/2
WIN.blit(START_BUTTON_TEXT, (start_text_x, start_text_y))
pygame.display.update()
def main():
# Run the start screen
draw_start_screen()
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
# Check if the start button was clicked
mouse_pos = pygame.mouse.get_pos()
if START_BUTTON.collidepoint(mouse_pos):
run = False
if __name__ == '__main__':
main()
As I worked on developing the start page for my space wars game, I made sure to incorporate the key elements of the game into the design. I coded the space ships and bullets into the page to give players a taste of what they can expect in the actual game. The space ships were designed to look sleek and futuristic, while the bullets were given a fiery trail to make them look more dangerous. By integrating these features into the start page, I hoped to give players a sense of what they're in for and entice them to play the game.
Code Used
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("SPACE WARS")
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT)
BULLET_HIT_SOUND = pygame.mixer.Sound('Assets/Grenade+1.mp3')
BULLET_FIRE_SOUND = pygame.mixer.Sound('Assets/Gun+Silencer.mp3')
HEALTH_FONT = pygame.font.SysFont('comicsans', 40)
WINNER_FONT = pygame.font.SysFont('comicsans', 100)
FPS = 60
VEL = 5
BULLET_VEL = 7
MAX_BULLETS = 3
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 40
YELLOW_HIT = pygame.USEREVENT + 1
RED_HIT = pygame.USEREVENT + 2
YELLOW_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_yellow.png'))
YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)
RED_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_red.png'))
RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270)
SPACE = pygame.transform.scale(pygame.image.load(
os.path.join('Assets', 'rmb4.png')), (WIDTH, HEIGHT))
def draw_window(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health):
WIN.blit(SPACE, (0, 0))
pygame.draw.rect(WIN, BLACK, BORDER)
red_health_text = HEALTH_FONT.render(
"Health: " + str(red_health), 1, WHITE)
yellow_health_text = HEALTH_FONT.render(
"Health: " + str(yellow_health), 1, WHITE)
WIN.blit(red_health_text, (WIDTH - red_health_text.get_width() - 10, 10))
WIN.blit(yellow_health_text, (10, 10))
WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
WIN.blit(RED_SPACESHIP, (red.x, red.y))
for bullet in red_bullets:
pygame.draw.rect(WIN, RED, bullet)
for bullet in yellow_bullets:
pygame.draw.rect(WIN, YELLOW, bullet)
pygame.display.update()
def yellow_handle_movement(keys_pressed, yellow):
if keys_pressed[pygame.K_a] and yellow.x - VEL > 0: # LEFT
yellow.x -= VEL
if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width < BORDER.x: # RIGHT
yellow.x += VEL
if keys_pressed[pygame.K_w] and yellow.y - VEL > 0: # UP
yellow.y -= VEL
if keys_pressed[pygame.K_s] and yellow.y + VEL + yellow.height < HEIGHT - 15: # DOWN
yellow.y += VEL
def red_handle_movement(keys_pressed, red):
if keys_pressed[pygame.K_LEFT] and red.x - VEL > BORDER.x + BORDER.width: # LEFT
red.x -= VEL
if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < WIDTH: # RIGHT
red.x += VEL
if keys_pressed[pygame.K_UP] and red.y - VEL > 0: # UP
red.y -= VEL
if keys_pressed[pygame.K_DOWN] and red.y + VEL + red.height < HEIGHT - 15: # DOWN
red.y += VEL
def handle_bullets(yellow_bullets, red_bullets, yellow, red):
for bullet in yellow_bullets:
bullet.x += BULLET_VEL
if red.colliderect(bullet):
pygame.event.post(pygame.event.Event(RED_HIT))
yellow_bullets.remove(bullet)
elif bullet.x > WIDTH:
yellow_bullets.remove(bullet)
for bullet in red_bullets:
bullet.x -= BULLET_VEL
if yellow.colliderect(bullet):
pygame.event.post(pygame.event.Event(YELLOW_HIT))
red_bullets.remove(bullet)
elif bullet.x < 0:
red_bullets.remove(bullet)
def draw_winner(text):
draw_text = WINNER_FONT.render(text, 1, WHITE)
WIN.blit(draw_text, (WIDTH/2 - draw_text.get_width() /
2, HEIGHT/2 - draw_text.get_height()/2))
pygame.display.update()
pygame.time.delay(5000)
def main():
red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
red_bullets = []
yellow_bullets = []
red_health = 10
yellow_health = 10
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q and len(yellow_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
yellow.x + yellow.width, yellow.y + yellow.height//2 - 2, 10, 5)
yellow_bullets.append(bullet)
BULLET_FIRE_SOUND.play()
if event.key == pygame.K_m and len(red_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
red.x, red.y + red.height//2 - 2, 10, 5)
red_bullets.append(bullet)
BULLET_FIRE_SOUND.play()
if event.type == RED_HIT:
red_health -= 1
BULLET_HIT_SOUND.play()
if event.type == YELLOW_HIT:
yellow_health -= 1
BULLET_HIT_SOUND.play()
winner_text = ""
if red_health <= 0:
winner_text = "Yellow Wins!"
if yellow_health <= 0:
winner_text = "Red Wins!"
if winner_text != "":
draw_winner(winner_text)
break
keys_pressed = pygame.key.get_pressed()
yellow_handle_movement(keys_pressed, yellow)
red_handle_movement(keys_pressed, red)
handle_bullets(yellow_bullets, red_bullets, yellow, red)
draw_window(red, yellow, red_bullets, yellow_bullets,
red_health, yellow_health)
main()
if __name__ == "__main__":
main()