Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pong
#1
I am making a pong game and I have most of the code correct except the last part. What I'm having a problem with is keeping a score. This is the code for the score. My thought process when I wrote the code was this, start both scores at 0 then if the puck.xcor is greater than the paddle, player1 score + 1 same with player 2 then I integrated it with update so it would call it every couple milliseconds. When I hit run, it just prints out zeros and it doesn't change when the puck goes behind the paddle. If there's something I'm missing or something I'm doing wrong, please feel free to tell me.

player_1_score = 0
player_2_score = 0


def check_score():
    if puck.xcor() > paddle1.xcor():
        player_1_score + 1

    elif puck.xcor() > paddle2.xcor():
        player_2_score + 1


def update():
    global dx, dy
    x = puck.xcor()
    y = puck.ycor()
    if math.fabs(x) > edge or check_collided(paddle1) or check_collided(paddle2):
        dx = -dx
    elif math.fabs(y) > edge:
        dy = -dy
    check_score()
    puck.goto(x + dx, y + dy)
    print ("player_1:" and player_1_score)
    print ("player_2:" and player_2_score)
    screen.ontimer(update, 10)


screen.onkey(right_up, "Up")
screen.onkey(right_down, "Down")
screen.onkey(left_up, "1")
screen.onkey(left_down, "2")
screen.listen()
update()
turtle.done()
Reply
#2
There is not enough code here to tell much, but player_X_score in check_score() is created in, and therefore local to the function (is garbage collected when the function exits). You pass the current score to the function, and the function returns the updated score http://www.tutorialspoint.com/python/pyt...ctions.htm
def check_score(player_1_score, player_2_score):
    if puck.xcor() > paddle1.xcor():
        player_1_score + 1
 
    elif puck.xcor() > paddle2.xcor():
        player_2_score + 1

    return player_1_score, player_2_score    

player_1_score, player_2_score = check_score(player_1_score, player_2_score)
Reply
#3
I put down this code, when i hit run it says this
Error:
File "/Users/DarkSideMoses/Documents/Programmin/Pong/Main_Code.py", line 115, in update check_score() TypeError: check_score() takes exactly 2 arguments (0 given)
player_1_score = 0
player_2_score = 0


def check_score(player_1_score, player_2_score):
    if puck.xcor() > paddle1.xcor():
        player_1_score + 1

    elif puck.xcor() > paddle2.xcor():
        player_2_score + 1

    return player_1_score, player_2_score


player_1_score, player_2_score = check_score(player_1_score, player_2_score)


def update():
    global dx, dy
    x = puck.xcor()
    y = puck.ycor()
    if math.fabs(x) > edge or check_collided(paddle1) or check_collided(paddle2):
        dx = -dx
    elif math.fabs(y) > edge:
        dy = -dy
    check_score()
    puck.goto(x + dx, y + dy)
    print ("player_1:" and player_1_score)
    print ("player_2:" and player_2_score)
    screen.ontimer(update, 10)


screen.onkey(right_up, "Up")
screen.onkey(right_down, "Down")
screen.onkey(left_up, "1")
screen.onkey(left_down, "2")
screen.listen()
update()
turtle.done()
Reply
#4
The error message says there is an error on line #115. The code you posted only has 39 lines. But none the less, the error states specifically what is wrong.
Reply
#5
What is a argument in python? It says line 115 cause theres more code then what I put, but I just gave you the part where the error was.
Reply
#6
Read the error message carefully... it says that you are calling check_score without arguments in the line 115, that is the line 26 of the extract of code you have copied.
You need to repeat the same expression as in the line 15 of the copied code.
Reply
#7
Quote:What is a argument in python?
2nd hit at
https://www.ecosia.org/search?q=What+is+...+in+python+
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020