Explicitly import objects from pygame
This commit is contained in:
parent
f83d2d0013
commit
90372bc198
|
@ -27,6 +27,11 @@ from math import cos, pi
|
||||||
from random import randrange
|
from random import randrange
|
||||||
|
|
||||||
with redirect_stdout(StringIO()): import pygame
|
with redirect_stdout(StringIO()): import pygame
|
||||||
|
from pygame import (K_0, K_1, K_9, K_ESCAPE, K_SPACE, KEYDOWN,
|
||||||
|
QUIT, K_q, Rect, draw, event, image)
|
||||||
|
from pygame.display import flip, set_caption, set_icon, set_mode
|
||||||
|
from pygame.font import Font
|
||||||
|
from pygame.time import get_ticks
|
||||||
|
|
||||||
TANGO = {'Butter': ((252, 233, 79), (237, 212, 0), (196, 160, 0)),
|
TANGO = {'Butter': ((252, 233, 79), (237, 212, 0), (196, 160, 0)),
|
||||||
'Orange': ((252, 175, 62), (245, 121, 0), (206, 92, 0)),
|
'Orange': ((252, 175, 62), (245, 121, 0), (206, 92, 0)),
|
||||||
|
@ -76,16 +81,13 @@ class SlackerTile:
|
||||||
"""Return the offset on x-axis to make the tile complete an cycle of
|
"""Return the offset on x-axis to make the tile complete an cycle of
|
||||||
wiggling oscillation in given duration (in milliseconds).
|
wiggling oscillation in given duration (in milliseconds).
|
||||||
"""
|
"""
|
||||||
if self.wiggle:
|
if not self.wiggle: return 0
|
||||||
return maxoffset * cos((pygame.time.get_ticks()/float(duration)
|
return maxoffset * cos((get_ticks()/duration+self.y/BOARD_HEIGHT)*pi)
|
||||||
+ self.y/float(BOARD_HEIGHT)) * pi)
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def get_yoffset(self):
|
def get_yoffset(self):
|
||||||
"""Return the offset on y-axis when the tile is falling."""
|
"""Return the offset on y-axis when the tile is falling."""
|
||||||
if self.missed_time is None:
|
if self.missed_time is None: return 0
|
||||||
return 0
|
return (get_ticks() - self.missed_time)**2 / 25000
|
||||||
return (pygame.time.get_ticks() - self.missed_time)**2 / 25000.0
|
|
||||||
|
|
||||||
def isfallen(self):
|
def isfallen(self):
|
||||||
"""Return if the tile has fallen off the screen."""
|
"""Return if the tile has fallen off the screen."""
|
||||||
|
@ -97,11 +99,11 @@ class SlackerTile:
|
||||||
color = COLOR_MAJOR
|
color = COLOR_MAJOR
|
||||||
else:
|
else:
|
||||||
color = COLOR_MINOR
|
color = COLOR_MINOR
|
||||||
rect = pygame.Rect((self.x+self.get_xoffset(max_x_offset)) * TILE_SIZE,
|
rect = Rect((self.x+self.get_xoffset(max_x_offset))*TILE_SIZE,
|
||||||
(self.y+self.get_yoffset()) * TILE_SIZE,
|
(self.y+self.get_yoffset())*TILE_SIZE,
|
||||||
TILE_SIZE, TILE_SIZE)
|
TILE_SIZE, TILE_SIZE)
|
||||||
pygame.draw.rect(self.screen, color[self.dim], rect)
|
draw.rect(self.screen, color[self.dim], rect)
|
||||||
pygame.draw.rect(self.screen, BG_COLOR, rect, TILE_SIZE // 11)
|
draw.rect(self.screen, BG_COLOR, rect, TILE_SIZE // 11)
|
||||||
|
|
||||||
|
|
||||||
class Slacker:
|
class Slacker:
|
||||||
|
@ -123,9 +125,9 @@ class Slacker:
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
pygame.init()
|
pygame.init()
|
||||||
pygame.display.set_icon(pygame.image.load(self.data('icon.png')))
|
set_caption('Slacker')
|
||||||
pygame.display.set_caption('Slacker')
|
set_icon(image.load(self.data('icon.png')))
|
||||||
self.screen = pygame.display.set_mode(SCREEN_SIZE)
|
self.screen = set_mode(SCREEN_SIZE)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __exit__(self, *exc):
|
def __exit__(self, *exc):
|
||||||
|
@ -139,8 +141,7 @@ class Slacker:
|
||||||
|
|
||||||
def draw_text(self, string, height):
|
def draw_text(self, string, height):
|
||||||
"""Width-fit the string in the screen on the given height."""
|
"""Width-fit the string in the screen on the given height."""
|
||||||
font = pygame.font.Font(
|
font = Font(self.font, int(SCREEN_WIDTH*2.5/(len(string)+1)))
|
||||||
self.font, int(SCREEN_WIDTH*2.5/(len(string)+1)))
|
|
||||||
text = font.render(string, False, COLOR_MINOR[0])
|
text = font.render(string, False, COLOR_MINOR[0])
|
||||||
self.screen.blit(text, ((SCREEN_WIDTH - text.get_width()) // 2,
|
self.screen.blit(text, ((SCREEN_WIDTH - text.get_width()) // 2,
|
||||||
int(SCREEN_HEIGHT * height)))
|
int(SCREEN_HEIGHT * height)))
|
||||||
|
@ -151,7 +152,7 @@ class Slacker:
|
||||||
(1.5, 4), (2, 5), (3, 5), (4, 5), (4.5, 6),
|
(1.5, 4), (2, 5), (3, 5), (4, 5), (4.5, 6),
|
||||||
(1.5, 7), (4.5, 7), (2, 8), (3, 8), (4, 8)]:
|
(1.5, 7), (4.5, 7), (2, 8), (3, 8), (4, 8)]:
|
||||||
SlackerTile(self.screen, *i, state=INTRO).draw(1.5)
|
SlackerTile(self.screen, *i, state=INTRO).draw(1.5)
|
||||||
if pygame.time.get_ticks() // 820 % 2:
|
if get_ticks() // 820 % 2:
|
||||||
self.draw_text('Press Spacebar', 0.75)
|
self.draw_text('Press Spacebar', 0.75)
|
||||||
|
|
||||||
def draw_board(self):
|
def draw_board(self):
|
||||||
|
@ -175,13 +176,13 @@ class Slacker:
|
||||||
self.intro()
|
self.intro()
|
||||||
elif self.game_state in (PLAYING, LOSE, WIN):
|
elif self.game_state in (PLAYING, LOSE, WIN):
|
||||||
self.draw_board()
|
self.draw_board()
|
||||||
pygame.display.flip()
|
flip()
|
||||||
|
|
||||||
def update_movement(self):
|
def update_movement(self):
|
||||||
"""Update the direction the blocks are moving in."""
|
"""Update the direction the blocks are moving in."""
|
||||||
speed = self.speed * self.speed_ratio
|
speed = self.speed * self.speed_ratio
|
||||||
positions = BOARD_WIDTH + self.width - 2
|
positions = BOARD_WIDTH + self.width - 2
|
||||||
p = int(round(pygame.time.get_ticks() / speed)) % (positions * 2)
|
p = int(round(get_ticks()/speed)) % (positions*2)
|
||||||
self.x = (-p % positions if p > positions else p) - self.width + 1
|
self.x = (-p % positions if p > positions else p) - self.width + 1
|
||||||
self.board[self.y] = [0 <= x - self.x < self.width
|
self.board[self.y] = [0 <= x - self.x < self.width
|
||||||
for x in range(BOARD_WIDTH)]
|
for x in range(BOARD_WIDTH)]
|
||||||
|
@ -200,8 +201,7 @@ class Slacker:
|
||||||
self.board[self.y][x] = False
|
self.board[self.y][x] = False
|
||||||
# Then, add that falling block to falling_tiles
|
# Then, add that falling block to falling_tiles
|
||||||
self.falling_tiles.append(SlackerTile(
|
self.falling_tiles.append(SlackerTile(
|
||||||
self.screen, x, self.y,
|
self.screen, x, self.y, missed_time=get_ticks()))
|
||||||
missed_time=pygame.time.get_ticks()))
|
|
||||||
self.width = sum(self.board[self.y])
|
self.width = sum(self.board[self.y])
|
||||||
if not self.width:
|
if not self.width:
|
||||||
self.game_state = LOSE
|
self.game_state = LOSE
|
||||||
|
@ -214,35 +214,35 @@ class Slacker:
|
||||||
|
|
||||||
def handle_intro(self):
|
def handle_intro(self):
|
||||||
"""Handle events in intro."""
|
"""Handle events in intro."""
|
||||||
for event in pygame.event.get():
|
for e in event.get():
|
||||||
if event.type == pygame.QUIT: return False
|
if e.type == QUIT: return False
|
||||||
if event.type != pygame.KEYDOWN: continue
|
if e.type != KEYDOWN: continue
|
||||||
if event.key in (pygame.K_ESCAPE, pygame.K_q): return False
|
if e.key in (K_ESCAPE, K_q): return False
|
||||||
if event.key == pygame.K_SPACE: self.game_state = PLAYING
|
if e.key == K_SPACE: self.game_state = PLAYING
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def handle_playing(self):
|
def handle_playing(self):
|
||||||
"""Handle events in game."""
|
"""Handle events in game."""
|
||||||
for event in pygame.event.get():
|
for e in event.get():
|
||||||
if event.type == pygame.QUIT: return False
|
if e.type == QUIT: return False
|
||||||
if event.type != pygame.KEYDOWN: continue
|
if e.type != KEYDOWN: continue
|
||||||
if event.key == pygame.K_SPACE:
|
if e.key == K_SPACE:
|
||||||
self.key_hit()
|
self.key_hit()
|
||||||
elif event.key in (pygame.K_ESCAPE, pygame.K_q):
|
elif e.key in (K_ESCAPE, K_q):
|
||||||
self.__init__()
|
self.__init__()
|
||||||
# Yes, these are cheats.
|
# Yes, these are cheats.
|
||||||
elif event.key == pygame.K_0:
|
elif e.key == K_0:
|
||||||
self.width += self.width < BOARD_WIDTH
|
self.width += self.width < BOARD_WIDTH
|
||||||
elif event.key in range(pygame.K_1, pygame.K_9+1):
|
elif K_1 <= e.key <= K_9 + 1:
|
||||||
self.speed_ratio = (pygame.K_9-event.key+1) / 5.0
|
self.speed_ratio = (K_9-e.key+1) / 5.0
|
||||||
self.update_movement()
|
self.update_movement()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def handle_ending(self):
|
def handle_ending(self):
|
||||||
"""Handle events in ending screens."""
|
"""Handle events in ending screens."""
|
||||||
for event in pygame.event.get():
|
for e in event.get():
|
||||||
if event.type == pygame.QUIT: return False
|
if e.type == QUIT: return False
|
||||||
if event.type == pygame.KEYDOWN: self.__init__(restart=True)
|
if e.type == KEYDOWN: self.__init__(restart=True)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def handle_events(self):
|
def handle_events(self):
|
||||||
|
|
Loading…
Reference in New Issue