【问题标题】:Loop Function Not Working when Button is Pressed. Code Check按下按钮时循环功能不起作用。代码检查
【发布时间】:2018-10-28 00:39:50
【问题描述】:

我创建了一个开始菜单,当我按下按钮时,我想要的图像没有出现,我得到的只是按钮和标题仍然显示。 我一直在使用这个网站来帮助我: https://pythonprogramming.net/(去游戏开发) 这是我的代码:

import pygame
import time
import random

pygame.init()

black=(0, 0, 0)
white=(255, 255, 255)
green=(0, 200, 0)
red=(255, 0, 0)
bright_green=(0, 255, 0)
bright_red=(200, 0, 0)

gameDisplay = pygame.display.set_mode((900, 600), pygame.RESIZABLE)
pygame.display.set_caption("Hero Jump")

clock = pygame.time.Clock()

def text_objects(text, font):
   textSurface = font.render(text, True, black)
   return textSurface, textSurface.get_rect()

def message(text):
   largeText=pygame.font.Font("LCALLIG.ttf", 65)
   TextSurf, TextRect = text_objects(text, largeText)    
   TextRect.center = ((450, 100))
   gameDisplay.blit(TextSurf, TextRect)

   pygame.display.update()

Earth_Image=pygame.image.load("Ground_Image.png")
Earth_Image=pygame.transform.scale(Earth_Image, (900, 600))

def In_Game_Earth_Image(x,y):
   gameDisplay.blit(Earth_Image, (x,y))

def Title():
    message("Hero Jump")

def button(msg, x, y, w, h, ic, ac, action=None):
    mouse = pygame.mouse.get_pos()
    click=pygame.mouse.get_pressed()

    if x+w > mouse[0] > x and y +h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac,(x, y, w, h) )
        if click[0]==1 and action != None:
            if action=="Start":
                In_Game()
            elif action=="quit":
                pygame.quit()
                quit()
    else:
        pygame.draw.rect(gameDisplay, ic,(x, y, w, h) )

    smallText=pygame.font.Font("freesansbold.ttf", 20)
    textSurf, textRect=text_objects(msg, smallText)
    textRect.center=( (x+(w/2)), (y+(h/2)))
    gameDisplay.blit(textSurf, textRect)

    pygame.display.update()

def In_Game():
    x=900
    y=600

    In_Game_Earth_Image(x,y)

    gameExit=False

    while not gameExit:

        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()

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

def Menu():
    Menu=True

    while Menu:
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()

        Title()

        button("Play", 120, 350, 200, 100, green, bright_green, "Start")
        button("Quit", 570, 350, 200, 100, bright_red, red, "quit")

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

gameDisplay.fill(white)
Menu()

pygame.quit()
quit()

感谢您的帮助。

【问题讨论】:

    标签: python loops button while-loop pygame


    【解决方案1】:

    您正在 In_Game 函数中的坐标 (900, 600) 处对图像进行 blitting,即在显示区域之外。改为在 (0, 0) 处进行 Blit:

    def In_Game():
        x = 0
        y = 0
        In_Game_Earth_Image(x,y)
    

    【讨论】:

    • 非常感谢。我救了我的命
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多