-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
562 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class AstObject: | ||
def wrap_around_screen(self): | ||
"""Wrap around screen.""" | ||
if self.pos.x > self.settings.screen_size[0]: | ||
self.pos.x = 0 | ||
if self.pos.x < 0: | ||
self.pos.x = self.settings.screen_size[0] | ||
if self.pos.y <= 0: | ||
self.pos.y = self.settings.screen_size[1] | ||
if self.pos.y > self.settings.screen_size[1]: | ||
self.pos.y = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from __future__ import annotations | ||
# for ide type hinting | ||
from typing import TYPE_CHECKING | ||
if TYPE_CHECKING: | ||
from asteroids import AsteroidsGame | ||
|
||
from ast_object import AstObject | ||
import pygame as pg | ||
from pygame.sprite import Sprite | ||
from pygame.math import Vector2 as vec | ||
from pygame.transform import rotozoom | ||
|
||
|
||
class Bullet(Sprite, AstObject): | ||
"""A class to manage bullets fired by the ship""" | ||
|
||
def __init__(self, game: AsteroidsGame): | ||
"""Create a bullet at the ship's current position""" | ||
super().__init__() | ||
self.screen = game.screen | ||
self.settings = game.settings | ||
self.color = self.settings.bullet_color | ||
|
||
# rotate the bullet to the same angle as the ship | ||
self.original_image = pg.Surface( | ||
(self.settings.bullet_width, self.settings.bullet_height)) | ||
self.original_image.fill(self.color) | ||
|
||
self.image = self.original_image.copy() | ||
|
||
self.rect = self.image.get_rect() | ||
self.rect.center = game.ship.pos | ||
self.pos = vec(self.rect.center) | ||
self.direction = game.ship.direction | ||
self.vel = self.direction * self.settings.bullet_speed | ||
self.angle = game.ship.angle | ||
|
||
self.image = pg.transform.rotate(self.original_image, self.angle) | ||
|
||
self.distance_travelled = 0.0 | ||
|
||
def update(self): | ||
self.wrap_around_screen() | ||
|
||
# self._move() | ||
|
||
self.pos += self.vel | ||
self.rect.center = self.pos | ||
|
||
# add to ship's total distance travelled | ||
self.distance_travelled += self.vel.length() | ||
self._check_expiration() | ||
|
||
def _check_expiration(self): | ||
if (self.distance_travelled >= | ||
self.settings.bullet_speed * self.settings.bullet_lifetime): | ||
self.kill() | ||
|
||
def draw(self): | ||
angle = self.direction.angle_to(self.settings.VECTOR_UP) | ||
self.image = rotozoom(self.original_image, angle, 1) | ||
blit_pos = self.pos - vec(self.image.get_size()) * 0.5 | ||
self.screen.blit(self.image, blit_pos) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from __future__ import annotations | ||
# for ide type hinting | ||
from typing import TYPE_CHECKING, Tuple | ||
if TYPE_CHECKING: | ||
from asteroids import AsteroidsGame | ||
|
||
import pygame as pg | ||
from pygame.font import SysFont | ||
|
||
|
||
class Button: | ||
def __init__(self, game: AsteroidsGame, text): | ||
"""Init button attributes""" | ||
|
||
self.game = game | ||
self.screen = game.screen | ||
self.screen_rect = self.screen.get_rect() | ||
|
||
# dimensions and properties of the button | ||
self.width = 250 | ||
self.height = 50 | ||
self.button_color = (107, 208, 255) | ||
self.text_color = (0, 0, 0) | ||
self.font = SysFont(None, 48) | ||
|
||
# build the rect | ||
self.rect = pg.Rect(0, 0, self.width, self.height) | ||
self.rect.center = self.screen_rect.center | ||
|
||
self._prep_text(text) | ||
|
||
def _prep_text(self, text): | ||
self.text_image = self.font.render( | ||
text, True, self.text_color, self.button_color) | ||
self.text_image_rect = self.text_image.get_rect() | ||
self.text_image_rect.center = self.rect.center | ||
|
||
def draw_button(self): | ||
"""Display the button""" | ||
self.screen.fill(self.button_color, self.rect) | ||
self.screen.blit(self.text_image, self.text_image_rect) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class GameStats: | ||
"""Track statistics for Asteroids.""" | ||
|
||
def __init__(self, game): | ||
self.game = game | ||
self.reset_stats() | ||
|
||
# start in an inactive state | ||
self.game_active = False | ||
|
||
def reset_stats(self): | ||
self.score = 0 | ||
self.level = 1 | ||
self.ships_left = self.game.settings.max_ships |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.