public
anonymous / robots.py
Created

  • Download Gist
robots.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
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()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.