Java I
Jonah Warrenjonah@parsons.edu
http://a.parsons.edu/~java2004
Lab 2
Complete the following exercises:
- Make a block that scrolls from left to right and wraps.
- Draw a rectangle on a canvas. When the user rolls over the rectangle with the mouse, change the background color.
- Create a program that alternates between black and white when you click on the canvas. Make it cycle through three colors.
Review the following code we went over in class. Change variable values and insert println() to determine the value of a changing variable.
- Using floats
float xPos=0.0;
void setup()
{
size(100,100);
}
void loop()
{
background(255);
xPos=xPos+0.1;
rect(xPos, 45, 10, 10);
}
int xPos=0;
void setup()
{
size(100,100);
}
void loop()
{
background(255);
rect(xPos, 45, 10, 10);
xPos=(xPos+1)%width;
}
boolean clicked = false;
void setup()
{
size(100,100);
}
void loop()
{
background(255);
clicked=mousePressed;
println(clicked);
if(clicked) {
background(0);
}
}
void setup()
{
}
void loop()
{
String name1="Bob";
String name2="Al";
String name3="Lance";
int randNum=0;
randNum=(int)random(3);
if (randNum==0) {
println(name1);
}
else if (randNum==1) {
println(name2);
}
else {
println(name3);
}
}
Homework 2
(Please email me if you have any questions.)- Create your own abstract clock using the processing's time functions (millis(), second(), minute(), hour()). Look at this as an example.
- Take a look at the code that follows. Its a simple game where the user tries to click a box moving across the screen.
// CLICK MINIGAME
// A simple game where the user must click on a horizontally scrolling block
int xPos=0; // holds the x position of the square
int yPos=(int)random(height); // holds the y position of the square
int score=0; // keeps track of the score
void setup()
{
size(100,100);
// determines the speed at which the screen gets refreshed
framerate(100);
}
void loop()
{
// redraws the background every time - no trails
background(0);
// increments the x position variable, and uses modulo to keep it scrolling
xPos=(xPos+1) % width;
// a conditional that checks if the box is at the edge of the screen
// if it is, then set the yPos to a random number
if (xPos%width == (width-1)) {
yPos = (int)random(height);
}
// checks to see if the mouse is pressed
if(mousePressed) {
// if the mouse coordinates are inside the moving square, increment the score, print
// it out, and reset the background for feedback
if ((mouseX > xPos && mouseX < xPos+10) && (mouseY > yPos && mouseY < yPos+10) ){
background(255);
score++;
println(score);
}
}
// draw the rectangle
rect(xPos, yPos, 10, 10);
}
- Use a float variable to keep track of the speed of the block instead of the frame rate.
- Vary the speed at which the block travels across the screen.
- Create a global boolean variable called gameComplete, and initialize it to 'false.'
- Make it so that after 30 seconds, the game stops and blocks no longer scroll across the screen. Use your gameComplete variable.
- Create a timebar (or some other representation of time) that shows the player how much time they have left.