Gönderdiğiniz kod parçacığı biraz kafa karıştırıcı. Birincisi, sözdizimi çok doğru değil, belki böyle bir şey geliyordu: Bir resim olarak ikili dize deseni görüntülemek istediğinizde ayrı bir resim yüklüyorsanız neden
class Enemy extends SpaceShip {
PImage img;
Enemy(int xpos, int ypos) {
x = xpos;
y = ypos;
//assuming sprite is inherited from SpaceShip ?
sprite = new String[5];
sprite[0] = "1011101";
sprite[1] = "0101010";
sprite[2] = "1111111";
sprite[3] = "0101010";
sprite[4] = "1000001";
//you are loading an image, but I see no use of the sprite
img = loadImage("image.png");
}
void draw() {
image(img, x, y);
}
}
Belli değil. Doğru bir şekilde anlarsam, ikili dize temsilini bir PImage'a dönüştürmek istersiniz. Eğer 0'a
beklediğiniz nerede 1 ve
color(0)
olduğunu beklediğiniz
color(255)
kullanılır
PImage sprite = new PImage(7,5,RGB);
sprite.pixels = new int[]{color(255),color(0),color(255),color(255),color(255),color(0),color(255),
color(0),color(255),color(0),color(255),color(0),color(255),color(0),
color(255),color(255),color(255),color(255),color(255),color(255),color(255),
color(0),color(255),color(0),color(255),color(0),color(255),color(0),
color(255),color(0),color(0),color(0),color(0),color(0),color(255)};
sprite.updatePixels();
noSmooth();
image(sprite,0,0,70,50);
Dikkat: doğrudan böyle imajınızı yazabilmesi için
doğrudan, PImage en pixels
erişebilir
PImage fromBinaryStrings(String[] sprite){
int h = sprite.length;
int w = sprite[0].length();
PImage result = new PImage(w,h,RGB);
for(int y = 0 ; y < h; y++){
String row = sprite[y];
for(int x = 0; x < w; x++){
if(row.charAt(x) == '1'){
result.set(x,y,color(255));
}else{
result.set(x,y,color(0));
}
}
}
return result;
}
:
Alternatif olarak, String[]
ve PImage piksellerini ayarlamak için her satırın her bir karakteri geçebilirler
Temel tanıtım:
void setup(){
noSmooth();
scale(10);
Enemy e = new Enemy(0,0);
e.draw();
}
PImage fromBinaryStrings(String[] sprite){
//determine height: number of lines
int h = sprite.length;
//determine width: length of a line
int w = sprite[0].length();
//create an image
PImage result = new PImage(w,h,RGB);
//traverse rows
for(int y = 0 ; y < h; y++){
//access row string
String row = sprite[y];
//traverse cols
for(int x = 0; x < w; x++){
//check each char's value and set the color accordingly
if(row.charAt(x) == '1'){
result.set(x,y,color(255));
}else{
result.set(x,y,color(0));
}
}
}
return result;
}
class SpaceShip{
int x,y;
String[] sprite;
}
class Enemy extends SpaceShip {
PImage img;
Enemy(int xpos, int ypos) {
x = xpos;
y = ypos;
sprite = new String[5];
sprite[0] = "1011101";
sprite[1] = "0101010";
sprite[2] = "1111111";
sprite[3] = "0101010";
sprite[4] = "1000001";
img = fromBinaryStrings(sprite);
}
void draw() {
image(img, x, y);
}
}
Lütfen çalışabileceğimiz bir [mcve] belirtin. Bunun bütün taslağınızın olmaması gerektiğini unutmayın, sadece bir örnek. Örneğin, sadece bir görüntüyü görüntülüyorsanız, oynatıcıyı taşımak veya mermi çekmek için herhangi bir koda gerek yoktur. Ayrıca, aldığınız hataların tam metnini lütfen gönderin. Gerekiyorsa ekran görüntüleri ekleyin. –