0% found this document useful (0 votes)
23 views7 pages

Shooter

This document outlines a simple shooting game implemented using Pygame, featuring a player-controlled spaceship that shoots down enemy UFOs. It details the initialization of game resources, the creation of sprite classes for the player, enemies, and bullets, and the main game loop that handles events, updates, and rendering. The game includes win/lose conditions based on the score and missed enemies, along with background music and sound effects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views7 pages

Shooter

This document outlines a simple shooting game implemented using Pygame, featuring a player-controlled spaceship that shoots down enemy UFOs. It details the initialization of game resources, the creation of sprite classes for the player, enemies, and bullets, and the main game loop that handles events, updates, and rendering. The game includes win/lose conditions based on the score and missed enemies, along with background music and sound effects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

from pygame import *

from random import randint

# Loading font functions separately


font.init()
font1 = font.Font(None, 80)
win = font1.render('YOU WIN!', True, (255, 255, 255))
lose = font1.render('YOU LOSE!', True, (180, 0, 0))

font2 = font.Font(None, 36)

# Background music
mixer.init()
mixer.music.load('space.ogg')
mixer.music.play()
fire_sound = mixer.Sound('fire.ogg')

# We need the following images:


img_back = "galaxy.jpg" # Game background
img_bullet = "bullet.png" # Bullet
img_hero = "rocket.png" # Hero
img_enemy = "ufo.png" # Enemy

score = 0 # Ships destroyed


goal = 10 # How many ships need to be shot down to win
lost = 0 # Ships missed
max_lost = 3 # Lose if you miss that many

# Parent class for other sprites


class GameSprite(sprite.Sprite):
# Class constructor
def __init__(self, player_image, player_x, player_y, size_x, size_y,
player_speed):
# Call for the class (Sprite) constructor:
sprite.Sprite.__init__(self)

# Every sprite must store the image property


self.image = transform.scale(image.load(player_image), (size_x, size_y))
self.speed = player_speed

# Every sprite must have the rect property that represents the rectangle it is
fitted in
self.rect = self.image.get_rect()
self.rect.x = player_x
self.rect.y = player_y

# Method drawing the character on the window


def reset(self):
window.blit(self.image, (self.rect.x, self.rect.y))
# Main player class
class Player(GameSprite):
# Method to control the sprite with arrow keys
def update(self):
keys = key.get_pressed()
if keys[K_LEFT] and self.rect.x > 5:
self.rect.x -= self.speed
if keys[K_RIGHT] and self.rect.x < win_width - 80:
self.rect.x += self.speed

# Method to "shoot" (use the player position to create a bullet there)


def fire(self):
bullet = Bullet(img_bullet, self.rect.centerx, self.rect.top, 15, 20, -15)
bullets.add(bullet)

# Enemy sprite class


class Enemy(GameSprite):
# Enemy movement
def update(self):
self.rect.y += self.speed
global lost
# Disappears upon reaching the screen edge
if self.rect.y > win_height:
self.rect.x = randint(80, win_width - 80)
self.rect.y = 0
lost = lost + 1

# Bullet sprite class


class Bullet(GameSprite):
# Bullet movement
def update(self):
self.rect.y += self.speed
# Disappears upon reaching the screen edge
if self.rect.y < 0:
self.kill()

# Create a small window


win_width = 700
win_height = 500
display.set_caption("Shooter")
window = display.set_mode((win_width, win_height))
background = transform.scale(image.load(img_back), (win_width, win_height))

# Create sprites
ship = Player(img_hero, 5, win_height - 100, 80, 100, 10)

# Creating a group of enemy sprites


monsters = sprite.Group()
for i in range(1, 6):
monster = Enemy(img_enemy, randint(80, win_width - 80), -40, 80, 50, randint(1,
5))
monsters.add(monster)

bullets = sprite.Group()

#The "game is over" variable: as soon as True is there, sprites stop working in the
main loop
finish = False

# Main game loop:


run = True # The flag is reset by the window close button
while run:
# "Close" button press event
for e in event.get():
if e.type == QUIT:
run = False
# Event of pressing the spacebar - the sprite shoots
elif e.type == KEYDOWN:
if e.key == K_SPACE:
fire_sound.play()
ship.fire()

# The game itself: actions of sprites, checking the rules of the game, redrawing
if not finish:
# Update the background
window.blit(background, (0, 0))

# Launch sprite movements


ship.update()
monsters.update()
bullets.update()

# Update them in a new location in each loop iteration


ship.reset()
monsters.draw(window)
bullets.draw(window)

#Check for a collision between a bullet and monsters (both monster and bullet
disappear upon a touch)
collides = sprite.groupcollide(monsters, bullets, True, True)
for c in collides:
# This loop will repeat as many times as the number of monsters hit
score = score + 1
monster = Enemy(img_enemy, randint(80, win_width - 80), -40, 80, 50,
randint(1, 5))
monsters.add(monster)

# Possible lose: missed too many monsters or the character collided with an
enemy
if sprite.spritecollide(ship, monsters, False) or lost >= max_lost:
finish = True # Lose, set the background and no longer control the
sprites.
window.blit(lose, (200, 200))

