import pygame, random
import math, sys
class Dot(object):
"""Dot object"""
def __init__(self):
self.surf = pygame.image.load("arrow.png").convert_alpha()
self.rect = pygame.Rect(0,0,50,50)
self.frame = 0
self.dir = 0 # 0 = up, 1 = down, 2 = left, 3 = right
self.start_time = pygame.time.get_ticks()
def update(self, keymap):
"""updates the sprite"""
if keymap[0] or keymap[1] or keymap[2] or keymap[3]:
if pygame.time.get_ticks() - self.start_time > 250:
self.frame += 1
if self.frame > 3:
self.frame = 0
self.start_time = pygame.time.get_ticks()
else:
self.frame = 0
self.start_time = pygame.time.get_ticks()
class Platform(object):
"""Platform Object"""
def __init__(self):
self.surf = pygame.Surface((100, 25))
self.surf.fill((0, 0, 0))
self.rect = self.surf.get_rect()
#self.rect = pygame.Rect(200,300,100,25)
class Game(object):
def __init__(self):
self.screen = pygame.display.set_mode((800, 600))
self.screen.fill((0, 255, 255))
self.game_over = False
self.clock = pygame.time.Clock()
self.dot_list = [Dot() for x in range(1)]
self.keymap = [False, False, False, False] #up, down, left, right key pressed? Really should use a dictionary
for dot in self.dot_list:
dot.rect.topleft = (775*random.random(), 575*random.random())
self.platform_list = [Platform() for x in range(1)]
for platform in self.platform_list:
platform.rect.topleft = (200, 300)
def process_input(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.game_over = True
if event.key == pygame.K_d:
self.keymap[3] = True
if event.key == pygame.K_a:
self.keymap[2] = True
if event.key == pygame.K_w:
self.keymap[0] = True
if event.key == pygame.K_s:
self.keymap[1] = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_d:
self.keymap[3] = False
if event.key == pygame.K_a:
self.keymap[2] = False
if event.key == pygame.K_w:
self.keymap[0] = False
if event.key == pygame.K_s:
self.keymap[1] = False
def update(self):
if self.keymap[3]:
for dot in self.dot_list:
for platform in self.platform_list:
dot.dir = 3
future_dot = dot.rect.move(10, 0)
if platform.surf.get_rect().colliderect(future_dot):
dot.rect.right = 200
elif self.screen.get_rect().contains(future_dot):
dot.rect = future_dot
else:
dot.rect.right = 800
if self.keymap[2]:
for dot in self.dot_list:
for platform in self.platform_list:
dot.dir = 2
future_rect = dot.rect.move(-10,0)
if platform.surf.get_rect().colliderect(future_rect):
dot.rect.left = 300
elif self.screen.get_rect().contains(future_rect):
dot.rect = future_rect
else:
dot.rect.left = 0
if self.keymap[0]:
for dot in self.dot_list:
for platform in self.platform_list:
dot.dir = 0
future_rect = dot.rect.move(0,-10)
if platform.surf.get_rect().colliderect(future_rect):
dot.rect.top = 325
elif self.screen.get_rect().contains(future_rect):
dot.rect = future_rect
else:
dot.rect.top = 0
if self.keymap[1]:
for dot in self.dot_list:
for platform in self.platform_list:
dot.dir = 1
future_rect = dot.rect.move(0,10)
if platform.surf.get_rect().colliderect(future_rect):
dot.rect.bottom = 300
elif self.screen.get_rect().contains(future_rect):
dot.rect = future_rect
else:
dot.rect.bottom = 600
for dot in self.dot_list:
dot.update(self.keymap)
def draw(self):
self.screen.fill((0, 255, 255))
for dot in self.dot_list:
self.screen.blit(dot.surf, dot.rect, pygame.Rect(dot.frame*50, dot.dir*50, 50, 50))
for platform in self.platform_list:
self.screen.blit(platform.surf, platform.rect)
pygame.init()
g = Game()
while not g.game_over:
g.clock.tick(30)
g.process_input()
g.update()
g.draw()
pygame.display.flip()
sys.exit()