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)
/* Copyright (c) 2010 Xarn
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
Disclaimer: this code has many examples of "bad practices" because I was 100% focused on minimize coding time while meeting the requirements
import java.awt.*;
import java.applet.*;
import java.util.*;
import java.awt.event.*;
import java.awt.image.*;
public class a extends Applet implements Runnable,KeyListener{
double x,y;
double r,sdx,sdy;
boolean left,right,down,up;
double sx=-5,sy=-5;
int TANK_SIZE=20;
BufferedImage buf;
public void init(){
x=getWidth()/2;
y=getHeight()/2;
addKeyListener(this);
buf=new BufferedImage(getWidth(),getHeight(),BufferedImage.TYPE_3BYTE_BGR);
new Thread(this).start();
}
public void update(Graphics g){
paint(buf.getGraphics());
g.drawImage(buf, 0,0,null);
}
public void paint(Graphics g){
int[] xpoints=new int[8];
int[] ypoints=new int[8];
for(int i=0;i<4;i++){
xpoints[i]=(int)(x+TANK_SIZE*Math.cos(r+i*Math.PI/2));
ypoints[i]=(int)(y+TANK_SIZE*Math.sin(r+i*Math.PI/2));
}
xpoints[4]=(4*xpoints[0]+5*xpoints[3])/9;
ypoints[4]=(4*ypoints[0]+5*ypoints[3])/9;
xpoints[5]=(int)(x+1.5*TANK_SIZE*Math.cos(r-3*Math.PI/11));
ypoints[5]=(int)(y+1.5*TANK_SIZE*Math.sin(r-3*Math.PI/11));
xpoints[6]=(int)(x+1.5*TANK_SIZE*Math.cos(r-3*Math.PI/13));
ypoints[6]=(int)(y+1.5*TANK_SIZE*Math.sin(r-3*Math.PI/13));
xpoints[7]=(5*xpoints[0]+4*xpoints[3])/9;
ypoints[7]=(5*ypoints[0]+4*ypoints[3])/9;
g.setColor(Color.gray);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(Color.green);
g.fillPolygon(xpoints,ypoints,8);
if(left)
r-=.1;
if(right)
r+=.1;
double dx=Math.cos(r-Math.PI/4);
double dy=Math.sin(r-Math.PI/4);
if(up){
x+=dx*10;
y+=dy*10;
}
if(down){
x-=dx*10;
y-=dy*10;
}
g.setColor(Color.yellow);
if(sx!=-5&&sy!=-5){
sx+=sdx;
sy+=sdy;
}
if(sx<-3||sx>getWidth()||sy<-3||sy>getWidth()){
sx=-5;
sy=-5;
}
g.fillOval((int)sx-1, (int)sy-1,3, 3);
}
public void run() {
while(true){
Graphics g=getGraphics();
if(g!=null)
update(getGraphics());
try{
Thread.sleep(30);
}catch(Exception e){}
}
}
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
up=true;
break;
case KeyEvent.VK_DOWN:
down=true;
break;
case KeyEvent.VK_LEFT:
left=true;
break;
case KeyEvent.VK_RIGHT:
right=true;
break;
case KeyEvent.VK_SPACE:
if(sx==-5&&sy==-5){
sx=x;
sy=y;
sdx=Math.cos(r-Math.PI/4)*20;
sdy=Math.sin(r-Math.PI/4)*20;
}
break;
}
}
public void keyReleased(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_UP:
up=false;
break;
case KeyEvent.VK_DOWN:
down=false;
break;
case KeyEvent.VK_LEFT:
left=false;
break;
case KeyEvent.VK_RIGHT:
right=false;
break;
}
}
public void keyTyped(KeyEvent e){}
}
>>16 #ifndef M_PI #define M_PI 3.14159265358979323846 #endif
This is unnecessary if you define _BSD_SOURCE or _XOPEN_SOURCE=500. The math.h man page doesn't tell you this for some reason, but M_PI isn't part of ANSI C, so compiling with -ansi undefines it (along with a bunch of other constants).
I initially thought glibc was being retarded, but it turns out it was just poor documentation.
Name:
Anonymous2010-05-14 20:05
>>22
It wasn't written for shortness or readability thus the disclaimer, 100% code time. Also the ugliest part is the tank drawing which the C version sidesteps by using images.
compared to the boilerplate needed for win32, opengl or directx to do the same? yes, no contest
Name:
Anonymous2010-05-14 22:17
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
static class Program
{
static void Main(string[] args)
{
using (TankGame game = new TankGame()) game.Run();
}
}
public class Entity
{
public Texture2D Texture;
public Vector2 Location;
public float Facing;
public float Velocity;
public Vector2 Origin { get { return new Vector2(this.Width / 2, this.Height / 2); } }
public int Height { get { return Texture.Height; } }
public int Width { get { return Texture.Width; } }
spriteBatch.Begin();
this.tank.Draw(this.spriteBatch);
foreach (Entity shot in shots) shot.Draw(this.spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
Name:
Anonymous2010-05-14 22:28
>>25
I can only assume you've never used a game programming or graphic library yourself.
You can even leave out END_OF_MAIN(), if you like. It's only there for the retarded platforms. It saves you the effort of having to write a WinMain by hand.
>>30
So why not do the same WinMain hack in Allegro that SDL is using? All they do is #define main to SDLmain or something, and link the "real" main in the library itself.
>>33
How many is enough? Engineers and physicists usually claim to need about 3-5. I need about 7. Almost no one needs as much as 12, but since it's a constant: why not?
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
//System.out.println("test ");
if ( key == KeyEvent.VK_UP )
up = true;
if ( key == KeyEvent.VK_LEFT )
left = true;
if ( key == KeyEvent.VK_DOWN )
down = true;
if ( key == KeyEvent.VK_RIGHT )
right = true;
repaint();
e.consume();
}
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
int i;
if ( key == KeyEvent.VK_UP )
up = false;
if ( key == KeyEvent.VK_LEFT )
left = false;
if ( key == KeyEvent.VK_DOWN )
down = false;
if ( key == KeyEvent.VK_RIGHT )
right = false;
if ( key == KeyEvent.VK_SPACE)
{
for(i = 0; i < 3; i++)
{
if(shots[i].active == false)
{
shots[i] = new burret(xpos, ypos, counter);
shots[i].active = true;
i = 3;
}
}
>>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))]))
Last time I checked, windows doesn't have sudo, apt-get or any unix command.
Name:
Anonymous2010-05-16 16:12
>>86
Before anybody blasts me... I know apt-get is not "unix" nor is it used in real "unix" systems, because they use a ports system. I was more or less talking about *nix userlandish commands.
>>86
It does have a posix subsystem, and there's also cygwin and mingw. As for the native C compiler: it comes with many development packages, but since most packages are distributed as binaries by default, there's no need to include the compiler unless the user is a developer, in which case he can install everything he needs himself.
Name:
Anonymous2010-05-16 17:29
>>85
It's assumed that anyone who doesn't use Ubanto knows what he's doing to an extent sufficient that he can locate and install libc and one of the most popular C game programming libraries around himself.
>>91
Idiot. The fact that it's a meme doesn't mean that it's empty of content, and >>90 replied to that content. You're the kind of person that gives meme-spouters a bad name.
>>92
Just go away. Your interpretation has been noted and implemented even before your joke was made the first time, thirty posts ago.
Thank goodness you guys have gotten back to quality distro-bashing and accusations of homosexuality. I was worried that there might actually be from programming going on here.
>>116
if you compile the source yourself, that depends on how you configure it.
Name:
Help!!2010-05-18 9:39
>>16
Tank.c:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘-’ token
Tank.c:1: error: stray ‘`’ in program
Tank.c:1: error: stray ‘`’ in program
In file included from /usr/include/stdio.h:75,
from Tank.c:24:
/usr/include/libio.h:332: error: expected specifier-qualifier-list before ‘size_t’
/usr/include/libio.h:364: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/libio.h:373: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/libio.h:493: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘_IO_sgetn’
In file included from Tank.c:24:
/usr/include/stdio.h:312: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:319: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:361: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:363: error: format string argument not a string type
/usr/include/stdio.h:365: error: expected declaration specifiers or ‘...’ before ‘size_t’
/usr/include/stdio.h:678: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fread’
/usr/include/stdio.h:684: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fwrite’
/usr/include/stdio.h:706: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fread_unlocked’
/usr/include/stdio.h:708: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘fwrite_unlocked’
Tank.c:26:21: error: allegro.h: No such file or directory
Tank.c: In function ‘main’:
Tank.c:38: error: ‘fixed’ undeclared (first use in this function)
Tank.c:38: error: (Each undeclared identifier is reported only once
Tank.c:38: error: for each function it appears in.)
Tank.c:38: error: expected ‘;’ before ‘tangle’
Tank.c:40: error: ‘BITMAP’ undeclared (first use in this function)
Tank.c:40: error: ‘background’ undeclared (first use in this function)
Tank.c:40: error: ‘tank’ undeclared (first use in this function)
Tank.c:40: error: ‘miss’ undeclared (first use in this function)
Tank.c:40: error: ‘buffa’ undeclared (first use in this function)
Tank.c:47: error: ‘GFX_AUTODETECT_WINDOWED’ undeclared (first use in this function)
Tank.c:67: error: ‘mangle’ undeclared (first use in this function)
Tank.c:68: error: ‘tangle’ undeclared (first use in this function)
Tank.c:69: error: ‘screen’ undeclared (first use in this function)
Tank.c:78: error: ‘KEY_SPACE’ undeclared (first use in this function)
Tank.c:87: error: ‘KEY_LEFT’ undeclared (first use in this function)
Tank.c:91: error: ‘KEY_RIGHT’ undeclared (first use in this function)
Tank.c:95: error: ‘KEY_UP’ undeclared (first use in this function)
Tank.c:99: error: ‘KEY_DOWN’ undeclared (first use in this function)
Tank.c: In function ‘END_OF_MAIN’:
Tank.c:117: error: expected ‘{’ at end of input
>>117
So what about if I install with setup.exe? And if I were to compile it myself, how ought I to configure it?
Name:
Anonymous2010-05-19 19:30
goto register for new and explicit private friend
Name:
Anonymous2010-05-19 19:39
do do do do do do do#########################################
for do do do do goto
do do do do do do do#########################################
for do do do do goto
do do do do do do do#########################################
for do do do do goto
do do do do do do do#########################################
I think every program won in at least one category. Obviously the result of pragu'eshidden communist agenda!
If we ever have another good thread like this again, we should properly critique programs and rate them from 1-10 on the INDUSTRY STANDARD PTS (Prague Troll Scale).
Name:
Anonymous2010-05-26 11:04
[b]NEW RESULTS:[b]
EVERYONE IS A WINNER!!!!!!!!!!!!!!!!!!!!!!!!
Name:
1702010-05-26 11:04
except me /b
Name:
Anonymous2010-05-26 15:43
So, who is up for Round 2: 3D Tank?
Requirements: Same as above, only in 3D perspective. Additionally, must be no more than 4kb of executable code and data (total) and include an award-winning soundtrack no less than 20 minutes in duration.
I could probably do the sound dev for that challenge, but I'm kind of over 4k stuff. But I bet if you extended it to 8kb you could very easily stuff in a full networked version.
>>180
4k challenges on /prog/ are about as sensible as shutting up, getting things done and not trolling each other on /prog/: never mind the implications, it's just not going to happen. I only brought it up because I felt like shitposting.
>>182
The "award-winning" requirement was put there to ensure you don't. If you do manage to pull it off I am sure someone will be happy to suck your dick for it.
i am so glad i stopped wasting my life with programming. now i have so much time to waste on other, more enjoyable things like games and explicitly sexual videos(no homosex).
>>204 Xarn writes perfect ANSI C. If it's erroring on allegro_init(), which is line 38, >>203 has fucked up either his Allegro install or his compiler arguments.
If you're talking about fixed, which >>203 wasn't, that's part of Allegro, not C99.
Name:
Anonymous2010-05-31 2:15
Xarnbump.
Name:
Anonymous2010-05-31 4:49
>>207
I use Miracle C compiler the best C compiler known to man