问题描述
Menu.py
import pygame as pg
from pygame.examples.sprite_texture import img
pg.init()
class Menu():
def __init__(self,x,y,img):
self.img = img
self.y = y
self.x = x
def draw(self,win):
"""draw image"""
win.blit(self,img)
主文件
import pygame as pg
from Menu import Menu
pg.init()
"""displays screen"""
winWidth = 1280
winHeight = 720
size = (winWidth,winHeight)
win = pg.display.set_mode(size)
"""Load images and puts them into the correct size"""
bg_img = pg.image.load('Pic/td-gui/PNG/menu/bg.png')
bg_img = pg.transform.scale(bg_img,(1280,720))
setting_img = pg.image.load('Pic/td-gui/PNG/menu/button_settings.png')
clock = pg.time.Clock()
win.blit(bg_img,(0,0))
mainMenu = True
run = True
running = True
while run:
clock.tick(30)
pg.display.update()
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
if not run:
pg.quit()
if mainMenu:
settings_button = Menu(100,100,setting_img)
Menu.draw(setting_img)
这些根本不是加载的图像!一旦我退出,就会出现:
这是我想要的正确背景图像,但是没有出现设置按钮,这是我在出现此问题之前试图弄清楚的。可能是什么问题?
解决方法
blit
方法必须有参数。第一个参数是Surface,需要在目标上绘制,第二个参数是位置。位置是指定左上角的一对坐标或矩形。
win.blit(self,img)
win.blit(self.img,(self.x,self.y))
Menu
类
class Menu():
def __init__(self,x,y,img):
self.img = img
self.y = y
self.x = x
def draw(self,win):
"""draw image"""
win.blit(self.img,self.y))
,
这些图像是由于您从 pygames 示例模块(即菜单中的 from pygame.examples.sprite_texture import img
)中提取的。只需将其删除并确保正确调用 blit 函数即可。
import pygame as pg
pg.init()
class Menu:
def __init__(self,self.y))
由于您没有在 blit 函数中使用 self.img
,您实际上是在 blit 全局变量 img
,这是您导入的示例图像。