首页 > 解决方案 > 如何在游戏中实现图形而不是使用像素点?

问题描述

我的游戏对象 Cat 工作得非常好,但我正在尝试包含更多图形。因此,我想使用的不仅仅是一个类似于猫的点,而是一个图像。

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

public class Cat extends GameObject{
        
        private float _acc=1f;
        private float _dcc=0.5f;
        private KeyInput input;
        
        
        public Cat(int x , int y , ID id , KeyInput input )
        {
                super(x,y,id);
                this.input=input;
        }

        @Override
        public void tick() 
        {
                y-=velY;
                
                //Vertical movement
                if(input.keys[2]) velY -= _acc;
                if(input.keys[3]) velY += _acc;
                else if(!input.keys[3] && !input.keys[2])
                {
                        velY=2;
                }
                
                velY = clamp(velY,3,1);
        }

        @Override
        public void render(Graphics g) 
        {
               //this part draws a dot that resembles a Cat
                g.setColor(Color.red);
                g.fillRect((int)x,(int)y,40,40);
        }
        
        private float clamp(float vel,float max,float min )
        {
                if(vel>max)return max;
                if(vel<min)return min;
                return vel;
        }
        
        
}

标签: javaoopuser-interfacegraphicsgame-development

解决方案


推荐阅读