模块将不会加载,仅字幕会更改(pygame)

时间:2018-08-14 11:17:01

标签: python pygame

我和几个朋友创建了一个篮球游戏,用户可以从中选择不同的游戏模式。我负责主菜单,必须将所有游戏链接在一起。这是一组四个任务。我通过导入文件并在用户单击按钮时将用户发送到该文件来链接它的方式。它适用于我的另外两个朋友,但是一个朋友的模块仅在单击按钮时更改标题。

我的主菜单代码:

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)

现在这是我朋友的代码:

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()

1 个答案:

答案 0 :(得分:2)

我认为发生这种情况是因为其他模块在main函数中没有主循环,但在全局范围内,并且缺少此if子句:

if __name__ == '__main__':
    main()

这意味着其他游戏/模块在您导入时将运行(通常是不希望的)。要使用main函数开始游戏,您必须调用它:

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

顺便说一句,模块将被分配给与您的函数同名的局部变量,这有点令人困惑。我建议重命名功能。