点击Pygame时增加金钱

问题描述

import pygame
pygame.init()
cpsecond = open("clickpersecond.txt","r+")
cps = cpsecond.read()
cointotal = open("totalcoin.txt","r+")
totalcoin = cointotal.read()

while True: # main game loop
...

    if event.type == MOUSEBUTTONDOWN:
        totalcoin += cps
        print("Your current coin is",totalcoin,end="\r")

pygame.display.flip()
clock.tick(30)

当我点击时,它不会增加硬币,它只会增加它附近的数字,例如:

#Current Coin = 0

<<<Your current coin is 01 #1st Click

<<<Your current coin is 011 #2nd Click

<<<Your current coin is 0111 #3rd Click

<<<Your current coin is 01111 #4th Click

#0 = your current money
#1 = increases your money by +1

我想让当你点击时,它会增加你的钱,你可以把它花在游戏中。

解决方法

cpstotalcoin 是字符串。使用 int(x) 将字符串转换为整数值:

cpsecond = open("clickpersecond.txt","r+")
cps = int(cpsecond.read())

cointotal = open("totalcoin.txt","r+")
totalcoin = int(cointotal.read())