Cian Mc
Cian Mc

Reputation: 49

Module won't load, caption only changes (pygame)

Me and a couple friends have created a basketball game with different game modes the user can choose from. I was in charge of the main menu and have to link all the games together. It's a group of four tasks. The way I've linked it by importing the file and sending the user to that file when the user clicks the button. It works for my other two friends but one friend's module only changes the caption when the button is clicked.

My main menu code:

import pygame
import sys

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

pygame.init()

# Set the height and width of the screen
size = [800, 600 ]
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Basketball")

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()



# This is a font we use to draw text on the screen (size 36)
'https://www.pygame.org/docs/ref/font.html'

font = pygame.font.Font(None, 36)
font2 = pygame.font.Font("Minecraft.ttf", 60)
font3 = pygame.font.Font("Minecraft.ttf", 40)
font4 = pygame.font.Font("Minecraft.ttf", 30)
font5 = pygame.font.Font("Minecraft.ttf", 30)


display_menu = True
start_page = 1

#Background
'https://stackoverflow.com/questions/28005641/how-to-add-a-background-image-into-pygame'

bg = pygame.image.load("resize.png")
bg2 = pygame.image.load("dar.png")
bg3 = pygame.image.load("controls.png")
bg4 = pygame.image.load("startscreen.png")

#Rectangle
'https://www.cs.ucsb.edu/~pconrad/cs5nm/topics/pygame/drawing/'

'start screen buttons'
rect1 = pygame.Rect(300,300,205,80)
rect2 = pygame.Rect(300,400,205,80)
rect3 = pygame.Rect(300,500,205,80)

'spacebar button'
rect4 = pygame.Rect(90,350,650,100)
'back button'
rect5 = pygame.Rect(50,510,100,50)
'universal idk button'
rect6 = pygame.Rect(300,400,205,80)
'play button in start screen'
rect7 = pygame.Rect(610,25,120,70)
rect8 = pygame.Rect(610,210,120,70)
rect9 = pygame.Rect(610,410,120,70)



def play_music():
    pygame.mixer.music.load('game.ogg')
    pygame.mixer.music.set_endevent(pygame.constants.USEREVENT)
    pygame.mixer.music.play()

def playerpong():
    import playerpong

def chrisgame():
    import chrisgame

def maxgametest():
    import maxgame






#Main Menu Loop

while not done and display_menu:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                start_page +=1

        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:
                if rect1.collidepoint(event.pos):
                    start_page =3
                if rect2.collidepoint(event.pos):
                    start_page =4

                if rect3.collidepoint(event.pos):
                    pygame.quit()

                if rect5.collidepoint(event.pos):
                    start_page =2

                if rect7.collidepoint(event.pos):
                    chrisgame()

                if rect8.collidepoint(event.pos):
                    maxgametest()

                if rect9.collidepoint(event.pos):
                    playerpong()

                    if start_page ==7:
                        display_menu = False


    # Set the screen background
    screen.fill(BLACK)

    if start_page == 1:

        screen.blit(bg2, (0, 0))
        pygame.draw.rect(screen, RED, rect4)
        text = font3.render("Click SPACEBAR to enter Game!", True, WHITE)
        screen.blit(text, [95, 400])

        play_music()

    if start_page == 2:

        screen.blit(bg, (0, 0))

        #Drawing the Button
        pygame.draw.rect(screen, BLACK, rect1)
        pygame.draw.rect(screen, BLACK, rect2)
        pygame.draw.rect(screen, BLACK, rect3)

        #Text in Button
        text = font3.render("START", True, WHITE)
        screen.blit(text, [335, 325])
        text = font4.render("CONTROLS", True, WHITE)
        screen.blit(text, [320, 425])
        text = font3.render("QUIT", True, WHITE)
        screen.blit(text, [335, 525])



    if start_page == 3:

        screen.blit(bg4, (0, 0))
        pygame.draw.rect(screen, GREEN, rect7)
        pygame.draw.rect(screen, RED, rect8)
        pygame.draw.rect(screen, BLACK, rect9)

        pygame.draw.rect(screen, RED, rect5)
        text = font5.render("BACK", True, WHITE)
        screen.blit(text, [60, 525])

        text = font5.render("PLAY", True, WHITE)
        screen.blit(text, [630, 45])
        text = font5.render("PLAY", True, WHITE)
        screen.blit(text, [630, 230])
        text = font5.render("PLAY", True, WHITE)
        screen.blit(text, [630, 430])


    if start_page == 4:

        screen.blit(bg3, (0, 0))
        pygame.draw.rect(screen, RED, rect5)
        text = font5.render("BACK", True, WHITE)
        screen.blit(text, [60, 525])


    clock.tick(60)

    pygame.display.flip()

#Main Program Loop
while not done:
    for event in pygame.event.get():       
        if event.type == pygame.QUIT:
            done = True

            screen.fill(RED)



    clock.tick(60)

Now this is my friend's code:

import pygame
import random
from pygame import mixer

pygame.font.init()
BLACK = (0, 0, 0)
width = 800
height = 600
ring_file = 'ring.png'
basketball_file = 'basketball.png'
goal_file = 'goal.png'
badguy_file = 'bad_guy.png'
pygame.init()
screen = pygame.display.set_mode((width, height))
screen_rect = screen.get_rect()
clock = pygame.time.Clock()
pygame.display.set_caption('Dribble Master')
font = pygame.font.Font(None, 25)
snd_file = 'Game.ogg'
background_file = 'halfcourt.jpg'
mixer.init()
mixer.music.load(snd_file)
mixer.music.play()

