Untitled
unknown
plain_text
2 years ago
4.4 kB
5
Indexable
import time import turtle import json import pathlib import os score = 0 filename = str(pathlib.Path(__file__).parent.resolve()) path = os.path.join(filename, "score.json") bg = turtle.Screen() bg.title("Brick breaker") bg.bgcolor("black") bg.setup(width=620, height=800) bg.tracer(0) # menghaluskan gerak bola ball = turtle.Turtle() ball.shape("circle") ball.color("white") ball.penup() # menghilangkan garis pada bola ball.goto(0, -200) # titik mulai ball.dy = 3 ball.dx = 3 sq = turtle.Turtle() sq.shape("square") sq.color("white") sq.penup() sq.goto(0, -360) sq.turtlesize(1, 6, 1) bricks = [] def setBrickPosisi(brick, x, y): brick.goto(x, y) # Membuat brick statis statisBrickPosisi = [ (250, 300), (150, 300), (50, 300), (-50, 300), (-150, 300), (-250, 300), (250, 230), (150, 230), (50, 230), (-50, 230), (-150, 230), (-250, 230), (250, 160), (150, 160), (50, 160), (-50, 160), (-150, 160), (-250, 160), (250, 90), (150, 90), (50, 90), (-50, 90), (-150, 90), (-250, 90), (250, 20), (150, 20), (50, 20), (-50, 20), (-150, 20), (-250, 20), ] # posisi statis untuk masing-masing brick for position in statisBrickPosisi: # membuat brick dalam posisi itu brick = turtle.Turtle() brick.shape("square") brick.color("red") brick.penup() brick.turtlesize(1.5, 3, 1) setBrickPosisi(brick, *position) bricks.append(brick) def PlayerGoLeft(): x = sq.xcor() if x >= -230: x -= 30 sq.setx(x) def PlayerGoRight(): x = sq.xcor() if x <= 230: x += 30 sq.setx(x) def pantulanBola(): if ( sq.xcor() - 70 < ball.xcor() < sq.xcor() + 70 and sq.ycor() - 30 < ball.ycor() < sq.ycor() + 30 ): return True else: return False def scoreDisplay(): #membuat objek score SD = turtle.Turtle() SD.color("white") SD.penup() SD.hideturtle() SD.goto(0, 350) SD.write(f"Score: {score}", align="center", font=("Courier", 24, "normal")) return SD def updateScore(points): #mengupdate skor global score score += points scoreDisplay.clear() # Membersihkan teks skor sebelumnya scoreDisplay.write(f"Score: {score}", align="center", font=("Courier", 24, "normal")) def tabrakBola(bola, brick): distance = bola.distance(brick) return distance < (max(bola.shapesize()) + max(brick.shapesize())) * 20 / 2 def inputNama(): playerInput = turtle.textinput("Brick Breaker", "Masukkan namamu:") print(f"Hello, {playerInput}! Let's start the game!") return playerInput # print(os.path.exists(path)) #print true atau falsenya file json playerInput = inputNama() playerData = {} if os.path.exists(path) and os.stat(path).st_size != 0: with open(path, "r") as jsonFile: playerData = json.load(jsonFile) if playerInput not in playerData: #cek apakah player sudah ada di dalam json playerData[playerInput] = { "score": 0, } scoreDisplay = scoreDisplay() bg.listen() bg.onkeypress(PlayerGoLeft, "a") bg.onkeypress(PlayerGoLeft, "Left") bg.onkeypress(PlayerGoRight, "d") bg.onkeypress(PlayerGoRight, "Right") with open(path, "w") as jsonFile: json.dump(playerData, jsonFile, indent=3) while len(bricks) > 0: bg.update() time.sleep(0.01) # kecepatan gerak bola ball.setx(ball.xcor() + ball.dx) ball.sety(ball.ycor() + ball.dy) for brick in bricks: if tabrakBola(ball, brick): brick.hideturtle() # menghilangkan brick jika terkena bola bricks.remove(brick) ball.dy *= -1 updateScore(500) if ball.xcor() >= 270 or ball.xcor() <= -280: ball.dx *= -1 if ball.ycor() >= 380 or ball.ycor() <= -380: ball.dy *= -1 if pantulanBola(): ball.dy *= -1 if ball.ycor() <= -380: playerData[playerInput]["score"] += score #update score di data player jika kalah with open(path, "w") as jsonFile: json.dump(playerData, jsonFile, indent=3) print("Game over!") turtle.bye() playerData[playerInput]["score"] += score #update score di data player jika menang with open(path, "w") as jsonFile: json.dump(playerData, jsonFile, indent=3)
Editor is loading...
Leave a Comment