从哪里开始向游戏循环添加图形 [英] Where to start with adding graphics to a game loop

查看:110
本文介绍了从哪里开始向游戏循环添加图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个基本的游戏循环。我的来源是Java中的KillerGameProgramming和YouTutbe上的foriegnguymike。所以我理解在这里添加双缓冲图形的最正确和最正确的方法是什么。 (假设我有一个带有JFrame的主类)



请考虑我是高级编程新手这一事实(意思是我可以制作没有游戏循环的基本游戏,但需要使用游戏循环)。另请参阅Java中的Killer Game Programming一书中的章节或内容。我在使用这本书时遇到了麻烦。(也有理由不正确的程序拼写我必须快速输入,因为我的代码在我的笔记本电脑上):



So i have a basic game loop. My source is KillerGameProgramming in Java and foriegnguymike on YouTutbe. So I understand what's the most proper and correct way to add Double buffer graphics in here. (Assuming I have A Main class with a JFrame)

Please consider the fact that I'm new to advanced programming(Meaning I can make a basic game without game loop, but need to use game loop). Also refer to chapters or things from the book Killer Game Programming in Java. I'm having trouble with the book.(Also excuse incorrect program spelling I had to quickly type it since my code is on my laptop):

    public class GamePanel extends JPanel implements Runnable {
    
    private Boolean running = false;

    private Thread thread;

     public GamePanel() {
     setFocusable(true);
     requestFocus();
     start();

   }
        public void init() {}

     public void start() {}
     public void stop() {}

     public void run(){
     running = true;
     while(running) {
     render();
     paint();
     
      }
   }
          public void render() {} //////// WHAT WOULD I DO TO START WITH GRAPHICS? /////


   }

     public void paint() {
     repaint(); ///////// WHAT SHOULD BE CALLED HERE? ////////
}

推荐答案

碰巧我也是用Java制作我的第一个游戏,所以我会尽力帮助你。我还没看过那本书。我的大部分知识都来自于在论坛上玩耍和阅读。



我不明白你的问题。你需要游戏循环或渲染方面的帮助吗?我猜它是关于渲染看你的代码。



因为你正在使用JFrame来绘制图形(据我所知,这是这是最好的去处。我也在使用它。)我建议覆盖protected void paintComponent(Graphics g)方法。将所有绘图代码放在那里,在游戏循环中简单地调用repaint();



在paintComponent方法中,您可以开始绘制图形。如果你遇到这个问题,我会嘲笑你玩一些演示项目来解决这个问题。



下面是一些导入png并在JPanel上绘制的代码:



It just so happens that I'm also making my first game in Java so I'll try to help you out as much as I can. I haven't read that book yet though. Most of my knowledge is from playing around and reading on forums.

I don't understand your question though. Do you need help with the game loop or with the rendering? I'm guessing it's about the rendering looking at your code.

Since you're using a JFrame to draw your graphics on (as far as I know this is the best place to do it. I'm using the same.) I recommend overriding the "protected void paintComponent(Graphics g)" method. Place all your drawing code in there and in your game loop simply call "repaint();"

In the "paintComponent" method you can start drawing your graphics. If you're having trouble with this I sagest that you play around with some demo projects to get the hang of this.

Here is some code to import a png and draw it on the JPanel:

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Driver extends JPanel
{
	JFrame frame;
	Image img;
	BufferedImage backBuffer;
	
	public static void main(String[] args)
	{
		Driver drive = new Driver();
		drive.initialize();
	}
	
	private void initialize()
	{
		frame = new JFrame();
		frame.setTitle("Main Menu");
		frame.setSize(816, 638);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocation(500, 0);
		frame.setLayout(new BorderLayout());
		frame.add(this);
		 	 	
		try
		{
			img = ImageIO.read(new File("pick.png"));
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	 	 	
		frame.setVisible(true);
		
		backBuffer = new BufferedImage( this.getSize().width, this.getSize().height, BufferedImage.TYPE_INT_RGB);
	}
	
	@Override
	protected void paintComponent(Graphics g)
	{
		Graphics bbg = backBuffer.getGraphics();
		
		int displayX = 10;		// Create the image 10px from Left
		int displayY = 10;		// Create the image 10px from Top
		int displayWidth = 20;	// Stretch/shrink the image to 20px in width (You can leave this out in the drawImage. I use it for zooming in my game)
		int displayHeight = 20;	// Stretch/shrink the image to 20px in height (You can leave this out in the drawImage. I use it for zooming in my game)
		
		bbg.drawImage(img, displayX, displayY, displayWidth, displayHeight, this);
		
		g.drawImage(backBuffer, 0, 0, this); 
	}
}





我希望这会对你有所帮助。

祝你的游戏好运。我希望有一天我能玩它:)



I hope this helps you.
Best of luck with your game. I hope I'll be able to play it some day :)


这篇关于从哪里开始向游戏循环添加图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