首页 > 解决方案 > How do I import a picture and put it on a pygame window

问题描述

I've been trying for a while now to make a destroyer type game using 'pygame' the main problem I have is importing a picture of a ship to use as the main player. And no matter what I try, I always get the same error

win.blit(win, char, (20, 20), (146, 149))
   TypeError: an integer is required (got type tuple)

And this is my code:

import pygame
pygame.init()
win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("First Game")
char = [pygame.image.load('char.png')]
run = True
while run:
    win.fill((0,0,0))
    win.blit(win, char, (20, 20), (146, 149))

    pygame.display.update()

    pygame.quit()

Any help would be appreciated.

标签: pythonpygameinteger

解决方案


blit()是一个实例方法。除了 的实例之外pygame.Surface,必需的参数是必须blit位于实例pygame.Surface和位置上的 Surface。

此外,char是具有 on 元素的表面列表。

char = [pygame.image.load('char.png')]

将其更改为:

char = pygame.image.load('char.png')

blit表面:char_win

win.blit(char, (20, 20))

或者

pygame.Surface.blit(win, char, (20, 20))

请注意,area参数 (of blit) 必须是一个矩形,并表示要绘制的源 Surface 的较小部分


推荐阅读