class Ball(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((100, 100)).convert_alpha()
        self.image = pygame.image.load(basketball_file).convert_alpha()
        self.image = pygame.transform.scale(self.image, (50, 50))
        self.rect = pygame.Rect(x, y, 100, 100)
        self.x = self.rect.x
        self.y = self.rect.y
        self.speed_x = 0
        self.speed_y = 0
        self.radiusx = 0
        self.radiusy = 10
        self.mask = pygame.mask.from_surface(self.image)
        self.rect.midbottom = x, y

    def update(self, screen_rect):  # Pass a rect with the size of the screen.
        self.x += self.speed_x
        self.y += self.speed_y
        self.rect.topleft = (self.x, self.y)
        if not screen_rect.contains(self.rect):
            # Clamp the rect if it's outside of the screen.
            self.rect.clamp_ip(screen_rect)
            self.x, self.y = self.rect.topleft

    def render(self, screen):
        screen.blit(self.image, self.rect)


class Goal(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((220, 220)).convert_alpha()
        self.image = pygame.image.load(goal_file).convert_alpha()
        self.image = pygame.transform.scale(self.image, (150, 100))
        self.x = x
        self.y = y
        self.rect = pygame.Rect(self.x, self.y, 100, 100)

    def render(self, screen):
        screen.blit(self.image, self.rect)


class Ring(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((400, 400)).convert_alpha()
        self.image = pygame.image.load(ring_file).convert_alpha()
        self.image = pygame.transform.scale(self.image, (80, 50))
        self.x = x
        self.y = y
        self.rect = pygame.Rect(self.x, self.y, 400, 400)
        self.mask = pygame.mask.from_surface(self.image)

    def render(self, screen):
        screen.blit(self.image, self.rect)


class Mob(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((90, 90)).convert_alpha()
        self.image = pygame.image.load(badguy_file).convert_alpha()
        self.image = pygame.transform.scale(self.image, (100, 100))
        self.rect = pygame.Rect(x, y, 100, 100)
        self.x = x
        self.y = y
        self.rect.x = random.randrange(800 - 100)
        self.rect.y = random.randrange(-100, -40)
        self.speedx = 5
        self.mask = pygame.mask.from_surface(self.image)

    def update(self):
        self.rect.y += self.speedy
        if self.rect.top > height + 10 or self.rect.left < -25 or self.rect.right > width + 20:
            self.rect.x = random.randrange(width - self.rect.width)
            self.rect.y = random.randrange(-100, -40)
            self.speedx = random.randrange(1,0)

    def render(self, screen):
        screen.blit(self.image, (self.x, self.y))

mobs = pygame.sprite.Group()       
for i in range(8):
    mob = Mob(200,300)
    mobs.add(mob)

def main():
    court = pygame.Surface((1200, 722))
    court = pygame.image.load('halfcourt.jpg')
    court = pygame.transform.scale(court, (width, height))
    basketball = Ball(375, 500)
    goal = Goal(324, 0)
    ring = Ring(363, 50)

    score = 0  # The score variable was missing.

    stop_game = False

    while not stop_game:
        # Event handling.
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                stop_game = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    basketball.speed_y = -2
                elif event.key == pygame.K_DOWN:
                    basketball.speed_y = 2
                elif event.key == pygame.K_LEFT:
                    basketball.speed_x = -2
                elif event.key == pygame.K_RIGHT:
                    basketball.speed_x = 2
            elif event.type == pygame.KEYUP:
                # Stop the ball.
                if event.key == pygame.K_UP:
                    basketball.speed_y = 0
                elif event.key == pygame.K_DOWN:
                    basketball.speed_y = 0
                elif event.key == pygame.K_LEFT and basketball.speed_x < 0:
                    basketball.speed_x = 0
                elif event.key == pygame.K_RIGHT and basketball.speed_x > 0:
                    basketball.speed_x = 0

        basketball.update(screen_rect)

        point = pygame.sprite.collide_mask(basketball, ring)  # Use basketball not player.
        if point:
            score += 1
            Ball(375, 500)

        # Drawing.
        screen.blit(court, (0,0))
        font = pygame.font.Font(None, 36)
        text = font.render(str(score), 1, (BLACK))
        textpos = (700,20)
        screen.blit(text, textpos)


        text = font.render('Dodge the enemies to get to the goal!',True, (0, 0, 0))
        screen.blit(text, (170, 550))
        goal.render(screen)
        ring.render(screen)
        basketball.render(screen)

        pygame.display.update()
        clock.tick(60)

    pygame.quit()

if __name__ == '__main__':
    main()

Upvotes: 2

Views: 91

Answers (1)

skrx
skrx

Reputation: 20438

I assume that happens because the other modules don't have their main loops in a main function but in the global scope and are missing this if clause:

if __name__ == '__main__':
    main()

That means the other games/modules will run when you import them (that's usually not desired). To start the game with the main function, you have to call it:

def start_maxgame():
    import maxgame
    maxgame.main()

By the way, the modules will be assigned to local variables with the same names as your functions which is a bit confusing. I suggest renaming the functions.

Upvotes: 2

Related Questions