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
|
||||
|
||||
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)),
|
||||
'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
|
||||
wiggling oscillation in given duration (in milliseconds).
|
||||
"""
|
||||
if self.wiggle:
|
||||
return maxoffset * cos((pygame.time.get_ticks()/float(duration)
|
||||
+ self.y/float(BOARD_HEIGHT)) * pi)
|
||||
return 0
|
||||
if not self.wiggle: return 0
|
||||
return maxoffset * cos((get_ticks()/duration+self.y/BOARD_HEIGHT)*pi)
|
||||
|
||||
def get_yoffset(self):
|
||||
"""Return the offset on y-axis when the tile is falling."""
|
||||
if self.missed_time is None:
|
||||
return 0
|
||||
return (pygame.time.get_ticks() - self.missed_time)**2 / 25000.0
|
||||
if self.missed_time is None: return 0
|
||||
return (get_ticks() - self.missed_time)**2 / 25000
|
||||
|
||||
def isfallen(self):
|
||||
"""Return if the tile has fallen off the screen."""
|
||||
|
@ -97,11 +99,11 @@ class SlackerTile:
|
|||
color = COLOR_MAJOR
|
||||
else:
|
||||
color = COLOR_MINOR
|
||||
rect = pygame.Rect((self.x+self.get_xoffset(max_x_offset)) * TILE_SIZE,
|
||||
(self.y+self.get_yoffset()) * TILE_SIZE,
|
||||
TILE_SIZE, TILE_SIZE)
|
||||
pygame.draw.rect(self.screen, color[self.dim], rect)
|
||||
pygame.draw.rect(self.screen, BG_COLOR, rect, TILE_SIZE // 11)
|
||||
rect = Rect((self.x+self.get_xoffset(max_x_offset))*TILE_SIZE,
|
||||
(self.y+self.get_yoffset())*TILE_SIZE,
|
||||
TILE_SIZE, TILE_SIZE)
|
||||
draw.rect(self.screen, color[self.dim], rect)
|
||||
draw.rect(self.screen, BG_COLOR, rect, TILE_SIZE // 11)
|
||||
|
||||
|
||||
class Slacker:
|
||||
|
@ -123,9 +125,9 @@ class Slacker:
|
|||
|
||||
def __enter__(self):
|
||||
pygame.init()
|
||||
pygame.display.set_icon(pygame.image.load(self.data('icon.png')))
|
||||
pygame.display.set_caption('Slacker')
|
||||
self.screen = pygame.display.set_mode(SCREEN_SIZE)
|
||||
set_caption('Slacker')
|
||||
set_icon(image.load(self.data('icon.png')))
|
||||
self.screen = set_mode(SCREEN_SIZE)
|
||||
return self
|
||||
|
||||
def __exit__(self, *exc):
|
||||
|
@ -139,8 +141,7 @@ class Slacker:
|
|||
|
||||
def draw_text(self, string, height):
|
||||
"""Width-fit the string in the screen on the given height."""
|
||||
font = pygame.font.Font(
|
||||
self.font, int(SCREEN_WIDTH*2.5/(len(string)+1)))
|
||||
font = Font(self.font, int(SCREEN_WIDTH*2.5/(len(string)+1)))
|
||||
text = font.render(string, False, COLOR_MINOR[0])
|
||||
self.screen.blit(text, ((SCREEN_WIDTH - text.get_width()) // 2,
|
||||
int(SCREEN_HEIGHT * height)))
|
||||
|
@ -151,7 +152,7 @@ class Slacker:
|
|||
(1.5, 4), (2, 5), (3, 5), (4, 5), (4.5, 6),
|
||||
(1.5, 7), (4.5, 7), (2, 8), (3, 8), (4, 8)]:
|
||||
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)
|
||||
|
||||
def draw_board(self):
|
||||
|
@ -175,13 +176,13 @@ class Slacker:
|
|||
self.intro()
|
||||
elif self.game_state in (PLAYING, LOSE, WIN):
|
||||
self.draw_board()
|
||||
pygame.display.flip()
|
||||
flip()
|
||||
|
||||
def update_movement(self):
|
||||
"""Update the direction the blocks are moving in."""
|
||||
speed = self.speed * self.speed_ratio
|
||||
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.board[self.y] = [0 <= x - self.x < self.width
|
||||
for x in range(BOARD_WIDTH)]
|
||||
|
@ -200,8 +201,7 @@ class Slacker:
|
|||
self.board[self.y][x] = False
|
||||
# Then, add that falling block to falling_tiles
|
||||
self.falling_tiles.append(SlackerTile(
|
||||
self.screen, x, self.y,
|
||||
missed_time=pygame.time.get_ticks()))
|
||||
self.screen, x, self.y, missed_time=get_ticks()))
|
||||
self.width = sum(self.board[self.y])
|
||||
if not self.width:
|
||||
self.game_state = LOSE
|
||||
|
@ -214,35 +214,35 @@ class Slacker:
|
|||
|
||||
def handle_intro(self):
|
||||
"""Handle events in intro."""
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT: return False
|
||||
if event.type != pygame.KEYDOWN: continue
|
||||
if event.key in (pygame.K_ESCAPE, pygame.K_q): return False
|
||||
if event.key == pygame.K_SPACE: self.game_state = PLAYING
|
||||
for e in event.get():
|
||||
if e.type == QUIT: return False
|
||||
if e.type != KEYDOWN: continue
|
||||
if e.key in (K_ESCAPE, K_q): return False
|
||||
if e.key == K_SPACE: self.game_state = PLAYING
|
||||
return True
|
||||
|
||||
def handle_playing(self):
|
||||
"""Handle events in game."""
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT: return False
|
||||
if event.type != pygame.KEYDOWN: continue
|
||||
if event.key == pygame.K_SPACE:
|
||||
for e in event.get():
|
||||
if e.type == QUIT: return False
|
||||
if e.type != KEYDOWN: continue
|
||||
if e.key == K_SPACE:
|
||||
self.key_hit()
|
||||
elif event.key in (pygame.K_ESCAPE, pygame.K_q):
|
||||
elif e.key in (K_ESCAPE, K_q):
|
||||
self.__init__()
|
||||
# Yes, these are cheats.
|
||||
elif event.key == pygame.K_0:
|
||||
elif e.key == K_0:
|
||||
self.width += self.width < BOARD_WIDTH
|
||||
elif event.key in range(pygame.K_1, pygame.K_9+1):
|
||||
self.speed_ratio = (pygame.K_9-event.key+1) / 5.0
|
||||
elif K_1 <= e.key <= K_9 + 1:
|
||||
self.speed_ratio = (K_9-e.key+1) / 5.0
|
||||
self.update_movement()
|
||||
return True
|
||||
|
||||
def handle_ending(self):
|
||||
"""Handle events in ending screens."""
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT: return False
|
||||
if event.type == pygame.KEYDOWN: self.__init__(restart=True)
|
||||
for e in event.get():
|
||||
if e.type == QUIT: return False
|
||||
if e.type == KEYDOWN: self.__init__(restart=True)
|
||||
return True
|
||||
|
||||
def handle_events(self):
|
||||
|
|
Loading…
Reference in New Issue