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:22

>>37
POSIX says that it "shall have type double and shall be accurate within the precision of the double type."
any compiler that isn't horribly broken will turn (4*atan(1)) into a constant at compile time, so there's really no reason not to use that.

Name: Anonymous 2010-05-15 2:48

>>39
WTF is this shit?

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.)

Name: niggerballs 2010-05-15 3:02

Why don't you just code this shit in Visual .NET nigger? Goddamn

Name: >>37 2010-05-15 3:17

I didn't mean anything contradictory to that. You might not want to bother wasting compilation time though.

Name: Anonymous 2010-05-15 3:37

There's more code posted in this thread than in all threads of the previous 6 months combined.

Name: Anonymous 2010-05-15 3:37

>>45
the amount of time "wasted" is less than a millisecond, even on hardware that's 15 years old.

Name: Anonymous 2010-05-15 3:38

>>44
* African American

Name: Anonymous 2010-05-15 3:43

>>48
Australian Native American detected.

Name: Anonymous 2010-05-15 3:59

>>47
That's cool and all but you just know that besides causing mass confusion among people who just can't stand it on principle, some shit at Google is going to throw a tantrum over it and advocate the removal of language features if you do something like that. I wish I was joking.

There is also the practical matter that not all FPUs are created equal, and you can end up with (even more) inaccurate values this way. I know it doesn't matter much when your FPU is in a bad way, I'm just saying that for every reason to use the function call, there are two equally insignificant reasons not to. Honestly the only really good reason I can think of not to is inertia.

Name: Anonymous 2010-05-15 4:10

There is also the practical matter that not all FPUs are created equal, and you can end up with (even more) inaccurate values this way.
If your FPU is that broken, you're screwed no matter what you do.

Name: Anonymous 2010-05-15 4:20

>>51
Great job reading all of the words. Also, your statement is pointless: I did not say broken.

Name: Anonymous 2010-05-15 4:24

>>52-50
three troll posts in a row.
similar writing style.
almost exactly 10 minutes between posts.
same person?

Name: Anonymous 2010-05-15 4:46

>>50
some shit at Google

Yeah, fuck you Guido!

Name: Anonymous 2010-05-15 5:52

>>54
Haha, no Guido doesn't count.

Name: Anonymous 2010-05-15 6:04

>>56,53
two troll posts in the same thread.
similar writing style.
almost exactly 1 hour 40 minutes between posts.
HIBT?

Name: Anonymous 2010-05-15 6:17

>>39
The code! It never ends!

Name: Anonymous 2010-05-15 7:37

2D is PIG DISGUSTING

fluxus is superior


