1q2w3easxz 发表于 2020-5-27 10:56:34

求大神解围!!!

就是#6的第2排“for x...”的什么的,用IDLE运行时报错了:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\bat&IDLE\W\game1.pyw", line 50, in <module>
    for x in range(width/grass.get_width()+1):
TypeError: 'float' object cannot be interpreted as an integer
就是这样的。
# 1 - Import library
import math
import pygame
from pygame.locals import *
import random

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

# 3 - Load Q
player = pygame.image.load("Q/dude.png")
grass = pygame.image.load("Q/grass.png")
castle = pygame.image.load("Q/castle.png")
badguyimg1 = pygame.image.load("Q/badguy.png")
badguyimg=badguyimg1
healthbar = pygame.image.load("Q/healthbar.png")
health = pygame.image.load("Q/health.png")
gameover = pygame.image.load("Q/gameover.png")
youwin = pygame.image.load("Q/youwin.png")
# 3.1 - Load Q
hit = pygame.mixer.Sound("Q/explode.wav")
enemy = pygame.mixer.Sound("Q/enemy.wav")
shoot = pygame.mixer.Sound("Q/shoot.wav")
hit.set_volume(0.05)
enemy.set_volume(0.05)
shoot.set_volume(0.05)
pygame.mixer.music.load('Q/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 screen elements
    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))
    screen.blit(player, playerpos)
    # 6.1 - Set player position and rotation
    position = pygame.mouse.get_pos()
    angle = math.atan2(position-(playerpos+32),position-(playerpos+26))
    playerrot = pygame.transform.rotate(player, 360-angle*57.29)
    playerpos1 = (playerpos-playerrot.get_rect().width/2, playerpos-playerrot.get_rect().height/2)
    screen.blit(playerrot, playerpos1)
    # 6.2 - Draw arrows
    for bullet in arrows:
      index=0
      velx=math.cos(bullet)*10
      vely=math.sin(bullet)*10
      bullet+=velx
      bullet+=vely
      if bullet<-64 or bullet>640 or bullet<-64 or bullet>480:
            arrows.pop(index)
      index+=1
      for projectile in arrows:
            arrow1 = pygame.transform.rotate(arrow, 360-projectile*57.29)
            screen.blit(arrow1, (projectile, projectile))
   # 6.3 - Draw badgers
    if badtimer==0:
      badguys.append()
      badtimer=100-(badtimer1*2)
      if badtimer1>=35:
            badtimer1=35
      else:
            badtimer1+=5
    index=0
    for badguy in badguys:
      if badguy<-64:
            badguys.pop(index)
      badguy-=7
    # 6.3.1 - Attack castle
      badrect=pygame.Rect(badguyimg.get_rect())
      badrect.top=badguy
      badrect.left=badguy
      if badrect.left<64:
            hit.play()
            healthvalue -= random.randint(5,20)
            badguys.pop(index)
    #6.3.2 - Check for collisions
      index1=0
      for bullet in arrows:
            bullrect=pygame.Rect(arrow.get_rect())
            bullrect.left=bullet
            bullrect.top=bullet
            if badrect.colliderect(bullrect):
                enemy.play()
                acc+=1
                badguys.pop(index)
                arrows.pop(index1)
            index1+=1
    # 6.3.3 - Next bad guy
      index+=1
    for badguy in badguys:
      screen.blit(badguyimg, badguy)
   # 6.4 - Draw clock
    font = pygame.font.Font(None, 24)
    survivedtext = font.render(str((90000-pygame.time.get_ticks())/60000)+":"+str((90000-pygame.time.get_ticks())/1000%60).zfill(2), True, (0,0,0))
    textRect = survivedtext.get_rect()
    textRect.topright=
    screen.blit(survivedtext, textRect)
    # 6.5 - Draw health bar
    screen.blit(healthbar, (5,5))
    for health1 in range(healthvalue):
      screen.blit(health, (health1+8,8))
# 7 - update the screen
    pygame.display.flip()
# 8 - loop through the events
    for event in pygame.event.get():
      # check if the event is the X button
      if event.type==pygame.QUIT:
            # if it is quit the game
            pygame.quit()
            exit(0)
      if event.type == pygame.KEYDOWN:
            if event.key==K_w:
                keys=True
            elif event.key==K_a:
                keys=True
            elif event.key==K_s:
                keys=True
            elif event.key==K_d:
                keys=True
      if event.type == pygame.KEYUP:
            if event.key==pygame.K_w:
                keys=False
            elif event.key==pygame.K_a:
                keys=False
            elif event.key==pygame.K_s:
                keys=False
            elif event.key==pygame.K_d:
                keys=False
            if event.type==pygame.MOUSEBUTTONDOWN:
                shoot.play()
                position=pygame.mouse.get_pos()
                cc+=1
                arrows.append(-(playerpos1+32),position-(playerpos1+26)),playerpos1+32,playerpos1+32])
# 9 - Move player
    if keys:
      playerpos-=5
    elif keys:
      playerpos+=5
    if keys:
      playerpos-=5
    elif keys:
      playerpos+=5
#10 - Win/Lose check
    if pygame.time.get_ticks()>=90000:
      running=0
      exitcode=1
    if healthvalue<=0:
      running=0
      exitcode=0
    if acc!=0:
      accuracy=acc*1.0/acc*100
    else:
      accuracy=0
# 11 - Win/lose display      
if exitcode==0:
    pygame.font.init()
    font = pygame.font.Font(None, 24)
    text = font.render("Accuracy: "+str(accuracy)+"%", True, (255,0,0))
    textRect = text.get_rect()
    textRect.centerx = screen.get_rect().centerx
    textRect.centery = screen.get_rect().centery+24
    screen.blit(gameover, (0,0))
    screen.blit(text, textRect)
else:
    pygame.font.init()
    font = pygame.font.Font(None, 24)
    text = font.render("Accuracy: "+str(accuracy)+"%", True, (0,255,0))
    textRect = text.get_rect()
    textRect.centerx = screen.get_rect().centerx
    textRect.centery = screen.get_rect().centery+24
    screen.blit(youwin, (0,0))
    screen.blit(text, textRect)
while 1:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
            pygame.quit()
            exit(0)
    pygame.display.flip()

Twilight6 发表于 2020-5-27 10:59:31

width/grass.get_width()
你除法后 返回的一定是 浮点型数值,而range 只能填int 整型类型的数你加上 int(width/grass.get_width()+1)即可:# 6 - draw the screen elements
    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))

heidern0612 发表于 2020-5-27 11:00:48

for x in range(width/grass.get_width()+1):
TypeError: 'float' object cannot be interpreted as an integer


错误提示都告诉你了,float类型不能被解释为整形,用//除试试。

1q2w3easxz 发表于 2020-5-27 11:00:54

本帖最后由 1q2w3easxz 于 2020-5-27 11:03 编辑

Twilight6 发表于 2020-5-27 10:59
你除法后 返回的一定是 浮点型数值,而range 只能填int 整型类型的数你加上 即可:

蟹蟹大佬,程序可以运行了

Twilight6 发表于 2020-5-27 11:01:41

1q2w3easxz 发表于 2020-5-27 11:00
蟹蟹大佬

嘿嘿问题解决了 就给个最佳吧~{:10_297:}

1q2w3easxz 发表于 2020-5-27 11:04:08

Twilight6 发表于 2020-5-27 11:01
嘿嘿问题解决了 就给个最佳吧~

嗯呐
页: [1]
查看完整版本: 求大神解围!!!