这是我朋友的游戏,我们似乎无法通过浮动解决此问题

时间:2019-12-08 15:31:59

标签: pygame pycharm

我们遇到的错误是:

File "C:/Games/nyuszi/game.py", line 51, in <module>
   for x in range(width/grass.get_width() // 1):
TypeError: 'float' object cannot be interpreted as an integer

这是我的新手,所以不要自大。

我们需要紧急情况帮助,我们还有1天的时间来解决此问题。

# 1 - Import library
import pygame
from pygame.locals import *
import math
import random

# 2 - Initialize the game
pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
keys = [False, False, False, False]
playerpos=[100,100]
acc=[0,0]
arrows=[]
badtimer=100
badtimer1=0
badguys=[[640,100]]
healthvalue=194
pygame.mixer.init()

# 3 - Load image
player = pygame.image.load("resources/images/dude.png")
grass = pygame.image.load("resources/images/grass.png")
castle = pygame.image.load("resources/images/castle.png")
arrow = pygame.image.load("resources/images/bullet.png")
badguyimg1 = pygame.image.load("resources/images/badguy.png")
badguyimg=badguyimg1
healthbar = pygame.image.load("resources/images/healthbar.png")
health = pygame.image.load("resources/images/health.png")
gameover = pygame.image.load("resources/images/gameover.png")
youwin = pygame.image.load("resources/images/youwin.png")
# 3.1 - Load audio
hit = pygame.mixer.Sound("resources/audio/explode.wav")
enemy = pygame.mixer.Sound("resources/audio/enemy.wav")
shoot = pygame.mixer.Sound("resources/audio/shoot.wav")
hit.set_volume(0.05)
enemy.set_volume(0.05)
shoot.set_volume(0.05)
pygame.mixer.music.load('resources/audio/moonlight.wav')
pygame.mixer.music.play(-1, 0.0)
pygame.mixer.music.set_volume(0.25)

# 4 - keep looping through
running = 1
exitcode = 0
while running:
    badtimer-=1
    # 5 - clear the screen before drawing it again
    screen.fill(0)
    # 6 - draw the player on the screen at X:100, Y:100
    for x in range(width/grass.get_width()+1):
        for y in range(height/grass.get_height()+1):
            screen.blit(grass,(x*100,y*100))
    screen.blit(castle,(0,30))
    screen.blit(castle,(0,135))
    screen.blit(castle,(0,240))
    screen.blit(castle,(0,345 ))

1 个答案:

答案 0 :(得分:0)

range的参数必须是整数值。 /(除法)运算符的结果可以是浮点值。请改用//(楼层划分)运算符:
(请参见Binary arithmetic operations

for x in range(width // grass.get_width()+1):
    for y in range(height // grass.get_height()+1):
        screen.blit(grass,(x*100,y*100))

或者,您可以通过intround将结果转换为整数值。例如:

for x in range(int(width / grass.get_width()) + 1):
    for y in range(int(height / grass.get_height()) + 1):
        screen.blit(grass,(x*100,y*100))