按下按钮时循环功能不起作用。代码检查

时间:2018-05-17 16:44:36

标签: python loops button while-loop pygame

我创建了一个开始菜单,当我按下按钮时,我想要的图像没有出现,我得到的只是按钮和标题仍然显示。 我一直在用这个网站来帮助我: 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()

我会尽全力帮助你。

1 个答案:

答案 0 :(得分:0)

您在In_Game功能的坐标(900,600)处,即在显示区域之外,对图像进行blitting。将它改为(0,0)而不是:

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