user13675340
user13675340

Reputation:

How to fix float integer TypeError in game

I'm making a game called Bunnies and Badgers and this is the code I have so far

import pygame
from pygame.locals import *
pygame.init()
width, height = 640, 480
screen = pygame.display.set_mode((width, height))
player = pygame.image.load("resources/images/dude.png")
grass = pygame.image.load("resources/images/grass.png")
castle = pygame.image.load("resources/images/castle.png")
while 1:
    screen.fill(0)
    screen.blit(player, (100, 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))
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit() 
            exit(0)

However, when I run the module, I get this error

Traceback (most recent call last):
  File "C:\Users\Ben\Documents\U Game Online\Games\Bunnies and Badgers\game.py", line 12, in <module>
    for x in range(width/grass.get_width()+1):
TypeError: 'float' object cannot be interpreted as an integer

I don't see any errors with the code given with the error.

Upvotes: 1

Views: 47

Answers (1)

Mike67
Mike67

Reputation: 11342

The range function requires an integer argument. Just case the parameter to int when calling range.

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))

Upvotes: 2

Related Questions