# Win checking: how many points scored?


if score >= goal:
finish = True
window.blit(win, (200, 200))

# Write text on the screen


text = font2.render("Score: " + str(score), 1, (255, 255, 255))
window.blit(text, (10, 20))

text_lose = font2.render("Missed: " + str(lost), 1, (255, 255, 255))


window.blit(text_lose, (10, 50))

display.update()

# Bonus: automatic restart of the game


else:
finish = False
score = 0
lost = 0
for b in bullets:
b.kill()
for m in monsters:
m.kill()

time.delay(3000)
for i in range(1, 6):
monster = Enemy(img_enemy, randint(80, win_width - 80), -40, 80, 50,
randint(1, 5))
monsters.add(monster)

time.delay(50)
Steps to take:
1. Initialize Pygame and Load Resources:
 Use pygame.init() to initialize Pygame.
 Load font resources using font.init() and font.Font.
 Load background music and sound effects using mixer.init() and mixer.Sound.
 Load images using pygame.image.load().
2. Define Game Variables:
 Define variables for the images and sounds required for the game.
 Initialize variables for the game state such as score, goal, and maximum allowed
misses.
3. Define Sprite Classes:
 Create a parent class GameSprite that inherits from pygame.sprite.Sprite.
 Define initialization method (__init__) in GameSprite to set image, position, and
speed.
 Implement reset method in GameSprite to draw the sprite on the window.
 Create subclasses Player, Enemy, and Bullet inheriting from GameSprite.
 Implement update method in each subclass to handle sprite movements.
4. Create Sprites:
 Create instances of player, enemy, and bullet sprites.
 Add enemies to a sprite group.
5. Game Loop:
 Set up the main game loop (while run:).
 Handle events such as quitting the game and firing bullets.
 Update sprite movements and check for collisions within the loop.
 Draw the background, sprites, and text on the screen.
 Manage game over conditions (win or lose).
 Provide an option for automatic restart of the game after a delay.
6. Display and Update:
 Update the display using pygame.display.update() or pygame.display.flip().
 Limit the frame rate using clock.tick() to control the speed of the game.
7. Cleanup and Exit:
 Handle cleanup tasks such as quitting Pygame and releasing resources.
 Use pygame.quit() to properly exit the game.
class Enemy(GameSprite):
# Enemy movement
def update(self):
# Update vertical position of the enemy sprite
self.rect.y += self.speed

# Access global variable 'lost' to keep track of missed enemies


global lost

# Check if the enemy sprite has reached beyond the bottom of the screen
if self.rect.y > win_height:
# If so, reposition the enemy sprite at a random position near the top of the screen
self.rect.x = randint(80, win_width - 80)
self.rect.y = 0

# Increment the 'lost' count as an enemy is missed


lost = lost + 1

Explanation:

1. self.rect.y += self.speed: This line updates the vertical position of the enemy sprite by adding its speed. It makes
the enemy sprite move downwards on the screen.
2. global lost: This line declares that the variable lost is global, allowing it to be accessed and modified within this
method and throughout the entire script. In this case, lost is used to count how many enemies have been
missed.
3. if self.rect.y > win_height: This condition checks if the vertical position of the enemy sprite is beyond the height
of the screen. If it is, it means the enemy sprite has disappeared off the bottom of the screen.
4. self.rect.x = randint(80, win_width - 80): If the enemy sprite has gone off the screen, this line repositions it at a
random horizontal position near the top of the screen, leaving some space from the edges.
5. self.rect.y = 0: After repositioning the enemy sprite horizontally, this line sets its vertical position to 0, effectively
placing it at the top of the screen again.
6. lost = lost + 1: Finally, this line increments the count of missed enemies by 1, updating the global variable lost.

# Creating a group of enemy sprites


monsters = sprite.Group()

# Loop to create multiple instances of Enemy sprites and add them to the 'monsters' group
for i in range(1, 6):
# Create a new instance of the Enemy sprite with random initial position and speed
# The arguments for Enemy are: (player_image, player_x, player_y, size_x, size_y, player_speed)
monster = Enemy(img_enemy, randint(80, win_width - 80), -40, 80, 50, randint(1, 5))

# Add the newly created enemy sprite to the 'monsters' group


monsters.add(monster)

Explanation:

1. monsters = sprite.Group(): This line creates a group of sprites specifically for enemy sprites. This group will allow
us to easily manage and update all the enemy sprites together.
2. for i in range(1, 6):: This loop iterates 5 times, creating 5 instances of the enemy sprite. Each iteration of the loop
creates a new enemy sprite with random initial position and speed.
3. monster = Enemy(...): Inside the loop, a new instance of the Enemy sprite is created using the provided image
(img_enemy), a random initial horizontal position (between 80 pixels and the width of the screen minus 80
pixels), an initial vertical position of -40 pixels (above the screen), a size of 80x50 pixels, and a random speed
between 1 and 5 pixels per frame.
4. monsters.add(monster): After creating each enemy sprite, it is added to the monsters group using the add()
method. This ensures that all enemy sprites are part of the same group, making it convenient to update and
manage them collectively during the game loop.

You might also like