python - 按下按钮时循环功能不起作用。代码检查
问题描述
我创建了一个开始菜单,当我按下按钮时,我想要的图像没有出现,我得到的只是按钮和标题仍然显示。我一直在使用这个网站来帮助我: 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()
我会很感激你的帮助。
解决方案
您正在函数中的坐标 (900, 600) 处对图像进行 blitting In_Game
,即在显示区域之外。改为在 (0, 0) 处进行 Blit:
def In_Game():
x = 0
y = 0
In_Game_Earth_Image(x,y)
推荐阅读
- sql - RDBM 和 SQL 数据库中主键、自增和 UUID 的最佳实践
- compression - 重新压缩压缩文件
- java - 这个最低共同祖先算法的时间复杂度是多少?
- python - 图形工具中的 has_path 等效项
- azure - 天蓝色 blob 存储 cli 列出目录
- php - 如果数据库中已存在行,则不要导入行
- azure - Azure AD B2C Xamarin.ios 本机
- python - 在 Pandas 中调用多行
- spring - ClassTemplateLoader 不在指定路径中寻找模板
- mysql - 尝试从 sqoop 中选择时出现 MySQLSyntaxErrorException