微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

运行游戏时显示随机图像

如何解决运行游戏时显示随机图像

我正在尝试为塔防游戏创建主菜单。这是我目前得到的代码

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)

enter image description here

这些根本不是加载的图像!一旦我退出,就会出现:

enter image description here

这是我想要的正确背景图像,但是没有出现设置按钮,这是我在出现此问题之前试图弄清楚的。可能是什么问题?

解决方法

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,这是您导入的示例图像。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。