Reputation: 107
I'm learning Pygame by making simple games with it. But my game is very laggy and I've learned that it could be because pygame is converting the .png image everytime it blits to the screen.
So I tried to use convert() to load the image. But it gives me an error. Made some research but no answer could help me even though I've tested some.
my code:
import pygame
import os, random
class Item:
def __init__(self):
self.image = Context.load_image("item.png")
self.X = random.randint(0, Context.resolution[1] - 200)
self.Y = 0
self.dY = 10
class Basket:
def __init__(self):
self.image = Context.load_image("catcher.png")
self.X = Context.resolution[0] / 2
self.Y = Context.resolution[1] - 200
self.vel = 30.
self.dX = self.dY = 0
def action(self, event):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
self.dX = -self.vel
elif event.key == pygame.K_d:
self.dX = self.vel
elif event.type == pygame.KEYUP:
self.dX = 0
class Context:
resolution = (1600, 900)
dir_resources = "resources/"
def __init__(self):
self.fps = 60
self.background_theme = self.load_image("background.png")
@staticmethod
def load_image(image_name):
path = Context.dir_resources + image_name
# ERROR HAPPENS HERE! If I take ".convert()" off, the game runs but laggy
return pygame.image.load(os.path.join(path)).convert()
class Game:
score = 0
def __init__(self):
self.game_on = True
self.context = Context()
self.basket = Basket()
self.screen = pygame.display.set_mode(self.context.resolution, pygame.HWSURFACE | pygame.DOUBLEBUF)
self.fps = self.context.fps
self.clock = pygame.time.Clock()
self.tshirt = Item()
def event_loop(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.game_on = False
self.basket.action(event)
def update(self):
self.basket.X = self.basket.X + self.basket.dX
self.tshirt.Y = self.tshirt.Y + self.tshirt.dY
def draw(self):
self.screen.blit(self.context.background_theme, (0, 0))
self.screen.blit(self.basket.image, (self.basket.X, self.basket.Y))
self.screen.blit(self.tshirt.image, (self.tshirt.X, self.tshirt.Y))
pygame.display.flip()
def run(self):
while self.game_on:
self.event_loop()
self.update()
self.draw()
pygame.display.update()
self.clock.tick(self.fps)
def main():
pygame.init()
game = Game()
game.run()
pygame.quit()
if __name__ == '__main__':
main()
here is the error:
Traceback (most recent call last):
File "/me/tmvaz/PycharmProjects/Game/__main__.py", line 105, in <module>
main()
File "/home/me/PycharmProjects/Game/__main__.py", line 98, in main
game = Game()
File "/home/me/PycharmProjects/Game/__main__.py", line 57, in __init__
self.context = Context()
File "/home/me/PycharmProjects/Game/__main__.py", line 42, in __init__
self.background_theme = self.load_image("background.png")
File "/home/me/PycharmProjects/Game/__main__.py", line 47, in load_image
return pygame.image.load(os.path.join(path)).convert()
pygame.error: No video mode has been set
Process finished with exit code 1
I'm using Linux, Lubuntu if that could be helpfull.
Upvotes: 1
Views: 361
Reputation: 20478
You have to call pygame.display.set_mode
before you call the convert
or convert_alpha
methods. So I'd just move this line to the top of the program:
pygame.init()
screen = pygame.display.set_mode(resolution, pygame.HWSURFACE | pygame.DOUBLEBUF)
You can pass the screen
variable to the Game object when you instantiate it and then assign it to self.screen
.
Upvotes: 2