Created
April 7, 2013 06:17
-
-
Save 74togo/5329271 to your computer and use it in GitHub Desktop.
A little example of the aim-bot tracker thing I made. Uses pygame.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame, sys, time, random, math | |
from pygame.locals import * | |
# this paragraph is just your standard pygame stuff | |
pygame.init() | |
screen_width = 640 | |
screen_height = 480 | |
screen = pygame.display.set_mode((screen_width, screen_height), RESIZABLE, 32) | |
pygame.display.set_caption("Shooter test") | |
screen.fill((0, 0, 0)) | |
def getAngle(h, k, d, c, m, v): | |
# h = enemy's change in x | |
# k = enemy's change in y | |
# d = difference of y value from me to enemy | |
# c = difference of x value from me to enemy | |
# m = amount of time passed between scanning enemy | |
# v = speed of our bullet | |
lawl = 0 | |
rape = 1 | |
if c <=0: | |
c = abs(c)+0.000000000001 | |
lawl = math.pi | |
rape = -1 | |
h *= -1 | |
thing = -c**4* k**2* m**2* v**2+c**4* m**4* v**4+2* c**3* d* h* k* m**2* v**2-c**2* d**2* h**2* m**2* v**2+c**2* d**2* m**4* v**4 | |
thing = abs(thing) | |
derp = -c**2* m**2* v**2* (c**2* k**2-c**2* m**2* v**2-2* c* d* h* k+d**2* h**2-d**2* m**2* v**2) | |
derp = abs(derp) | |
angle = lawl + math.pi/2 - math.atan2((math.sqrt(thing)-c* d* k* m* v+d**2* h* m* v)/(c**2* m**2* v**2+d**2* m**2* v**2),((d* m* v* math.sqrt(derp))/(c**2* m**2* v**2+d**2* m**2* v**2)-(c* d**2* k* m**2* v**2)/(c**2* m**2* v**2+d**2* m**2* v**2)+(d**3* h* m**2* v**2)/(c**2* m**2* v**2+d**2* m**2* v**2)+c* k-d* h)/(c* m* v)) | |
# if c_b <= 0: | |
# if d <= 0 or True: | |
# angle = (math.pi*(2/3) - angle) + math.pi*(2/3) | |
# else: | |
# angle = (math.pi/2 - angle) + math.pi/2 | |
return rape*angle | |
class Enemy: | |
def __init__(self): | |
# Standard set up of position and sprite | |
self.x = 50 | |
self.y = 50 | |
self.vx = 3 | |
self.vy = 3 | |
self.color = (255,0,0) | |
def update(self): | |
if self.x < 0 or self.x+32 > screen_width: | |
self.vx *= -1 | |
if self.y < 0 or self.y+32 > screen_height: | |
self.vy *= -1 | |
self.move() | |
self.draw() | |
def move(self): | |
# Takes input and moves x cordinate | |
self.x += self.vx | |
self.y += self.vy | |
def draw(self): | |
pygame.draw.rect( screen, self.color, self.get_rect() ) | |
def get_rect(self): | |
return pygame.Rect(self.x-16, self.y-16, 32, 32) | |
class Player(Enemy): | |
def __init__(self): | |
self.x = screen_width/2 | |
self.y = 256 | |
self.color = (0,255,0) | |
def update(self): | |
self.draw() | |
class Tracker: | |
def __init__(self): | |
# [ (x1, y1), (x2, y2) ] | |
self.enemy_positions = [(0,0),(0,0)] | |
self.last_scan_time = time.time() | |
self.before_last_scan_time = time.time()-2 | |
def add_new_pos(self, enemy): | |
if time.time() - self.last_scan_time >= 0.1: | |
if len(self.enemy_positions) == 2: | |
self.enemy_positions.pop(0) | |
self.enemy_positions.append((enemy.x, enemy.y)) | |
self.before_last_scan_time = self.last_scan_time | |
self.last_scan_time = time.time() | |
def get_time_change(self): | |
return self.last_scan_time - self.before_last_scan_time | |
def get_change_in_x(self): | |
return self.enemy_positions[1][0] - self.enemy_positions[0][0] | |
def get_change_in_y(self): | |
return self.enemy_positions[1][1] - self.enemy_positions[0][1] | |
class Bullet(Enemy): | |
def __init__(self, x, y, angle): | |
self.x = x | |
self.y = y | |
self.color = (255,255,0) | |
self.angle = angle | |
def update(self): | |
angle = self.angle | |
self.x += math.cos(angle)*bullet_speed | |
self.y += math.sin(angle)*bullet_speed | |
pygame.draw.rect( screen, self.color, self.get_rect() ) | |
def get_rect(self): | |
return pygame.Rect(self.x, self.y, 8, 8) | |
def shoot(t, p, e): | |
h = t.get_change_in_x() | |
k = t.get_change_in_y() | |
d = e.y - p.y | |
c = e.x - p.x | |
m = t.get_time_change() | |
v = math.sqrt(bullet_tracker.get_change_in_x()**2 + bullet_tracker.get_change_in_y()**2)/bullet_tracker.get_time_change() | |
if v == 0: | |
v = bullet_speed | |
angle = getAngle(h, k, d, c, m, v) | |
bullets.append(Bullet(p.x, p.y, angle)) | |
e = Enemy() | |
p = Player() | |
c = pygame.time.Clock() | |
tracker = Tracker() | |
bullet_tracker = Tracker() | |
bullets = [] | |
bullet_speed = 10 | |
while __name__ == "__main__": | |
# Upate screen | |
pygame.display.update() | |
screen.fill((0, 0, 0)) | |
# Check if trying to close window | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
pygame.quit() | |
sys.exit() | |
pressed_keys = pygame.key.get_pressed() | |
if pressed_keys[K_SPACE]: | |
shoot(tracker, p, e) | |
if pressed_keys[K_RIGHT]: | |
p.x += 5 | |
elif pressed_keys[K_LEFT]: | |
p.x -= 5 | |
if pressed_keys[K_DOWN]: | |
p.y += 5 | |
elif pressed_keys[K_UP]: | |
p.y -= 5 | |
e.update() | |
p.update() | |
for b in bullets: | |
b.update() | |
bullet_tracker.add_new_pos(bullets[0]) | |
tracker.add_new_pos(e) | |
c.tick(30) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment