Message
Message
import random
import os
# Game Settings
DISPLAY_W = 480
DISPLAY_H = 600
FPS = 60
BLACK = pygame.Color(0, 0, 0)
WHITE = pygame.Color(255, 255, 255)
BLUE = pygame.Color(0, 255, 255)
########################
# HELPER FUNCTIONS #
########################
Example:
fontName = pygame.font.match_font('arial')
drawText(self.display, 'Hello', fontName, 18, (DISPLAY_W/2, 10))
"""
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.center = (x, y)
surface.blit(text_surface, text_rect)
def waitKey(key=pygame.K_RETURN):
"""
Waits for the given key to be pressed (KEYUP)
or the window to be closed (QUIT).
Returns False if QUIT event detected and True oherwise.
The key argument is optional, the default is the ENTER key.
Example:
running = waitKey()
"""
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
if event.type == pygame.KEYUP and event.key == key:
return True
Example:
drawMultImg(self.display, ASSETS['test'], 2, (20,20), (35,0))
"""
for i in range(n):
rect = img.get_rect()
rect.center = (start[0] + i * spacing[0], start[1] + i * spacing[1])
surface.blit(img, rect)
########################
# SPRITE CLASSES #
########################
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50, 50))
self.image.fill((BLUE))
self.rect = self.image.get_rect()
self.rect.center = (DISPLAY_W / 2, DISPLAY_H / 2)
def __init__(self):
pygame.sprite.Sprite.__init__(self)
# TODO: define position variable (center of player, should match with rect)
########################
# GAME CLASS #
########################
class Game:
"""
Game Class, responsible for initialing pygame modules,
loading game assets and controlling game states and game loop.
"""
def __init__(self):
pygame.init()
self.display = pygame.display.set_mode((DISPLAY_W, DISPLAY_H))
pygame.display.set_caption('Space Shooter')
self.loadAssets()
self.running = True
def loadAssets(self):
# TODO: load game assets: images, sounds, fonts...
# Example:
# ASSETS['player_img'] = pygame.image.load(os.path.join(IMG_DIR,
'player.png')).convert()
# ASSETS['player_img'].set_colorkey(BLACK)
# ASSETS['font_name'] = pygame.font.match_font('arial')
pass
def run(self):
while self.running:
self.showStartScreen()
if self.running:
self.playLevel()
def showStartScreen(self):
# TODO: draw start screen and wait user input
pass
clock = pygame.time.Clock()
allSprites = pygame.sprite.Group()
# TODO: create other groups if needed
player = Player()
allSprites.add(player)
# TODO: create other sprites/objects if needed
playing = True
# START LEVEL
while playing:
# dt: seconds passed since last frame
dt = clock.tick(FPS) / 1000
# check events
for event in pygame.event.get():
# close window event
if event.type == pygame.QUIT:
self.running = False
playing = False
pygame.display.update()
def quit(self):
pygame.quit()
if __name__ == "__main__":
g = Game()
g.run()
g.quit()