Java Game Programming Wizard Top Down Shooter Part 7

preview_player
Показать описание


• Course Selection •

• Individual Game Design Courses •

Have fun learning!

Рекомендации по теме
Комментарии
Автор

I've never been this eager to continue a series, let alone a gave dev tutorial. The way to teach is compelling like you make it easy to understand. Thank you a lot for these, they've helped tremendously.

AdaptiveArtisian
Автор

Keep up the good work man, my friend and I follow your tutorials and it's allowing us to make games. We know the basics but your tutorials actually let us make start to finish programs!

curtisreaves
Автор

Below is my modified player class. I changed the collision to a clamping system and gave the PC four different bounds to check for collision (top, bottom, left, right). This way there is no sticking AND IT'S PIXEL PERFECT :D Yay!!!!


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

public class Player extends GameObject { //Simple player subclass for user-controlled game objects

Handler handler;

public Player(int x, int y, ID id, Handler handler) {
super(x, y, id);
this.handler = handler;
}


public void tick() {



x += velX;
y += velY;

collision();


//movement
if(handler.isUp()) velY = -5;
else if (!handler.isDown()) velY = 0;

if(handler.isDown()) velY = 5;
else if (!handler.isUp()) velY = 0;

if(handler.isRight()) velX = 5;
else if (!handler.isLeft()) velX = 0;

if(handler.isLeft()) velX = -5;
else if (!handler.isRight()) velX = 0;
}

private void collision() { //You can fix the sticking by creating separate floor and wall objects

for(int i = 0; i < handler.object.size(); i++) {

GameObject tempObject = handler.object.get(i);

if(tempObject.getId() == ID.Block) {

{

= (tempObject.getX()+32);


}

if {

= (tempObject.getX()-32);
}

if {

= (tempObject.getY()+32);
}

if {

= (tempObject.getY()-48);
}


}
}
}

public void render(Graphics g) { //describes how the player object will be rendered in the game window
g.setColor(Color.blue);
g.fillRect(x+5, y, 22, 10);
g.fillRect(x+5, y+38, 22, 10);
g.fillRect(x, y+5, 10, 38);
g.fillRect(x+22, y+5, 10, 38);
}


public Rectangle getTopBounds() {

return new Rectangle(x+5, y, 22, 10);
}

public Rectangle getBotBounds() {

return new Rectangle(x+5, y+38, 22, 10);
}

public Rectangle getLeftBounds() {

return new Rectangle(x, y+5, 10, 38);
}

public Rectangle getRightBounds() {

return new Rectangle(x+22, y+5, 10, 38);
}



}

devindellazizzototh
Автор

Noticed on some of your videos you do alot of manual work, I recommend the following shortcuts in Eclipse:
Alt + Up(or Down) = Move the current line up or down
Ctrl + Alt + Up (or Down) = Duplicate the current line up or down
Ctrl + D = Delete the current line

Hughsie
Автор

here is a collision system that doesn't get stuck but it still isn't pixel perfect im too lazy to make it perfect here is the class the only thing is my class name is Player not wizard so you will need to change it feel free to use it if you want :)

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

public class Player extends GameObject{

Handler handler;

public Player(int x, int y, ID id, Handler handler) {
super(x, y, id);
this.handler = handler;
}

@Override
public void tick() {
collision();

x += VelX;
y += VelY;


if(handler.isUp()) VelY = -5;
else if (handler.isDown() == false) VelY = 0;

if(handler.isDown()) VelY = 5;
else if(handler.isUp() == false) VelY = 0;

if(handler.isRight()) VelX = 5;
else if(handler.isLeft() == false) VelX = 0;

if(handler.isLeft()) VelX = -5;
else if(handler.isRight() == false) VelX = 0;
}

public boolean place_free(int x, int y, Rectangle myRect, Rectangle otherRect) {
myRect.x = x;
myRect.y = y;
if {
return false;
}
return true;
}

public void collision(){
for (int i = 0; i < handler.object.size(); i++) {
GameObject tempObject = handler.object.get(i);

if (tempObject.getId() == ID.Block) {
if (!place_free((int) (x + VelX), y, getBounds(), tempObject.getBounds())) {
VelX = 0;
}

if (!place_free(x, (int) (y + VelY), getBounds(), tempObject.getBounds())) {
VelY = 0;
}
}
}
}

@Override
public void render(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(x, y, 32, 48);
}

@Override
public Rectangle getBounds() {
return new Rectangle(x, y, 32, 48);
}

}

AndrewMiko_
Автор

Whoa, you collision system is so much better than mine, and simpler. I used to set the position to the player on the edge. That caused so many bugs with narrow walls and I had to give them so many hitboxes. I feel bad now.

alexsere
Автор

First thanx briliant stuff...question if i move up or down blue box "player" disappear...don't get it...thaks for advise ;) let's keep coding ;)

richardtichy
Автор

I'm getting an error when I launch the game "Exception in thread "Thread-2"

Carteeeer
Автор

Can you do a tutorial series on making a game similar to SoMuchBlood?

thecodingpheonix
Автор

I am having an issue where the game randomly stops updating the frames/freezes when i try and move the players direction. I can move around fine for a random amount of time (between 10 seconds to 1 minute or so) and then i can no longer move the character. Theres no errors coming up in the console and i even tried moving around the keybindings for movement to see if that was the issue. If i changed the amountOfTicks in the run function in the game class to 30 it freezes quicker (under 5 seconds). Any idea on how to fix this? (also it seems to freeze at random times - but it tends to freeze more often when i hit a wall)

shmiggy
Автор

Will you add multiplayer to this game? Or would it just be singleplayer?

spiritwolf
Автор

For smoothly going along the walls, call after setting new x and new y:

private void kl) {
int pushback = 1;
for(GameObject obj : handler.getObjects()) {
if(obj instanceof Block) {
if {
// top line intersects with something above
while (topIntersectsObj(obj) && kl.keyUp())
y += pushback;
// bottom line intersects with something below
while (botIntersectsObj(obj) && kl.keyDown())
y -= pushback;
// left line intersects with something on left
while (lefIntersectsObj(obj) && kl.keyLeft())
x += pushback;
// right line intersects with something on right
while (rigIntersectsObj(obj) && kl.keyRight())
x -= pushback;
}
}
}
}
private boolean topIntersectsObj(GameObject obj) {
return obj.getBounds().contains(x+(w/2), y);
}
private boolean botIntersectsObj(GameObject obj) {
return obj.getBounds().contains(x+(w/2), y + h);
}
private boolean lefIntersectsObj(GameObject obj) {
return obj.getBounds().contains(x, y+(h/2));
}
private boolean rigIntersectsObj(GameObject obj) {
return obj.getBounds().contains(x + w, y+(h/2));
}

dstealth
Автор

the player get's stuck 100% of the time for me

EDIT: I fixed it. I just called collision before adding velX and velY to x and y

yaskokai