Skip to content

Commit

Permalink
Merge pull request rodrigets#8 from nagoli104/master
Browse files Browse the repository at this point in the history
shooting of asteroids added
  • Loading branch information
rodrigets authored Oct 6, 2017
2 parents b657132 + c2545e1 commit 5c80e42
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,17 @@ Install dependencies by running `pip install -r requirements.txt`
### Usage:
Run `python asteroides.py` to start the game.

Use arrow keys to move up, down, left and right on the canvas.
Use arrow keys to move up, down, left and right on the canvas.
Use space bar to shoot.


## License:
Uses [GPL-3.0 License](https://github.com/rodrigets/games-in-pygame/blob/master/LICENSE).
Uses [GPL-3.0 License](https://github.com/rodrigets/games-in-pygame/blob/master/LICENSE).

## Credits
Bullet image was provided by [Gumichan01](https://gumichan01.github.io/en/)

Shooting sound was taken from [K.L.Jonasson, Winnipeg, Canada./ Triki Minut Interactive](https://twitter.com/trikiminut)



73 changes: 67 additions & 6 deletions asteroides.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@


def main():

pygame.init()
pygame.font.init()
pygame.mixer.pre_init(44100, 32, 2, 4096)
pygame.font.init()


font_name = pygame.font.get_default_font()
game_font = pygame.font.SysFont(font_name, 72)
Expand Down Expand Up @@ -36,9 +38,14 @@ def main():
},
'rect': Rect(0, 0, 48, 48)
}


# sounds
explosion_sound = pygame.mixer.Sound('boom.wav')
explosion_played = False

fire_sound = pygame.mixer.Sound('shot.ogg')
fire_played = False

pygame.display.set_caption('Asteroides')

clock = pygame.time.Clock()
Expand All @@ -53,8 +60,45 @@ def create_asteroid():

ticks_to_asteroid = 90
asteroids = []



# counter to prevent permanent fire
ticks_to_shot = 15
shots = []

def create_shot(ship):
# load image of bullet
surf = pygame.image.load('bullet.png')
# scale it to smaller size
surf = pygame.transform.scale(surf,(10,10))
ship_rect = get_rect(ship)

return {
'surface': surf.convert_alpha(),
# create shot on tip of the ship
'position': [(ship_rect[0] + 0.5*ship_rect[2]) - 5,
ship_rect[1]],
'speed': 5
}

def move_shots():
for shot in shots:
shot['position'][1] -= shot['speed']

def remove_missed_shots():
# remove shots from game that leave screen
for shot in shots:
if shot['position'][1] < 0:
shots.remove(shot)

def shoot_asteroids():
# check for collisions between shots and asteroids
for shot in shots:
shot_rect = get_rect(shot)
for asteroid in asteroids:
if shot_rect.colliderect(get_rect(asteroid)):
shots.remove(shot)
asteroids.remove(asteroid)

def move_asteroids():
for asteroid in asteroids:
asteroid['position'][1] += asteroid['speed']
Expand Down Expand Up @@ -90,6 +134,7 @@ def ship_collided():
asteroids.append(create_asteroid())
else:
ticks_to_asteroid -= 1


ship['speed'] = {
'x': 0,
Expand All @@ -111,14 +156,28 @@ def ship_collided():
ship['speed']['x'] = -5
elif pressed_keys[K_RIGHT]:
ship['speed']['x'] = 5

if pressed_keys[K_SPACE] and ticks_to_shot <= 0:
# play sound
fire_sound.play()
shots.append(create_shot(ship))
# set timer for next possible shot
ticks_to_shot = 15


screen.blit(background, (0, 0))

move_asteroids()

move_shots()
shoot_asteroids()

for asteroid in asteroids:
screen.blit(asteroid['surface'], asteroid['position'])


for shot in shots:
screen.blit(shot['surface'], shot['position'])


if not collided:
collided = ship_collided()
ship['position'][0] += ship['speed']['x']
Expand Down Expand Up @@ -154,7 +213,9 @@ def ship_collided():

pygame.display.update()
time_passed = clock.tick(30)
ticks_to_shot -= 1

remove_used_asteroids()
remove_missed_shots()

main()
Binary file added bullet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added shot.ogg
Binary file not shown.

0 comments on commit 5c80e42

Please sign in to comment.