Last active
August 29, 2015 14:01
-
-
Save wibbia/81da880280b5f32a50a0 to your computer and use it in GitHub Desktop.
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, math | |
from pygame.locals import * | |
pygame.init() | |
size = width, height = 1000,600 | |
black = 0,0,0 | |
brown = 168,132,32 | |
grey = 119,119,119 | |
screen = pygame.display.set_mode(size) | |
tank_image = pygame.image.load("tank.png") | |
class Tank(pygame.sprite.Sprite): # class for making walls | |
def __init__(self, image): | |
pygame.sprite.Sprite.__init__(self) | |
self.angle = 0 | |
self.image = image | |
self.rect = self.image.get_rect() | |
def moveForward(self): | |
theta = math.radians(self.angle) | |
tank_list.sprites()[0].rect.x += round(math.sin(theta)) * speed | |
tank_list.sprites()[0].rect.y += round(math.cos(theta)) * speed | |
def moveBackward(self): | |
theta2 = math.radians(self.angle) | |
tank_list.sprites()[0].rect.x -= round(math.sin(theta2)) * speed | |
tank_list.sprites()[0].rect.y -= round(math.cos(theta2)) * speed | |
def moveForward2(self): | |
theta3 = math.radians(self.angle) | |
tank_list.sprites()[1].rect.x += round(math.sin(theta3)) * speed | |
tank_list.sprites()[1].rect.y += round(math.cos(theta3)) * speed | |
def moveBackward2(self): | |
theta4 = math.radians(self.angle) | |
tank_list.sprites()[1].rect.x -= round(math.sin(theta4)) * speed | |
tank_list.sprites()[1].rect.y -= round(math.cos(theta4)) * speed | |
def turnLeft(self): | |
center = self.rect.center | |
self.angle += 2 | |
self.image = pygame.transform.rotate(tank_image, self.angle) | |
self.rect = self.image.get_rect() | |
self.rect.center = center | |
self.angle %= 360 | |
def turnRight(self): | |
center = self.rect.center | |
self.angle -= 2 | |
self.image = pygame.transform.rotate(tank_image, self.angle) | |
self.rect = self.image.get_rect() | |
self.rect.center = center | |
self.angle %= 360 | |
class Bullet(pygame.sprite.Sprite): | |
def __init__(self, x, y, angle): | |
pygame.sprite.Sprite.__init__(self) | |
self.image = pygame.Surface([16, 16]) | |
self.image.fill(black) | |
self.rect = self.image.get_rect() | |
self.rect.x = x | |
self.rect.y = y | |
self.angle = angle | |
tank_list = pygame.sprite.Group() | |
class Wall(pygame.sprite.Sprite): # class for making walls | |
def __init__(self, x, y, width, height, color): | |
pygame.sprite.Sprite.__init__(self) | |
self.image = pygame.Surface([width, height]) | |
self.image.fill(color) | |
self.rect = self.image.get_rect() | |
self.rect.x = x | |
self.rect.y = y | |
wall_list = pygame.sprite.Group() | |
all_sprites_list = pygame.sprite.Group() | |
for i in range(0, 2): # make 2 tanks | |
new_tank = Tank(tank_image) | |
tank_list.add(new_tank) | |
all_sprites_list.add(new_tank) | |
# set tank position | |
tank_list.sprites()[1].rect.y = 510 | |
tank_list.sprites()[1].rect.x = 920 | |
tank_list.sprites()[0].rect.y = 50 | |
tank_list.sprites()[0].rect.x = 50 | |
# make 4 walls and other squares | |
new_wall = Wall(10, 10, 980, 20, grey) | |
new_wall2 = Wall(10,570,980,20, grey) | |
new_wall3 = Wall(10,10,20,580, grey) | |
new_wall4 = Wall(970,10,20,580, grey) | |
new_wall_square = Wall(350,150, 300,300, grey) | |
wall_list.add(new_wall) | |
wall_list.add(new_wall2) | |
wall_list.add(new_wall3) | |
wall_list.add(new_wall4) | |
wall_list.add(new_wall_square) | |
all_sprites_list.add(wall_list.sprites()) | |
keysDown = [] | |
def is_key_down(key): | |
return key in keysDown | |
speed = 1 | |
while True: # start main loop | |
screen.fill((brown)) | |
for event in pygame.event.get(): | |
if event.type == pygame.KEYDOWN: | |
keysDown.append(event.key) | |
elif event.type == pygame.KEYUP: | |
keysDown.remove(event.key) | |
time.sleep(0.025) | |
if is_key_down(pygame.K_UP):# Move tank 1 up | |
tank_list.sprites()[0].moveForward() | |
if len(pygame.sprite.spritecollide(tank_list.sprites()[0], wall_list, False)) > 0: | |
tank_list.sprites()[0].moveBackward() | |
if is_key_down(pygame.K_DOWN):# Move tank 1 down | |
tank_list.sprites()[0].moveBackward() | |
if len(pygame.sprite.spritecollide(tank_list.sprites()[0], wall_list, False)) > 0: | |
tank_list.sprites()[0].moveForward() | |
if is_key_down(pygame.K_LEFT):# rotate tank 1 left | |
tank_list.sprites()[0].turnLeft() | |
if len(pygame.sprite.spritecollide(tank_list.sprites()[0], wall_list, False)) > 0: | |
tank_list.sprites()[0].turnRight() | |
if is_key_down(pygame.K_RIGHT):# rotate tank 1 right | |
tank_list.sprites()[0].turnRight() | |
if len(pygame.sprite.spritecollide(tank_list.sprites()[0], wall_list, False)) > 0: | |
tank_list.sprites()[0].turnLeft() | |
if is_key_down(pygame.K_w):# move tank 2 up | |
tank_list.sprites()[1].moveForward2() | |
if len(pygame.sprite.spritecollide(tank_list.sprites()[1], wall_list, False)) > 0: | |
tank_list.sprites()[1].moveBackward2() | |
if is_key_down(pygame.K_s):# move tank 2 down | |
tank_list.sprites()[1].moveBackward2() | |
if len(pygame.sprite.spritecollide(tank_list.sprites()[1], wall_list, False)) > 0: | |
tank_list.sprites()[1].moveForward2() | |
if is_key_down(pygame.K_a):# move tank 2 left | |
tank_list.sprites()[1].turnLeft() | |
if len(pygame.sprite.spritecollide(tank_list.sprites()[1], wall_list, False)) > 0: | |
tank_list.sprites()[1].turnRight() | |
if is_key_down(pygame.K_d):# move tank 2 right | |
tank_list.sprites()[1].turnRight() | |
if len(pygame.sprite.spritecollide(tank_list.sprites()[1], wall_list, False)) > 0: | |
tank_list.sprites()[1].turnLeft() | |
if is_key_down(pygame.K_ESCAPE):# quit the game with Esc | |
sys.exit() | |
if is_key_down(pygame.K_RETURN):# set tanks back to original position | |
tank_list.sprites()[1].rect.y = 510 | |
tank_list.sprites()[1].rect.x = 920 | |
tank_list.sprites()[0].rect.y = 50 | |
tank_list.sprites()[0].rect.x = 50 | |
if is_key_down(pygame.K_SPACE): | |
Bullet() | |
all_sprites_list.draw(screen)# draw all sprites onto the screen | |
pygame.display.update()# update the screen |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment