0

I'm quite new to pygame and I'm trying to make a game for a school project but I'm struggling to get a sprite class which I've called Enemy. working, I want my end product to have like one sprite on the screen and then be replaced when it "dies" but I'm currently struggling to even make one work

menuScreen.blit(self.image, self.rect)

builtins.AttributeError: 'Screen' object has no attribute 'blit'

I don't know what else I'm meant to use and I've tried .draw and some other things but nothings working

import pygame

colours = {"White" : (255, 255, 255), "Black" : (0, 0, 0), "Red" : (255, 0, 0), "Blue" : (0, 0, 255), "Green" : (0, 255, 0)}

class Screen():
    def __init__(self, title, width=400, height=600, fill=colours["White"]):
        self.title=title
        self.width=width
        self.height = height
        self.fill = fill
        self.current = False
        
    def makeCurrent(self):
        pygame.display.set_caption(self.title)
        self.current = True
        self.screen = pygame.display.set_mode((self.width, self.height))
         
    def endCurrent(self):
        self.current = False
        
    def checkUpdate(self):
        return self.current
    def screenUpdate(self):
        if(self.current):
            self.screen.fill(self.fill)
            
    def returnTitle(self):
        return self.screen
    
class Button():
        def __init__(self, x, y, sx, sy, bcolour, fbcolour, font, fontsize, fcolour, text):
            self.x = x
            self.y = y
            self.sx = sx
            self.sy = sy
            self.bcolour = bcolour
            self.fbcolour = fbcolour
            self.fcolour = fcolour
            self.fontsize = fontsize
            self.text = text
            self.current = False
            self.buttonf = pygame.font.SysFont(font, fontsize)
        def showButton(self, display):
            if(self.current):
                pygame.draw.rect(display, self.fbcolour, (self.x, self.y, self.sx, self.sy))
            else:
                pygame.draw.rect(display, self.bcolour, (self.x, self.y, self.sx, self.sy))
                
            textsurface = self.buttonf.render(self.text, False, self.fcolour)
            display.blit(textsurface, ((self.x + (self.sx/2) - (self.fontsize/2)*(len(self.text)/2) - 5,(self.y + (self.sy/2) -(self.fontsize/2) - 4))))
        def focusCheck(self, mousepos, mouseclick):
            if(mousepos[0] >= self.x and mousepos[0] <= self.x + self.sx and mousepos[1] >= self.y and mousepos[1] <= self.y + self.sy):
                self.current = True
                return mouseclick[0]
            else:
                self.current = False
                return False
class Enemy(pygame.sprite.Sprite):

    def __init__(self, dx, dy, filename):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load(filename).convert()

        self.rect = self.image.get_rect()
        self.rect.x = dx
        self.rect.y = dy

   

    def draw(self, menuScreen):
        menuScreen.blit(self.image, self.rect)
    
pygame.init()
pygame.font.init()

menuScreen = Screen("Menu Screen")
screen2 = Screen("Screen 2")

win = menuScreen.makeCurrent()

done = False
shopButton = Button(125, 500, 150, 50, colours["Black"], colours["Red"], "arial", 20, colours["White"], "Shop")
DungeonButton = Button(125, 500, 150, 50, colours["Black"], colours["Blue"], "arial", 20, colours["White"], "Dungeon")
goblin = Enemy(0 , 0, "images\goblin.jpg")
toggle = False
while not done:
    menuScreen.screenUpdate()
    screen2.screenUpdate()
    mouse_pos = pygame.mouse.get_pos()
    mouse_click = pygame.mouse.get_pressed()
    keys = pygame.key.get_pressed()
    
    if menuScreen.checkUpdate():
        screen2button = shopButton.focusCheck(mouse_pos, mouse_click)
        shopButton.showButton(menuScreen.returnTitle())
        goblin.draw(menuScreen)
        if screen2button:
            win = screen2.makeCurrent()
            menuScreen.endCurrent()
            
    elif screen2.checkUpdate():
        returnm = DungeonButton.focusCheck(mouse_pos, mouse_click)
        DungeonButton.showButton(screen2.returnTitle())
        
        if returnm:
            win = menuScreen.makeCurrent()
            screen2.endCurrent()
    
    for event in pygame.event.get():
        if(event.type == pygame.QUIT):
            done = True
    
    
    pygame.display.update()
    
pygame.quit()
Masoud Keshavarz
  • 2,056
  • 7
  • 31
  • 44
  • 2
    Blitting is something that you do to a surface, which your `Screen` class is *not.* There is a distinction between "is-a" (inheritance) and "has-a" (containment). Either make your screen class a child of surface, or provide a `Screen.blit` that call through to `self.screen.blit`. – paxdiablo Jun 30 '21 at 00:14
  • Change `goblin.draw(menuScreen)` to `goblin.draw(menuScreen.screen)` – Rabbid76 Jun 30 '21 at 04:03

0 Answers0