Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Challenge: 2D Tank

Name: Anonymous 2010-05-14 15:43

The Challenge:
|---->Program a 2d tank application in the language of your choice that adheres to the following:

#-The tank must rotate using the left and right directional keys.
#-The tank must loosely resemble a tank.
#-The tank must move forward or backward respectively from the direction it is facing using the up and down directional keys.
#-Pressing spacebar must fire a 'shot' in the direction the tank is facing.
#-The perspective of the user must be fixed and facing down at the tank.
#-The tank, shot, and ground must each have their own color. (all shots may have the same color)


<------You have 24 hours!------>
GET TO WORK!

Name: Anonymous 2010-05-15 2:49

Another one, just because.
Images here: http://cairnarvon.rotahall.org/pytank/

#!/usr/bin/python

import pyglet
from pyglet.window import key

import math

window = pyglet.window.Window(640, 640, caption="PYTANK")


tank_img = pyglet.image.load('tank.png')
tank_img.anchor_x = tank_img.width / 2
tank_img.anchor_y = tank_img.height / 2

tank = pyglet.sprite.Sprite(tank_img)
tank.x = window.width / 2
tank.y = window.height / 2
tank.rotation = -90

tank.angling, tank.speed = 0, 0

background = pyglet.image.load('background.png').get_texture()
background.offset = [0, 0]

missile_img = pyglet.image.load('missile.png')
missile_img.anchor_x = missile_img.width / 2
missile_img.anchor_y = missile_img.height / 2

missilen = pyglet.graphics.Batch()
missiles = []


def rotate_left():   tank.angling = -3
def rotate_right():  tank.angling = 3
def stop_rotation(): tank.angling = 0
def move_forward():  tank.speed = 4
def move_backward(): tank.speed = -4
def stop_moving():   tank.speed = 0
def fire():
    missiles.append(pyglet.sprite.Sprite(missile_img,
                                         window.width / 2,
                                         window.height / 2,
                                         batch=missilen))
    missiles[-1].angle = (tank.rotation - 180) * math.pi / 180


@window.event
def on_key_press(symbol, modifiers):
    try:
        { key.LEFT: rotate_left,
          key.RIGHT: rotate_right,
          key.UP: move_forward,
          key.DOWN: move_backward,
          key.SPACE: fire,
          key.Q: pyglet.app.exit }[symbol]()
    except:
        pass

@window.event
def on_key_release(symbol, modifiers):
    try:
        { key.LEFT: stop_rotation,
          key.RIGHT: stop_rotation,
          key.UP: stop_moving,
          key.DOWN: stop_moving }[symbol]()
    except:
        pass

@window.event
def on_draw():
    window.clear()
    background.blit(*background.offset)
    missilen.draw()
    tank.draw()


def update(tic):
    dx, dy = 0, 0

    if tank.speed:
        dx = tank.speed * math.sin(tank.rotation * math.pi / 180)
        dy = tank.speed * math.cos(tank.rotation * math.pi / 180)

        background.offset[0] += dx
        while background.offset[0] <= -128:
            background.offset[0] += 128
        while background.offset[0] > 0:
            background.offset[0] -= 128

        background.offset[1] += dy
        while background.offset[1] <= -128:
            background.offset[1] += 128
        while background.offset[1] > 0:
            background.offset[1] -= 128

    for miss in missiles:
        miss.x += dx + 8 * math.sin(miss.angle)
        miss.y += dy + 8 * math.cos(miss.angle)
        miss.rotation += 6

    tank.rotation += tank.angling

pyglet.clock.schedule_interval(update, 1 / 60.)
pyglet.app.run()


(Spot the memory leak.)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List