-
Notifications
You must be signed in to change notification settings - Fork 0
/
starry_sky.py
89 lines (73 loc) · 3.07 KB
/
starry_sky.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 29 15:09:30 2020
@author: Cathig
"""
import sys
import pygame
from random import randint
from settings import Settings
from star import Star
class StarrySky:
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.settings = Settings()
# Set the window size and title bar text
# Windowed
self.screen = pygame.display.set_mode(
(self.settings.screen_width, self.settings.screen_height))
# Full screen
# self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
# self.settings.screen_width = self.screen.get_rect().width
# self.settings.screen_height = self.screen.get_rect().height
pygame.display.set_caption("Starry Sky")
self.stars = pygame.sprite.Group()
self._create_starry_field()
def _create_starry_field(self):
"""Create the field of stars."""
# Create a star and find the number of stars in a row if spaced evenly.
star = Star(self)
star_width, star_height = star.rect.size
available_space_x = self.settings.screen_width - (2 * star_width)
number_stars_x = available_space_x // (2 * star_width)
# Determine the number of rows of stars that fit if spaced evenly.
available_space_y = (self.settings.screen_height - 3 * star_height)
number_stars_y = available_space_y // (2 * star_height)
# Determine the number of stars to create.
number_stars = number_stars_x * number_stars_y
# Create the full field of of stars.
for star_number in range(number_stars):
self._create_star(star_number)
def _create_star(self, star_number):
#Create a star and place it in the row.
star = Star(self)
star_width, star_height = star.rect.size
star.x = randint(0, self.settings.screen_width - star_width)
star.rect.x = star.x
star.rect.y = randint(0, self.settings.screen_height - star_height)
self.stars.add(star)
def run_game(self):
"""Start the main loop for the game."""
while True:
self._check_events()
self._update_screen()
def _check_events(self):
"""Respond to key presses and mouse events."""
# Gracefully exit when 'X' or alt+F4 close the window
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN
and event.key == pygame.K_q):
pygame.quit()
# sys.exit() - in text, but does not close gracefully
def _update_screen(self):
"""Update images on the screen, and flip to the new screen."""
self.screen.fill(self.settings.bg_color)
self.stars.draw(self.screen)
# Make the most recently drawn screen visible.
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance, and run the game.
ss = StarrySky()
ss.run_game()