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:
Anonymous2010-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.
>>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:
Anonymous2010-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.
Is anyone actually trying any of these? >>43 is nice because it's a slightly different interpretation of the assignment.
Name:
Anonymous2010-05-15 12:55
i feel sad that I cant do thi
Name:
Anonymous2010-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?
>>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
(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))]))