William Crusader
Hectopat
- Katılım
- 11 Kasım 2020
- Mesajlar
- 5.371
- Makaleler
- 1
- Çözümler
- 14
Daha fazla
- Cinsiyet
- Erkek
- Meslek
- Yok
Javascript ile basit bir clicker oyunu yaptım. Ve oyuna "ekstra tıklama" denen bir şey ekledim. Fakat 5 tane buton için ayrı ayrı kod yazmaktansa hepsini bir function() içine sığdırmayı istiyorum, daha optimize olması için. Bunu nasıl yapabilirim?
Gerekli kodlar:
JS:
[CODE lang="javascript" title="JavaScript"]let clickCount = 0;
// tıklama sayısını kontrol eder
let clickBoost = 0;
// ekstra tıklamaları kontrol eder
document.getElementById("clickButton").onclick = function(){
clickCount += 1;
document.getElementById("clickCounter").innerHTML = clickCount;
if (clickBoost >= 1){
clickCount = clickCount + clickBoost;
}
}
// "Click" tuşunun kodları
document.getElementById("boostButton1").onclick = function(){
if (clickCount > 100){
let yorn = prompt("Do you wanna buy 1 click boost?" + "\n" + "\n " + "(Y/N)")
if (yorn == "y" || yorn == "Y"){
clickBoost = clickBoost += 1;
alert("Bought Succesfully!")
clickCount = clickCount -= 100
}
else {
alert("You didn't bought extra clicks.")
}
}
else{
alert("You don't have enough clicks!")
}
}
// "Buy 1 Click Boost" tuşunun kodları
document.getElementById("boostButton10").onclick = function () {
if (clickCount > 1000) {
let yorn = prompt("Do you wanna buy 10 click boost?" + "\n" + "\n " + "(Y/N)")
if (yorn == "y" || yorn == "Y") {
clickBoost = clickBoost += 10;
alert("Bought Succesfully!")
clickCount = clickCount -= 1000
} else {
alert("You didn't bought extra clicks.")
}
}
else {
alert("You don't have enough clicks!")
}
}
// "Buy 10 Click Boost" tuşunun kodları
document.getElementById("boostButton100").onclick = function () {
if (clickCount > 10000) {
let yorn = prompt("Do you wanna buy 100 click boost?" + "\n" + "\n " + "(Y/N)")
if (yorn == "y" || yorn == "Y") {
clickBoost = clickBoost += 100;
alert("Bought Succesfully!")
clickCount = clickCount -= 10000
} else {
alert("You didn't bought extra clicks.")
}
}
else {
alert("You don't have enough clicks!")
}
}
// "Buy 100 Click Boost" tuşunun kodları
document.getElementById("boostButton1000").onclick = function () {
if (clickCount > 100000) {
let yorn = prompt("Do you wanna buy 1000 click boost?" + "\n" + "\n " + "(Y/N)")
if (yorn == "y" || yorn == "Y") {
clickBoost = clickBoost += 1000;
alert("Bought Succesfully!")
clickCount = clickCount -= 100000
} else {
alert("You didn't bought extra clicks.")
}
}
else {
alert("You don't have enough clicks!")
}
}
// "Buy 1000 Click Boost" tuşunun kodları
document.getElementById("boostButton10000").onclick = function () {
if (clickCount > 1000000) {
let yorn = prompt("Do you wanna buy 10000 click boost?" + "\n" + "\n " + "(Y/N)")
if (yorn == "y" || yorn == "Y") {
clickBoost = clickBoost += 10000;
alert("Bought Succesfully!")
clickCount = clickCount -= 1000000
} else {
alert("You didn't bought extra clicks.")
}
}
else {
alert("You don't have enough clicks!")
}
}
// "Buy 10000 Click Boost" tuşunun kodları[/CODE]
HTML:
#clickCounter{
display: block;
text-align: center;
font-size: 150px;
}
#clickButton{
display: flex;
text-align: center;
font-size: 50px;
}
#boostButton1{
display: blox;
text-align: center;
font-size: 50px;
}
#boostButton10{
display: blox;
text-align: center;
font-size: 50px;
}
#boostButton100{
display: blox;
text-align: center;
font-size: 50px;
}
#boostButton1000{
display: blox;
text-align: center;
font-size: 50px;
}
#boostButton10000{
display: blox;
text-align: center;
font-size: 50px;
}
Not: Son kod CSS.
Gerekli kodlar:
JS:
[CODE lang="javascript" title="JavaScript"]let clickCount = 0;
// tıklama sayısını kontrol eder
let clickBoost = 0;
// ekstra tıklamaları kontrol eder
document.getElementById("clickButton").onclick = function(){
clickCount += 1;
document.getElementById("clickCounter").innerHTML = clickCount;
if (clickBoost >= 1){
clickCount = clickCount + clickBoost;
}
}
// "Click" tuşunun kodları
document.getElementById("boostButton1").onclick = function(){
if (clickCount > 100){
let yorn = prompt("Do you wanna buy 1 click boost?" + "\n" + "\n " + "(Y/N)")
if (yorn == "y" || yorn == "Y"){
clickBoost = clickBoost += 1;
alert("Bought Succesfully!")
clickCount = clickCount -= 100
}
else {
alert("You didn't bought extra clicks.")
}
}
else{
alert("You don't have enough clicks!")
}
}
// "Buy 1 Click Boost" tuşunun kodları
document.getElementById("boostButton10").onclick = function () {
if (clickCount > 1000) {
let yorn = prompt("Do you wanna buy 10 click boost?" + "\n" + "\n " + "(Y/N)")
if (yorn == "y" || yorn == "Y") {
clickBoost = clickBoost += 10;
alert("Bought Succesfully!")
clickCount = clickCount -= 1000
} else {
alert("You didn't bought extra clicks.")
}
}
else {
alert("You don't have enough clicks!")
}
}
// "Buy 10 Click Boost" tuşunun kodları
document.getElementById("boostButton100").onclick = function () {
if (clickCount > 10000) {
let yorn = prompt("Do you wanna buy 100 click boost?" + "\n" + "\n " + "(Y/N)")
if (yorn == "y" || yorn == "Y") {
clickBoost = clickBoost += 100;
alert("Bought Succesfully!")
clickCount = clickCount -= 10000
} else {
alert("You didn't bought extra clicks.")
}
}
else {
alert("You don't have enough clicks!")
}
}
// "Buy 100 Click Boost" tuşunun kodları
document.getElementById("boostButton1000").onclick = function () {
if (clickCount > 100000) {
let yorn = prompt("Do you wanna buy 1000 click boost?" + "\n" + "\n " + "(Y/N)")
if (yorn == "y" || yorn == "Y") {
clickBoost = clickBoost += 1000;
alert("Bought Succesfully!")
clickCount = clickCount -= 100000
} else {
alert("You didn't bought extra clicks.")
}
}
else {
alert("You don't have enough clicks!")
}
}
// "Buy 1000 Click Boost" tuşunun kodları
document.getElementById("boostButton10000").onclick = function () {
if (clickCount > 1000000) {
let yorn = prompt("Do you wanna buy 10000 click boost?" + "\n" + "\n " + "(Y/N)")
if (yorn == "y" || yorn == "Y") {
clickBoost = clickBoost += 10000;
alert("Bought Succesfully!")
clickCount = clickCount -= 1000000
} else {
alert("You didn't bought extra clicks.")
}
}
else {
alert("You don't have enough clicks!")
}
}
// "Buy 10000 Click Boost" tuşunun kodları[/CODE]
HTML:
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>oyun.. heralde</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<label id= "clickCounter">0</label><br>
<center><button id= "clickButton">Click</button></center>
<center><button id="boostButton1">Buy 1 Click Boost</button></center>
<center><button id="boostButton10">Buy 10 Click Boost</button></center>
<center><button id="boostButton100">Buy 100 Click Boost</button></center>
<center><button id="boostButton1000">Buy 1000 Click Boost</button></center>
<center><button id="boostButton10000">Buy 10000 Click Boost</button></center>
<script src="index.js"></script>
<
</body>
</html>
display: block;
text-align: center;
font-size: 150px;
}
#clickButton{
display: flex;
text-align: center;
font-size: 50px;
}
#boostButton1{
display: blox;
text-align: center;
font-size: 50px;
}
#boostButton10{
display: blox;
text-align: center;
font-size: 50px;
}
#boostButton100{
display: blox;
text-align: center;
font-size: 50px;
}
#boostButton1000{
display: blox;
text-align: center;
font-size: 50px;
}
#boostButton10000{
display: blox;
text-align: center;
font-size: 50px;
}
Not: Son kod CSS.