Created
October 25, 2018 07:29
-
-
Save dinarname/4f304ed095dfa630c39aabfb11d99552 to your computer and use it in GitHub Desktop.
Assignment to consolidate the previous lesson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void setup() { | |
size(800, 800); | |
} | |
//Данные для рисования дома | |
// >>> Вводим переменные: ширина и высота дома | |
// .. Высота дома высчитывается от ширины в «золотой пропорции» | |
float houseWidth = 150; | |
float houseHeight = houseWidth / 1.618033981; | |
// >>> Начальные координаты для дома | |
float x = 200; | |
float y = 200; | |
// >>> Координаты и скорость для персонажа | |
int characterX = 500, characterY = 500, speed = 5; | |
void draw() { | |
background(#C5E5B7); | |
// >> Рисуем персонажа | |
fill(#73C2E8); | |
noStroke(); | |
ellipse(characterX, characterY, 50, 20); | |
ellipse(characterX, characterY, 20, 50); | |
fill(#3982A5); | |
ellipse(characterX, characterY, 30, 30); | |
// >> Управление персонажем с клавиатуры | |
if (keyPressed == true) { | |
if (key == 'w') { | |
characterY -= speed; | |
} else if (key == 's') { | |
characterY += speed; | |
} else if (key == 'd') { | |
characterX += speed; | |
} else if (key == 'a') { | |
characterX -= speed; | |
} | |
} | |
// >> Переброс на другую сторону экрана при достижении краёв экрана | |
if (characterX >= width) { | |
characterX = 1; | |
} else if (characterX <= 0) { | |
characterX = width - 1; | |
} | |
if (characterY >= height) { | |
characterY = 1; | |
} else if (characterY <= 0) { | |
characterY = height - 1 ; | |
} | |
// >>> Выбираем цвет дома и рисуем стену | |
stroke(0); | |
strokeWeight(5); | |
fill(#E8B07C); | |
rect(x, y, houseWidth, houseHeight); | |
// >>> Выбираем цвет и ширину линии, чтобы нарисовать окна | |
// .. В рисовании окон используем координаты и размеры дома | |
fill(#9ACCCE); | |
strokeWeight(5); | |
rect(x + 10, y + 10, houseWidth / 3.2, houseHeight / 2); | |
rect(x + houseWidth - 10 - houseWidth / 3.2, y + 10, houseWidth / 3.2, houseHeight / 2); | |
// >> Рисуем трубу | |
fill(#795F40); | |
rect(x + houseWidth - houseWidth / 5, y - houseHeight / 3, houseWidth / 10, houseHeight / 3); | |
// >> Проверка на попадание в дом | |
if (characterX >= x && characterX <= x+ houseWidth && characterY >= y && characterY <= y + houseHeight) { | |
fill(#3982A5); | |
textSize(30); | |
text("Welcome home!", width / 3, height / 2); | |
textSize(20); | |
text("Press space to start a new game.", width / 3, height / 2 + 30); | |
// >>> При нажатии на пробел дом будет появлятся в случайном месте на экрана. | |
if (keyPressed == true && key == ' ') { | |
x = random(0, width - houseWidth); | |
y = random(0, height - houseHeight); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment