Snake Game - Refresh Grid Of ASCII Characters Every 0.2 Seconds
Mar 4, 2014
I am busy programming a clone of the popular phone game they had on Nokia cellphones a long time ago called Snake II but since I know very little about programming I will be using ASCII graphics instead of a 2D graphics engine.
My idea for implementation is having a main class called game which should refresh a grid of ascii characters every, say 0.2 seconds. Then I have another class called Dot. Each Dot object has x and y coordinates, and a direction in the x and y planes (dirx = -1 means left, dirx = 1 means right, diry = 1 means up, diry = -1 means down, and obviously the snake cant move the diagonals)
The Game class prints a "*" symbol where the Dot is, and what I'm trying to do is get the screen to refresh (I think I need to use the sleep() function for this to slow the game down to a reasonable pace), and go in the direction it is supposed to go.
(I haven't programmed this in yet but the snake will be an array of Dot, and at each refresh Dot at position 0 will pass it's coordinates and direction to Dot at position 1, Dot1 to Dot2, Dot2 to Dot3, etc.
Here's my code so far:
A first class called Game.
Java Code: //Not done yet but this is the start to my game of Snake. Basically the class Game generates a Grid of ASCII characters
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
final int WIDTH = 79;
[Code] .....
My problem now is that I am taking input from the Scanner class. What it does is wait for my input, then it goes on to executing the rest of the code in other words refresh the ASCII grid. This is a problem because I need the snake to keep moving at constant pace while listening to the keyboard.
How can I get my while loop to keep going (i will add a sleep() function later) while listening to the keyboard without stopping?
View Replies
ADVERTISEMENT
Dec 14, 2014
For part of my program, I am trying to ask the user for input on generating a number of random characters. I have already done this with integers and doubles, how would I do this for characters using ASCII values? Would I use the same format as I did for generating integers (Shown in code)?
import java.util.Random;
import java.util.Scanner;
public class NewNumberCharacter {
public static void main(String[] args) {
// Create a Scanner
Scanner input = new Scanner(System.in);
[code]....
View Replies
View Related
Apr 9, 2014
gridworld snake game all over the internet, but all of those are the "snakebug extends Bug" version, mine is different. im behind everyone else in apcs so i rarely have any idea what im doing(gridworld especially). our teacher gave us most of the code, which i coped down (hopefully correctly) and he told us that we still needed to fill in the "removeTail" method which i did. basically the snake moves by placing a segment in front then removing the last segment.
When i click run the snakerunner file, i can make the snake segment rotate by pressing the arrow keys, but it doesn't move. when i click the "run" on the gridworld window, the single snake segment just disappears. so the move method is triggering the "removetail" method but it isn't placing the next segment. im starting to think that i copied something wrong from the board, because he showed us the entire move method. i have attached my gridworld files, what is wrong with my snake.java that makes the move method delete a segment but not add one
View Replies
View Related
Jun 16, 2014
I am working on replicating this project: [URL] ....
What is wrong with the moveSnake method and the for loop within it?
View Replies
View Related
May 25, 2014
I'm trying to create a simple snake game, but I can't get my timer and keyListener to work...
//Timer
Timer tm = new Timer(5, this);
int x = 50, y = 250, vel = 2;
// The snake
g.setColor(Color.black);
g.fillRect(x, y, snakeSize, snakeSize);
[Code] .....
View Replies
View Related
Apr 19, 2014
I tried to make a runnable snake game. When i try to run my code i get this:
Exception in thread "main" java.lang.NullPointerException
at Game.Move(Game.java:85)
at Game.run(Game.java:244)
at Game.main(Game.java:38)
The game runs, but the snake doesn't move, and apples are spawned everywhere.
Here are Move, run and main code:
public static void main(String[] args) {
JFrame frame = new JFrame("Snake");
Game game = new Game();
frame.add(game);
frame.setSize(406, 430);
frame.setVisible(true);
frame.setResizable(false);
[Code] .....
View Replies
View Related
May 27, 2015
I wanted to know which RAD TOOL should I use to make the grid. I used the label but it's not interesting like the game *. I m starting to make the game and can't think how to make the grid.
Thought of using the buttons but can't add image to it.
what should I use to make the grid.
* what I did earlier Is place 9 label in grid form (just to test) and nine buttons one for each. When u click a button, image is shown in respective label... But it doesn't feel like u r playing game..
View Replies
View Related
Mar 28, 2014
I am making a RTS game in Pure Java, and no libraries. So pretty much I want a constructor like so
Java Code: new Grid(40, 40) mh_sh_highlight_all('java');
That will create a 40x40 grid of a certain size per cell. Then I would like to fill the empty cells, and also be able to place things on top of them.
Also, I would like a tilt similar to Clash of Clans. How do I do this?
View Replies
View Related
Apr 14, 2014
I am making a java text adventure and i am trying to add characters to it. I wrote it exactly the same as i wrote the items class (which works fine) but for some reason the character class keeps getting a null pointer exception when it gets to line 140 in the room class
characters.add(c);
game class
import java.util.ArrayList;
import java.util.HashMap;
/**
* Game class for Free Willy Five: an exciting text based adventure game.
*
[Code] ....
View Replies
View Related
May 24, 2015
I am trying to make a simple platformer game in Java. When I try to move the character I made it only moves over then immediately back
import java.awt.event.KeyEvent;
public class UCC
{
private int xLoc;
private int yLoc;
private int xSpeed;
private int ySpeed;
private double gravity;
private int size;
[code]....
View Replies
View Related
Jul 24, 2014
I am having a problem with my java program. My goal is to request the user to enter how many times they want to roll a pair of dices. Each dice have numbers 1-6. When rolling the dices, I randomly pick a number from each dice and total the number. My goal is to calculate the number of times snake-eyes was rolled which is a total of 2 and total number of times a total of 7 was rolled. Here is my code. I am calling the rollDice method to perform the random pick and calculations. The error I am getting is at the bottom of the code.
package dice;
import java.util.Scanner;
import java.util.Random;
public class Dice
{
public static void main(String[] args)
{
int numRolls; // number of rolls
[code]...
How many times would you like to roll the two dice? 1000 Exception in thread "main" java.lang.NullPointerException at dice.Dice.main(Dice.java:40)
Java Result: 1 BUILD SUCCESSFUL (total time: 4 seconds)
View Replies
View Related
Feb 25, 2014
I want to execute my jsp code to compare two dates after every 10 seconds if user enter date and current sys date are equal it will send the mail to the user automatically. Below is my jsp code
this if condition i want to check the date after every 10 seconds and when two dates are equal it will send the mail using below mail code
if(ExpcReDt.compareTo(dateStr)>0) {
out.println("Expected return date is greater than current date");
}
else if(ExpcReDt.compareTo(dateStr)==0)
[code]....
View Replies
View Related
Jul 11, 2014
How can I time my while loop to executeevery 3 seconds?
Here is my code:
class Info{
public String name;
public String version;
public String arch;
double CPUSpeed;
};
Info info = new Info();
Info[] queue = new Info[100];
[Code]...
I seem to have done it right, but it doesn't work... What is wrong?
View Replies
View Related
Apr 8, 2014
I am trying to convert time in seconds to hhmmss format.How do i get the minutes and seconds.
View Replies
View Related
Apr 27, 2013
I want to compare two times. What I am doing is
java.util.Date date = new java.util.Date();
java.sql.Time cur_time = new java.sql.Time(date.getTime());
/*this is my database connectivity code from where I am getting second date that I am comparing. This is right. Don't bother it.
Connection c= ConnectionManager.getConnection();
String sql = "select t from datesheet where subjectCode=? and sessional=? and d=?";
PreparedStatement ps = c.prepareStatement(sql);
ps.setString(1,subjectCode);
ps.setString(2,sessional);
ps.setDate(3,cur_date);
ResultSet rs = ps.executeQuery();*/
if(rs.next())
{
java.sql.Time t = rs.getTime("t");
long diff = cur_time.getTime()-t.getTime();
}
Then I am converting it to seconds, minutes and hours, but the problem is time that we get from cur_time.getTime() has more digits than time that we get from t.getTime(), so it is always greater than t.getTime() even when it is not.
long seconds = (diff/1000)%60;
long minutes = (diff/60000)%60;
long hours = (diff/(60*60*1000))%24;
View Replies
View Related
Oct 6, 2014
I made an Analog Clock and its working but when a remove the filloval (Background) the seconds hand keep repeating itself..here is the code
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import javax.swing.JFrame;
import javax.swing.JPanel;
[code]....
View Replies
View Related
Jul 7, 2014
So I'm trying to make an applet and I found that Thread.sleep() to simply delay is a bad idea.
I'm not sure how to use the Timer class to implement a same version of sleep() to delay 6 seconds. I am trying to do this in a for loop to delay before every loop. How can I implement this?
This code is after the init() method. For simplicity I didn't include. Assuming I did:
Timer t = new Timer(6000, null);
public void actionPerformed(ActionEvent e) {
try {
URL rsUrl = new URL("http://rscript.org/lookup.php?type=namecheck&name=" + n1);
BufferedReader br = new BufferedReader(new InputStreamReader(rsUrl.openStream()));
for (count = 0; count < maxCount; count++) {
[Code] ....
View Replies
View Related
May 16, 2014
I try to use below codes to convert Hex String "1111737999630745" to ASCII.
When I read the result in Notepad++ with HEX-Editor plugin, I find attached image which is different from above String, there is one additional c2 and I am quite confused.
Java Code:
public static void main(String[] args) throws IOException {
Test strToHex = new Test();
File file = new File("D:/filename");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
[Code] .....
View Replies
View Related
Apr 19, 2015
I need to write a program that combines everything I learned in my Java course from chapter 1-6 and 8 by Tony Gaddis at least with all the other chapters being bonus points, so I decided to write a program that tells you the binary to hex conversion or hex to ascii conversion. However I keep getting the following errors in my class, enums, and main program that I'm not sure about why. Please do not point out that the numbers in the case select are not what the hex values translate to because I know that, but I was trying to use more meaningful place holders temporarily:
These are the errors for main:
----jGRASP exec: javac BinToHex.java
BinToHex.java:9: error: cannot find symbol
Scanner keyboard = new Scanner(System.in);
^
symbol: class Scanner
location: class BinToHex
[Code] .....
3 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Because of the error in the picture attached I am unable to show the remaining errors for the class and enumsjGrasp Save error for Ascii character import enum datatype.jpg
Here is the code for main:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.*;
/**
This program shows that you can binary to hex and hex to ascii.
*/
public class BinToHexToAscii
[Code] .....
/**
Hex value output enumerated data type
*/
enum BintoHexfinalDatatype { "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F",
"10","11","12","13","14","15","16","17","18","19","1A","1B","1C","1D","1E","1F",
"20","21","22","23","24","25","26","27","28","29","2A","2B","2C","2D","2E","2F",
"30","31","32","33","34","35","36","37","38","39","3A","3B","3C","3D","3E","3F",
"40","41","42","43","44","45","46","47","48","49","4A","4B","4C","4D","4E","4F",
"50","51","52","53","54","55","56","57","58","59",&quo
View Replies
View Related
Feb 2, 2014
I am having an awful time submitting the right program for this star pattern. 2.4: Star Pattern Write a program that displays the following pattern:
*
***
*****
*******
*****
I input the following:
// Program Challenge 2.4
2
3 public class PC24
4 {
5 public static void main(String[ ] args)
6
7 /**
Newline Advances the cursor to the next line for subsequent printing p.42*/
8
9 {
10
11 System.out.println(" *
"+ " ***
"+" *****
"+"*******
"+ " *****
");
12
13
14 }
15 }
View Replies
View Related
Jan 21, 2015
I have two programs that talk to each other over a TCP connection. When I write the data "STX+1234" where STX is the Ascii character STX or Ctrl B and I expect the written String length to be 6 which it is. On the other side of the socket I create the ServerSocket and use the client socket's InputStream to create a BufferedReader. When I receive the string it now has 12 characters where each original character has been replaced by NUL and the character. How do I read the string as it is originally specified without the conversion? And is the problem on the reader side or the writer side?
View Replies
View Related
Feb 12, 2014
I've written some code to do this, but it's not working as I expect. This is what the ASCII map looks like:
###################
#.................#
#......G........E.#
#.................#
#..E..............#
#..........G......#
#.................#
#.................#
###################
Here's how I've tried to store the information in a 2D array:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
public class Map {
public void importMap() throws IOException{
BufferedReader br = new BufferedReader(new FileReader("map.txt"));
String line;
[Code] ....
But it's not printing out what I'm expecting. It's printing out:
[[#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [#, #, #, #, #, #, #, #, #], [
What is printed is much larger than what I've copy and pasted but I can't post it all here.
The error I get is:
Exception in thread "main" java.io.IOException: Stream closed
at java.io.BufferedReader.ensureOpen(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Map.importMap(Map.java:26)
at Map.main(Map.java:44)
View Replies
View Related
Aug 25, 2014
I am trying to figure out how to convert a string of ASCII code into char.I know that you can use (char) to convert it, but the issue is you cannot really just it for Strings.
View Replies
View Related
Feb 23, 2014
I am trying to make a class definition file for an ASCII File.
Ultimately, I want to be able to add methods to allow the image produced by the file to be printed normally, then printed with various manipulations.
However, for some reason, whenever I try to run the program to test my normalPrint method, it terminates without printing anything.
I think this is because the array's values width and height are not within the scope of the method. I tried passing the array as a parameter for the method like so:
Java Code:
public void normalPrint(char [][] poop){
//method here
} mh_sh_highlight_all('java');
But it gave me an error that stated
"The method normalPrint(char[][]) in the type asciiFile is not applicable for the arguments ()"
Class Definition:
Java Code:
import java.io.*;
import java.util.Scanner;
public class asciiFile {
int height;
int width;
[Code] .....
View Replies
View Related
May 23, 2014
I want to make a parser from binary to ASCII with JavaCC or another Compiler Construction with Java. I don't know how to start.
View Replies
View Related
Mar 22, 2014
This is my program.
public class C4
{
// instance variables - replace the example below with your own
public static void main(String[] args)
{
System.out.println((char)41);
}
}
I wanted to check that the integer 41 is 'A' in UTF-8, although it's ')' in UTF-16 , so I ran the program with javac -encoding UTF-8 C4.java but the result was still ')'.
How do i fix this? Also, do constants for the max values of ASCII and UTF-8 exist in Java? I need to show how many number of bits are used to represent characters in both encodings.
View Replies
View Related