Skip to content

Instantly share code, notes, and snippets.

Created May 20, 2013 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5615274 to your computer and use it in GitHub Desktop.
Save anonymous/5615274 to your computer and use it in GitHub Desktop.
import sys, pygame
import math
import time
pygame.init()
def display_box(screen, message, x, y):
fontobject=pygame.font.SysFont('Arial', 14)
if len(message) != 0:
screen.blit(fontobject.render(message, 1, (255, 255, 255)),
(x, y))
#pygame.display.flip()
def move(r):
x = math.cos(r.angle) * r.speed
y = math.sin(r.angle) * r.speed
c = r.rect.center
r.rect.center=(c[0]+x, c[1]+y)
# print r.rect.center
def colisionRobot(r1, r2):
x1 = r1.rect.center[0]
y1 = r1.rect.center[1]
x2 = r2.rect.center[0]
y2 = r2.rect.center[1]
if math.sqrt((x1-x2)**2 + (y1-y2)**2) < 50:
return True
return False
def colisionSides(r, L, W):
x = r.rect.center[0]
y = r.rect.center[1]
d1 = math.sqrt((x)**2 + (0)**2) < 25
d2 = math.sqrt((x - L)**2 + (0)**2) < 25
d3 = math.sqrt((y)**2 + (0)**2) < 25
d4 = math.sqrt((y - W)**2 + (0)**2) < 25
if(d2 or d1):
print "FIM"
if(d1 or d2 or d3 or d4):
r.angle = - r.angle
r.speed -= r.speed*0.1
print r.angle, r.speed
class robot(pygame.sprite.Sprite):
def __init__(self, location, image, angle, speed):
pygame.sprite.Sprite.__init__(self)
robot.image = pygame.image.load(image).convert()
robot.image.set_colorkey((0,0,0))
self.image = robot.image
self.rect = self.image.get_rect()
self.rect.center = location
self.speed = speed
self.angle = angle
#------
#L = 5
#W = 1
R = int(0.25 * 100)
#D_R1 = 45
#V_R1 = 9
#D_R2 = 45
#V_R2 = -15
L, W = map(int, raw_input().split())
D_R1, V_R1 = map(int, raw_input().split())
D_R2, V_R2 = map(int, raw_input().split())
#------
fim = 0
V_R2 = -V_R2
L *= 100
W *= 100
screen = pygame.display.set_mode((L, W))
r1 = robot([R, W/2], "r1.png", D_R1, V_R1)
r2 = robot([L-R, W/2], "r2.png", D_R2, V_R2)
screen.blit(r1.image, r1.rect)
screen.blit(r2.image, r2.rect)
while 1:
event = pygame.event.poll()
if event.type == pygame.QUIT:
break
if not fim:
screen.fill((0,0,0))
msgr1 = "R1: S:"+ "%.2f" % r1.speed + ", A: " + str(r1.angle)
msgr2 = "R2: S:"+ "%.2f" % r2.speed + ", A: " + str(r2.angle)
display_box(screen, msgr1, 10, 10)
display_box(screen, msgr2, L- 120, 10)
move(r1)
move(r2)
colisionSides(r1, L, W)
colisionSides(r2, L, W)
if colisionRobot(r1, r2):
fim = 1
if r1.speed > math.fabs(r2.speed):
display_box(screen, "R1 Won!! "+ str("%.2f" % r1.speed) + " > " + str(math.fabs("%.2f" % r2.speed)), L/2-100, 10)
else:
display_box(screen, "R2 Won!! " + str("%.2f" % math.fabs(r2.speed)) + " > " + str("%.2f" % r1.speed), L/2-100, 10)
r1.speed = 0
r2.speed = 0
screen.blit(r1.image, r1.rect)
screen.blit(r2.image, r2.rect)
time.sleep(0.05)
pygame.display.flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment