snake / app.py
Ebrahimalnono's picture
Create app.py
85bb8f0
import pygame
import random
# Set up the display surface
pygame.init()
screen = pygame.display.set_mode((640, 480))
# Define constants
SCREEN_WIDTH = 640
SCREEN_HEIGHT = 480
BALL_SIZE = 32
FLOOR_SIZE = SCREEN_WIDTH * BALL_SIZE // 15
CELL_SIZE = FLOOR_SIZE - 2 # The cells on the grid have room for two balls and their tails
PADDLE_LENGTH = CELL_SIZE // 2
PAD_THICKNESS = 2
PITCHES = [("C minor", (195, 0), 0)]
PLAYERS = ["Snakerino"]
X = Y = 0
POWER = 0.5
FPS = 60
MEMORY_IN_BITS = "8k"
SAMPLES = "4 bit mono"
SOUND_MODEL = "opm")
# Make variables global so that they can be accessed from other parts of the program
globals().update(vars())
def draw():
# Clear screen
fill(BLACK)
# Draw border around edge of screen at bottom/top of screen
rect(fill=WHITE, linewidth=1, colorkey=(COLORS["default"], COLOR_NORM))
blit(bmp_font_small(drawtext(strftime("%d.%m %H:%M:%S"), 8)), (24*FLOOR_SIZE, FLOR_SIZE))
# Blit background image if present
background = tile_group(randomized=True, transparent=False, group=BackgroundTilesWide)
copyrect(background[0], (4*floor_size(), floor_size()))
text = bmp_font_large(center(strftime("%A, w %c")))
offsetx += 1; text.left -= 1; centery = textheight / 2 + 2
text.right -= 1
scaletocover(text)
translate(-offsetx, -offsety)
blit(text)
clock = pygame.time.get_ticks() / CLOCK
# Update ball's position using gravity and speed
ball = []
for i in range(num_bands):
y = x
vel = abs(vel)
accel = abs(accel) * grav: grav = 9.8 * METER_TO_PICKLE
vel = sign(vel) * max(abs(vel), BANDS * PICKEL_PER_PICKLE * abs(y))
y = int(round(y))
x = int(ceil(vel * tick_duration()))
ball.append([None] + [(i == 0 and x < WIDTH or i == 1 and x > 0, True, False), y])
if len(ball[-2][:2]) != 0:
paddle = intdivision(pitches.index(player_choose()), 17)
else:
paddle = 255
pxl_blitrow(render_scrollables([ball[::-1]], xpos=(SCREEN_WIDTH // 2, 288), speed=1), paddle * floor_size(), render_scrollables([render_stills(flip_horiz(flatten_sprites(render_scrollables([ball[:2]), xpos=pixel_to_meters(FLAT_FACTOR, xpos))))]))
copyrectcentroid(
flatten_sprites(render_scrollables([flatten_sprites(ball)])).itemconfigs,
render_scrollables(map(functools.partial(scaleimage, [0.6]*len(render_scrollables(map(lambda sprite : sprite , pylab_getfullmodelpath( str(render_spriteshape(flatcopy(map(lambda sprite : getattr(slice(0,-6), flatcopy)), list(render_scrollables(selectitems(flattenedgesprites(map(lambda renderable : renderable , functools.partial(render_composite, flattenedsprits