微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

点击Pygame时增加金钱

如何解决点击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())

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。