2

I am trying to create a main menu for a tower defence game. This is the code I've got so far.

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)

Main.py

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

These are not the images loaded at all! Once I exit this is then what appears:

enter image description here

This is the correct background image I want, however the settings button does not appear which is what I was trying to figure out before this issue came along. What could be the issue?

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
james
  • 21
  • 3

2 Answers2

2

The images are due to you blitting them from pygames example module, i.e. from pygame.examples.sprite_texture import img in your menu. Just remove it and make sure to call the blit function correctly.

import pygame as pg

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, (self.x, self.y))

Since you weren't using self.img in your blit function, you where actually blitting the global variable img, which was an example image you imported.

Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
0

The blit method has to arguments. The 1st argument is Surface, which needs to be drawn on the target, and the 2nd argument is the position. The position is either a pair of coordinates specifying the top left corner or a rectangle.

win.blit(self, img)

win.blit(self.img, (self.x, self.y))

Menu class

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.x, self.y))
Rabbid76
  • 202,892
  • 27
  • 131
  • 174