0
我試圖讓使用Python一場比賽,所以這是代碼:錯誤在簡單的pygame的
# 1 - Import library
import pygame
from pygame.locals import *
# 2 - Initialize the game
pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
# 3 - Load images
player = pygame.image.load("resources/images/dude.png")
grass = pygame.image.load("resources/images/grass.png")
castle = pygame.image.load("resources/images/castle.png")
# 4 - keep looping through
while 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, (100,100))
# 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)
這是我得到的錯誤,但我無法弄清楚如何解決這個問題:
Traceback (most recent call last):
File "C:/Python32/Game 1/game.py", line 19, in <module>
for x in range(width/grass.get_width()+1):
TypeError: 'float' object cannot be interpreted as an integer
寬度/ grass.getwidth()被返回它似乎是一個浮子。嘗試將其轉換爲int以查看是否可以緩解您的問題。 (round(width/grass.get_width()+ 1)): 顯然,您應該根據您的需要向上或向下四捨五入。 – Tadgh
'range()'需要一個'int'參數,而在你的情況下得到'width/grass.get_width()+ 1'生成的'float'。 –
啊來想起它,round()返回一個浮點數似乎。使用int()來查看你是否得到你想要的行爲。 – Tadgh