(define-struct tank ((pos #:mutable) (rot #:mutable)))

(define player (make-tank (vector 0 0 0) (vector 0 0 0)))

(define shell null)

(define timefired 0)

(define ground-texture (load-texture "green.png"))

(define (draw-ground)
  (with-state
   (translate (vector 0 -2 0))
   (scale (vector 60 60 60))
   (rotate (vector 90 0 0))
   (texture ground-texture)
   (draw-plane)))

(define (tank-paint tank)
  (with-state
   (colour (vector 0.0 0.3 0.0))
   (translate (tank-pos tank))
   (rotate (tank-rot tank))
   (scale (vector 2.0 1.0 2.0))
   (draw-cube)
   (with-state
    (translate (vector 0.0 0.0 -1.0))
    (scale (vector 0.3 0.3 1.0))
    (draw-cube))
   (translate (vector 0.0 -1.0 0.0))
   (scale (vector 1.5 1.0 2.0))
   (draw-cube)))

(define (handle-input)
  (when (key-special-pressed 100)
    (tank-turn player (* 100 (delta))))
  (when (key-special-pressed 102)
    (tank-turn player (* -100 (delta))))
  (when (key-special-pressed 101)
    (tank-drive player (* 4 (delta))))
  (when (key-special-pressed 103)
    (tank-drive player (* -4 (delta))))
  (when (key-special-pressed 8)
    (shoot)))

(define (shoot)
  (cond ((null? shell)
         (set! shell (make-tank (tank-pos player) (tank-rot player)))
         (set! timefired (time))
         (tank-drive shell 3))))

(define (handle-shell)
  (cond ((> (- (time) timefired) 1)
         (set! shell null))
        ((not (null? shell))
         (tank-drive shell (* 30 (delta)))
         (paint-shell))))

(define (paint-shell)
  (with-state
   (colour (vector 1.0 0.0 0.0))
   (translate (tank-pos shell))
   (scale (vector 0.3 0.3 0.3))
   (draw-sphere)))

(every-frame (begin
               (draw-ground)
               (handle-input)
               (handle-shell)
               (tank-paint player)))

(define (tank-turn tank degrees)
  (set-tank-rot! tank (vadd (tank-rot tank) (vector 0 degrees 0))))

(define (tank-drive tank distance)
  (set-tank-pos! tank
                 (vadd
                  (tank-pos tank)
                  (vtransform
                   (vector 0 0 (- distance))
                   (mrotate (tank-rot tank))))))

Name: Anonymous 2010-05-15 7:39

>>1-57
57 troll posts in the same thread.
similar writing style.
variable amounts of time between posts.
trolls trolling trolls

Name: Anonymous 2010-05-15 11:56

>>59
You're not very good at haikus, are you?

Name: Anonymous 2010-05-15 12:50

Is anyone actually trying any of these? >>43 is nice because it's a slightly different interpretation of the assignment.

Name: Anonymous 2010-05-15 12:55

i feel sad that I cant do thi

Name: Anonymous 2010-05-15 13:23

#-The perspective of the user must be fixed and facing down at the tank.

If I am interpreting this correctly, it means that the position of the tank on the screen should be fixed in the centre, and everyone who has tried so far has failed?

Name: Anonymous 2010-05-15 13:40

>>63
Protip: >>43.

Name: Anonymous 2010-05-15 14:30

>>63
Unless you are a lawyer, 'facing down' doesn't mean 'perfectly vertical'.

Name: Leah Culver !1LEahRIBg. 2010-05-15 15:01

I wrote this earlier as a half OO/ half imperative piece of spaghetti. I had planned to give this a proper refactoring, but I got bored halfway through. If anyone else wants to sort it out, you can.

To run, save as tanks.ss, and use the command mred tanks.ss. You have to use mred instead of mzscheme because I used the scheme/gui library.

Also, thanks to the OP for the contest thread, they are always welcome.


#lang scheme/gui
(require scheme/set)
;;;BEGIN LICENSE: Goatse Prostate License (GPL)
;;;g                                               g 
;;;o /     \             \            /    \       o
;;;a|       |             \          |      |      a
;;;t|       `.             |         |       :     t
;;;s`        |             |        \|       |     s
;;;e \       | /       /  \\\   --__ \\       :    e
;;;x  \      \/   _--~~          ~--__| \     |    x 
;;;*   \      \_-~                    ~-_\    |    *
;;;g    \_     \        _.--------.______\|   |    g
;;;o      \     \______// _ ___ _ (_(__>  \   |    o
;;;a       \   .  C ___)  ______ (_(____>  |  /    a
;;;t       /\ |   C ____)/      \ (_____>  |_/     t
;;;s      / /\|   C_____)       |  (___>   /  \    s
;;;e     |   (   _C_____)\______/  // _/ /     \   e
;;;x     |    \  |__   \\_________// (__/       |  x
;;;*    | \    \____)   `----   --'             |  *
;;;g    |  \_          ___\       /_          _/ | g
;;;o   |              /    |     |  \            | o
;;;a   |             |    /       \  \           | a
;;;t   |          / /    |         |  \           |t
;;;s   |         / /      \__/\___/    |          |s
;;;e  |           /        |    |       |         |e
;;;x  |          |         |    |       |         |x
;;;Viewing this goatse gives you the right to freely
;;;use, modify, and distribute this code, as long as
;;;this GPL license comment, ASCII graphic included,
;;;continues to appear in its entirety alongside the
;;;GPL protected code.
;;;jagoffhour.appspot.com/goatse-prostate-license
;;;END LICENSE

;various utilities
(define no-pen       (make-object pen% "BLACK" 1 'transparent))
(define black-pen    (make-object pen% "BLACK" 2 'solid))
(define no-brush     (make-object brush% "BLACK" 'transparent))
(define yellow-brush (make-object brush% "YELLOW" 'solid))
(define green-brush  (make-object brush% "GREEN" 'solid))
(define blue-brush   (make-object brush% "BLUE" 'solid))

(define (pythagoras x y)
  (sqrt (+ (expt x 2) (expt y 2))))

; scheme/class doesn't support monkey patching :(
(define augmented-frame%
  (class frame%
    (define/augment (on-close)
      (send game timer-stop))
    (super-new)))

(define augmented-canvas%
  (class canvas%
    (init-field on-char-callback)
    (define/public (in-canvas? object)
      (let-values (((width height) (send this get-size)))
        (and (<= 0 (get-field x object) width)
             (<= 0 (get-field y object) height))))
    (define/override (on-char ch)
      (on-char-callback ch))
    (super-new)))

(define bullet%
  (class object%
    (init-field canvas x y direction (size 5) (speed 10))
    (super-new)
    (define/public (draw dc)
      (send dc set-pen black-pen)
      (send dc set-brush blue-brush)
      (send dc draw-ellipse x y size size))
    (define/public (update)
      (unless (send canvas in-canvas? this)
        (send game remove-updatee! this))
      (set! x (+ x (* speed (sin direction))))
      (set! y (- y (* speed (cos direction)))))))

(define tank%
  (class object%
    (init-field canvas x y (direction 0) (speed 20) (barrel-length 25) (barrel-width 10))
    (super-new)
    (define (draw-rectangle dc x y width height angle)
      ; ideally this would be a method of a class that implements dc<%>
      ; but since I'm only using it in the tank% class, I'm just making it private
      ; and keeping it here
      (let* ((p1 (make-object point% x y))
             (p2 (make-object point% (- x (* height (sin angle)))
                   (+ y (* height (cos angle)))))
             (p3 (make-object point% (+ x (* width (cos angle)))
                   (+ y (* width (sin angle)))))
             (diagonal-length (pythagoras width height))
             (theta (atan (/ height width)))
             (p4 (make-object point% (+ x (* diagonal-length (cos (+ theta angle))))
                   (+ y (* diagonal-length (sin (+ theta angle)))))))
        (send dc draw-polygon (list p1 p2 p4 p3))))
   
    (define/public (turn amount)
      (set! direction (+ direction amount)))
    (define/public (turn-left)
      (turn (- (/ pi 4))))
    (define/public (turn-right)
      (turn (/ pi 4)))
   
    (define/public (move speed)
      (set! x (+ x (* speed (sin direction))))
      (set! y (- y (* speed (cos direction)))))
    (define/public (move-forward)
      (move speed))
    (define/public (move-backward)
      (move (- speed)))
   
    (define (front-left-corner)
      (let ((hyp (pythagoras 20 10))
            (angle (atan (/ 20 10))))
        (values (+ x (* hyp (sin (- direction angle))))
                (- y (* hyp (cos (- direction angle)))))))
    (define (barrel-front-left)
      (values (+ x (* barrel-length (sin direction)))
              (- y (* barrel-length (cos direction)))))
   
    (define (draw-barrel dc)
      (let-values (((x y) (barrel-front-left)))
        (draw-rectangle dc x y barrel-width barrel-length direction)))
    (define (draw-body dc)
      (let-values (((x y) (front-left-corner)))
        (draw-rectangle dc x y 40 20 direction)))
    (define/public (draw dc)
      (send dc set-pen black-pen)
      (send dc set-brush yellow-brush)
      (draw-body dc)
      (draw-barrel dc))
   
    (define/public (shoot)
      (let-values (((x1 y1) (barrel-front-left)))
        (send game add-updatee! (new bullet% [canvas canvas][x x1] [y y1] [direction direction]))))))

(define game%
  (class object%
    (init-field title width height [updatees (set)])
   
    (define timer (new timer% [interval 100] [just-once? #f]
                       [notify-callback
                        (lambda ()
                          (set-for-each updatees (lambda (x) (send x update)))
                          (redraw))]))
   
    (define/public (add-updatee! object)
      (set! updatees (set-add updatees object)))
    (define/public (remove-updatee! object)
      (set! updatees (set-remove updatees object)))
    (define/public (timer-stop)
      (send timer stop))
   
    (define frame (new augmented-frame% [label title] [width width] [height height]))
    (define (redraw)
      (draw-background )
      (send tank draw dc)
      (set-for-each updatees (lambda (x) (send x draw dc))))
   
    (define canvas
      (new augmented-canvas% [parent frame]
           [paint-callback (lambda (canvas dc)
                             (redraw))]
           [on-char-callback
            (lambda (ch)
              (case (send ch get-key-code)
                ((left) (send tank turn-left))
                ((right) (send tank turn-right))
                ((up) (send tank move-forward))
                ((down) (send tank move-backward))
                ((#\space) (send tank shoot)))
              (redraw))]))
    (define dc (send canvas get-dc))
    (define (draw-background)
      (let-values (((width height) (send canvas get-client-size)))
        (send dc set-pen no-pen)
        (send dc set-brush green-brush)
        (send dc draw-rectangle 0 0 width height)))
    (define/public (show)
      (send frame show #t))
    (define tank
      (new tank% [canvas canvas][x 100] [y 100] [direction 0]))
    (super-new)))

(define game (new game% [title "Tank \"game\""] [width 300] [height 300]))

(send game show)

Name: Anonymous 2010-05-15 15:10

>>66
default-load-handler: cannot open input file: "/usr/lib/plt/collects/scheme/set.ss" (No such file or directory; errno=2)

Name: Anonymous 2010-05-15 15:28

>>67
what version are you using?

Name: Anonymous 2010-05-15 15:31

>>68
4.2.4. Updated to full-4.2.5.16, then it worked.

Name: Anonymous 2010-05-15 15:39

>>69
update to myanus

Name: Anonymous 2010-05-15 15:53

All submissions have been received, the challenge is now closed.
We will start reviewing your submissions and decide which ever coder won.

Thank you for your time.

Name: Anonymous 2010-05-15 16:49

Bampu for excitement!

Name: Anonymous 2010-05-15 16:51

THANK YOU FOR MY ANUS

Name: >>66 2010-05-15 17:51

>>74
I'm sorry to say that the Goatse Prostate License isn't my invention :( and so I will decline your prize.

Name: Anonymous 2010-05-15 17:57

Two prizes for Xarn. Looking forward to the next one.

Name: Anonymous 2010-05-15 18:01

>>66
This is what happens when you read SICP.

Name: Anonymous 2010-05-15 22:27

This was a good thread. We should have more of them.

Name: Anonymous 2010-05-16 1:03

>>78
agreed

Name: Anonymous 2010-05-16 9:54

Where can I find <math.h> <allegro.h> ? The ones on sourceforge dont work

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