Add Star war game

Signed-off-by: Manuel Vergara <manuel@vergaracarmona.es>
This commit is contained in:
Manuel Vergara 2023-06-09 22:23:53 +02:00
parent 75e0a1f679
commit dde6d446a7
17 changed files with 284 additions and 0 deletions

BIN
star_wars/Img/Grenade+1.mp3 Normal file

Binary file not shown.

Binary file not shown.

BIN
star_wars/Img/Han-Solo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 137 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.4 MiB

BIN
star_wars/Img/controls.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 KiB

BIN
star_wars/Img/han-solo.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 916 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 846 KiB

1
star_wars/Img/im.png Normal file
View File

@ -0,0 +1 @@

BIN
star_wars/Img/soldier.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 45 KiB

BIN
star_wars/Img/star_wars.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 554 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 989 KiB

Binary file not shown.

BIN
star_wars/Img/victory.mp3 Normal file

Binary file not shown.

81
star_wars/README.md Normal file
View File

@ -0,0 +1,81 @@
ORIGINAL REPO: https://github.com/pedrollamas/Star_wars_pygame
# 🎮 Star Wars: Space Duel! 🎮
![Hello there](Img/hello_there.gif)
Dive into the Star Wars galaxy and get ready for an epic showdown between two legendary characters: Han Solo and a relentless Stormtrooper. Take control of your character, dodge enemy shots, and showcase your skills with the laser pistol.
## Description
This is an arcade-style game 🕹️ created with ![Pygame](https://img.shields.io/badge/Pygame-%23FFCA1C?style=for-the-badge&logo=pygame&logoColor=white). You can play 1⃣ 🆚 1⃣ with a friend using the same keyboard.
I used Tech with Tim tutorial and used it as a base, here is the link https://www.youtube.com/watch?v=jO6qQDNa2UY
Each player has 10 lives ♥️ and will take on the role of either Han Solo or a Stormtrooper.
My objective with this game was to learn the basics of ![Pygame](https://img.shields.io/badge/Pygame-%23FFCA1C?style=for-the-badge&logo=pygame&logoColor=white), in order to create different mini-games 🧩🎲 in the future for fun.
I hope you enjoy it, and I invite you to collaborate to improve it or create new games if you wish.
## Demonstration
There are two playable characters:
| ![Han Solo](Img/han-solo.gif) | ![Stormtrooper](Img/stormtrooper_dance.gif) |
|:---:|:---:|
| **Han Solo** | **Stormtrooper** |
The quality of the gif is low, you'll find out later on when you download it that it is better 🤙
<p align="left">
<img src="Img/Star_wars_demonstration.gif" alt="GIF del video" width="600" height="400">
</p>
## Instructions
To play the Star Wars game in your local environment, follow these steps:
### Clone the Repository
1. Open a terminal or command prompt on your system.
2. Navigate to the location where you want to clone the repository.
3. Run the following command to clone the repository:
```
git clone https://github.com/pedrollamas/Star_wars_pygame.git
```
### Install Dependencies
1. Make sure you have Python installed on your system.
2. Navigate to the root folder of the cloned repository.
3. Run the following command to install the dependencies:
```
pip install -r requirements.txt
```
### Run the Game
In the same location as the root folder, run the following command to start the game:
```
python star_wars.py
```
Once the command is executed, the game will open in a new window, and you can start playing.
## Controls
![Controls](Img/controls.jpg)
#### For the Stormtrooper:
Move Up: W
Move Down: S
Move Left: A
Move Right: D
Shoot: Caps lock key (Press to shoot)
#### For Han Solo:
Move Up: Up Arrow
Move Down: Down Arrow
Move Left: Left Arrow
Move Right: Right Arrow
Shoot: Spacebar (Press to shoot)
To close the game, simply click the ❌ button in the window. If you want to play again, wait for 4 seconds and the game will automatically restart 🔁
## Contact:
* Feel free to send me an email, generate an issue or pull. My goal is for everyone who likes the idea, to contribute and start developing small fun games.
* 📬 Send me an email at pedrollamaslopez@hotmail.com
* 👨‍💼🤝 Connect with me on Linkedin at https://www.linkedin.com/in/pedrollamaslopez/
🌟**Enjoy the game and may the Force be with you!**🌟

View File

@ -0,0 +1 @@
pygame==2.4.0

201
star_wars/star_wars.py Normal file
View File

@ -0,0 +1,201 @@
# Libraries.
import pygame
import os
pygame.init() # To initialize the game
pygame.font.init() # To load the font library
pygame.mixer.init() # To load the sounds.
# We load the theme music and play while it runs.
pygame.mixer.music.load("Img/theme_music.mp3")
pygame.mixer.music.play(-1)
# We define the width and height of the game.
WIDTH, HEIGHT = 1000,600
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Stormtrooper VS Han Solo")
# We define the colors to have a clear code since it uses RGB.
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)
BLUE = (0, 170, 255)
# We define the rectangle that acts as the border for the characters.
BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT)
# We add the sound variables
BULLET_HIT_SOUND = pygame.mixer.Sound(os.path.join('Img', 'Grenade+1.mp3'))
BULLET_FIRE_SOUND = pygame.mixer.Sound(os.path.join('Img', 'Gun+Silencer.mp3'))
VICTORY_SOUND = pygame.mixer.Sound(os.path.join('Img', 'victory.mp3'))
# We define the font used to show the health.
HEALTH_FONT = pygame.font.SysFont('Fixedsys', 40)
# We define the font used to show the winner.
WINNER_FONT = pygame.font.SysFont('Fixedsys', 100)
# We define this in order for the game to not depend on how fast our system can run it. We define how often we want the game to update.
FPS = 60
# We define the velocity they are moving when they press keys.
VEL = 5
# We define the velocity of the bullets.
BULLET_VEL = 6
# We define the max bullets each player can use.
MAX_BULLETS = 10
SOLDIER_WIDTH, SOLDIER_HEIGHT = 90, 140
SOLO_WIDTH, SOLO_HEIGHT = 150, 120
# We create the event of getting hit by the bullet.
SOLDIER_HIT = pygame.USEREVENT + 1
SOLO_HIT = pygame.USEREVENT + 2
# Import the images for the characters.
SOLDIER_IMAGE = pygame.image.load(os.path.join('Img', 'soldier.png'))
SOLDIER = pygame.transform.scale(SOLDIER_IMAGE, (SOLDIER_WIDTH, SOLDIER_HEIGHT))
SOLO_IMAGE = pygame.image.load(os.path.join('Img', 'Han-Solo.png'))
SOLO = pygame.transform.scale(SOLO_IMAGE, (SOLO_WIDTH, SOLO_HEIGHT))
# We generate the backround because we checked white and the yellow bullets were hard to see.
SPACE = pygame.transform.scale(pygame.image.load(os.path.join('Img', 'star_wars.png')), (WIDTH, HEIGHT))
def draw_window(solo, soldier, solo_bullets, soldier_bullets, solo_health, soldier_health):
WIN.blit(SPACE, (0, 0)) # Fill the background.
pygame.draw.rect(WIN, WHITE, BORDER)
# We define the text and position of the health.
solo_health_text = HEALTH_FONT.render("Han Solo: " + str(solo_health), 1, WHITE, BLACK)
soldier_health_text = HEALTH_FONT.render("Stormtrooper: " + str(soldier_health), 1, WHITE, BLACK)
WIN.blit(solo_health_text, (WIDTH - solo_health_text.get_width() - 10, 10))
WIN.blit(soldier_health_text, (10, 10))
WIN.blit(SOLDIER, (soldier.x, soldier.y))
WIN.blit(SOLO, (solo.x, solo.y))
# We define the rectangle that represents the bullet and color for each character.
for bullet in soldier_bullets:
pygame.draw.rect(WIN, BLUE, bullet)
for bullet in solo_bullets:
pygame.draw.rect(WIN, RED, bullet)
pygame.display.update() # We always need to update pygame to display our changes.
# We create a function to not repeat it every time.
def soldier_handle_movement(keys_pressed, soldier):
if keys_pressed[pygame.K_a] and soldier.x - VEL > 0: # LEFT
soldier.x -= VEL
if keys_pressed[pygame.K_d] and soldier.x + VEL + soldier.width < BORDER.x: # RIGHT
soldier.x += VEL
if keys_pressed[pygame.K_w] and soldier.y - VEL > 0: # UP # We subtract when going up because in pygame the top left corner is (0,0)
soldier.y -= VEL
if keys_pressed[pygame.K_s] and soldier.y + VEL + soldier.height < HEIGHT - 15: # DOWN # We make sure we don't allow it to go of the border with (-15).
soldier.y += VEL
# We do the same with the other character.
def solo_handle_movement(keys_pressed, solo):
if keys_pressed[pygame.K_LEFT] and solo.x - VEL > BORDER.x + BORDER.width: # LEFT
solo.x -= VEL
if keys_pressed[pygame.K_RIGHT] and solo.x + VEL + solo.width < WIDTH: # RIGHT
solo.x += VEL
if keys_pressed[pygame.K_UP] and solo.y - VEL > 0: # UP
solo.y -= VEL
if keys_pressed[pygame.K_DOWN] and solo.y + VEL + solo.height < HEIGHT - 15: # DOWN
solo.y += VEL
# We define the function for the bullets movement.
def handle_bullets(soldier_bullets, solo_bullets, soldier, solo):
for bullet in soldier_bullets:
bullet.x += BULLET_VEL
if solo.colliderect(bullet):
pygame.event.post(pygame.event.Event(SOLO_HIT))
soldier_bullets.remove(bullet)
elif bullet.x > WIDTH:
soldier_bullets.remove(bullet)
for bullet in solo_bullets:
bullet.x -= BULLET_VEL
if soldier.colliderect(bullet):
pygame.event.post(pygame.event.Event(SOLDIER_HIT))
solo_bullets.remove(bullet)
elif bullet.x < 0:
solo_bullets.remove(bullet)
# We define the function to show the winner and restart the game.
def draw_winner(text):
draw_text = WINNER_FONT.render(text, 1, RED, BLACK)
WIN.blit(draw_text, (WIDTH/2 - draw_text.get_width()/2, HEIGHT/2 - draw_text.get_height()/2))
pygame.display.update()
pygame.time.delay(4000)
# Our main function for the game.
def main():
solo = pygame.Rect(700, 300, SOLO_WIDTH, SOLO_HEIGHT) # we define the spaceships position
soldier = pygame.Rect(100, 300, SOLDIER_WIDTH, SOLDIER_HEIGHT)
solo_bullets = []
soldier_bullets = []
# We define the health of players.
solo_health = 10
soldier_health = 10
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS) # Ensures our game runs maximum at 60 FPS(defined at the begining)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_CAPSLOCK and len(soldier_bullets) < MAX_BULLETS: # We add the condition to not shoot bulllets if its over the limit we defined.
bullet = pygame.Rect(soldier.x + soldier.width, soldier.y + soldier.height//2 - 2, 10, 5) # We define where the bullets come from which is at the edge of the character and the middle.
soldier_bullets.append(bullet)
BULLET_FIRE_SOUND.play()
if event.key == pygame.K_SPACE and len(solo_bullets) < MAX_BULLETS:
bullet = pygame.Rect(solo.x, solo.y + solo.height//2 - 2, 10, 5)
solo_bullets.append(bullet)
BULLET_FIRE_SOUND.play()
if event.type == SOLO_HIT:
solo_health -= 1
BULLET_HIT_SOUND.play()
if event.type == SOLDIER_HIT:
soldier_health -= 1
BULLET_HIT_SOUND.play()
# We define the text depending on the character that wins.
winner_text = ""
if solo_health <= 0:
winner_text = "Stormtrooper Wins!"
if soldier_health <= 0:
winner_text = "Han Solo Wins!"
if winner_text != "":
pygame.mixer.music.stop() # We stop the theme music.
draw_winner(winner_text)
VICTORY_SOUND.play() # Reproduce the sound of victory.
pygame.time.delay(4000) # We add delay so it gives it time to sound and restart.
VICTORY_SOUND.stop()
break
keys_pressed = pygame.key.get_pressed() # to know the keys are currently being pressed.
soldier_handle_movement(keys_pressed, soldier)
solo_handle_movement(keys_pressed, solo)
handle_bullets(soldier_bullets, solo_bullets, soldier, solo)
draw_window(solo, soldier, solo_bullets, soldier_bullets, solo_health, soldier_health)
main()
# We define this in order for the game to run when we execute this file.
if __name__ == "__main__":
main()