user3342999
user3342999

Reputation: 35

using classes with python and pygame

I have two scripts for a game I just started, one is the main game and the other is meant to show 3 images when the first script is ran, so when I run the first script (mainGame.py) it should run using classes and functions the second script (startUp.py) and then startUP would show a images with the game title and a created by screen and then I would have it go on to the game menu. but of course its not working, here is my code for the main game:

import pygame, random, math, sys, os, time
import startUp
from pygame.locals import *
pygame.init()

while True:

    #quit button 
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    startUp.Begin.Run()

code for second script (startUp.py):

import pygame, time
from pygame.locals import *
pygame.init()

class Begin(self):

    def __init__(self):
        self.screenA = "createdbyscreen.png"
        self.screenB = "powerdbyscreen.png"
        self.screenC = "gamelogoscreen.png"
        self.blackscreen = "blackSreen.png"

        self.screen = pygame.display.set_mode((1440, 720), 0, 32)

        self.startscreenA = pygame.image.load(screenA).convert()
        self.startscreenB = pygame.image.load(screenB).convert()
        self.startscreenC = pygame.image.load(screenC).convert()
        self.blackscreen = pygame.image.load(blackscreen).convert()

    def Run(self):
        while True:
           #starting imagers
            self.screen.blit(blackscreen, (0, 0))
            self.timeLeft = pygame.time.get_ticks()
            print(self.timeLeft)
            if self.timeLeft < 3500:
                self.self.screen.blit(self.startscreenA, (480, 120))
            if self.timeLeft > 4000 and self.timeLeft < 7500:
                self.screen.blit(self.startscreenB, (480, 120))
            if self.timeLeft > 8000 and self.timeLeft < 11150:
                self.screen.blit(self.startscreenC, (480, 120))

            pygame.display.update()

then if it helps then here are the error messages:

first script:

Traceback (most recent call last):
  File "C:\Users\claude\Desktop\game\dnagame.py", line 2, in <module>
    import startUp
  File "C:\Users\claude\Desktop\game\startUp.py", line 5, in <module>
    class Begin(self):
NameError: name 'self' is not defined

and the second:

Traceback (most recent call last):
  File "C:/Users/claude/Desktop/game/startUp.py", line 5, in <module>
    class Begin(self):
NameError: name 'self' is not defined

thank you for your time and help!

Upvotes: 0

Views: 950

Answers (2)

Jakob Bowyer
Jakob Bowyer

Reputation: 34698

class Begin(self):

You should inherit from object

class Begin(object):

You also need to make an instance

begin = startUp.Begin()
begin.Run()

Upvotes: 2

Maxime Lorant
Maxime Lorant

Reputation: 36161

A class must inherit from object and not self:

class Begin(object):

self is the convention name used for the first argument of instance methods, which's a reference to the instance itself.

Upvotes: 0

Related Questions