为了账号安全,请及时绑定邮箱和手机立即绑定

图像没有出现在开始菜单中。小游戏

图像没有出现在开始菜单中。小游戏

德玛西亚99 2021-06-02 01:45:40
我在这里有这个代码,在开始菜单中我想要一些图像从天而降,但它不起作用。我尝试了很多东西,但都没有奏效。这是代码:import pygameimport randompygame.init()display_width= 1000display_height= 600black = (0,0,0)white = (255, 255, 255)blue = (0, 0, 112)x=0y=0gameDisplay = pygame.display.set_mode((display_width, display_height))pygame.display.set_caption("Name")clock = pygame.time.Clock()def text_objects(text, font, color):    textSurface = font.render(text, True, color)    return textSurface, textSurface.get_rect()Stmenpic = pygame.image.load("Start_menu_sky_back_ground.png")def StartMenPic(x,y):    gameDisplay.blit(Stmenpic, (x,y) )def Title_Of_Game(text):    Title_Text = pygame.font.Font("ARDESTINE.ttf", 115)    TextSurf, TextRect = text_objects(text, Title_Text, blue)    TextRect.center = ((500),(100))    gameDisplay.blit(TextSurf, TextRect)    pygame.display.updateBaby_1=pygame.image.load("Baby_1.png")def things(x,y):    gameDisplay.blit(Baby_1,(x, y ))x_change = 0def Title_of_Game():    Title_Of_Game("Title")def Start_Menu():    StartMenu=True    while StartMenu:        x = 0        y = 0        x_change = 0        thing_startx = random.randrange(0, display_width)        thing_starty = -600        thing_speed = 7        thing_width= 30        thing_height=30        for event in pygame.event.get():            #print(event)            if event.type == pygame.QUIT:                pygame.quit()                quit()        Title_of_Game()        x_change=0        x+= x_change        if thing_starty > display_height:            thing_starty = 0 - thing_height            thing_startx = random.randrange(0, display_width)        pygame.display.update()        clock.tick(60)StartMenPic(0, 0)Start_Menu()pygame.quit()quit()这可能很明显,但我才刚刚开始,所以我需要很多帮助。感谢您对这个问题的所有帮助。现在我遇到了块的问题,它总是在随机位置生成但不会下降。
查看完整描述

1 回答

?
手掌心

TA贡献1942条经验 获得超3个赞

不要在while循环中定义位置和速度变量,否则它们将在每帧(循环的迭代)中重置。相反,在循环外定义它们并通过增加速度来改变循环内的位置。


我还建议使用一个pygame.Rect对象,而不是独立的thing_startx,thing_starty,thing_width和thing_height变量:


# Pass the top left coordinates, the width and the height.

thing_rect = pygame.Rect(random.randrange(display_width), -60, 30, 30)

然后通过添加每帧的速度来更新其 y 坐标:


thing_rect.y += thing_speed

这是一个最小的完整示例:


import pygame

import random



pygame.init()


display_width = 1000

display_height = 600


gameDisplay = pygame.display.set_mode((display_width, display_height))

clock = pygame.time.Clock()


stmenpic = pygame.Surface((30, 50))

stmenpic.fill((0, 200, 50))



def start_menu():

    thing_rect = pygame.Rect(random.randrange(display_width), -60, 30, 30)

    thing_speed = 7


    while True:

        for event in pygame.event.get():

            if event.type == pygame.QUIT:

                return


        # Move the rect downwards.

        thing_rect.y += thing_speed

        # Reset the position of the rect when it leaves the screen.

        if thing_rect.y > display_height:

            thing_rect.y = 0 - thing_rect.height

            thing_rect.x = random.randrange(display_width)


        gameDisplay.fill((30, 30, 30))

        gameDisplay.blit(stmenpic, thing_rect)  # Blit the image at the rect.

        pygame.display.update()

        clock.tick(60)



start_menu()

pygame.quit()

如果你想要多个落下的物体,只需将一些矩形放入一个列表中并使用for循环来更新和绘制它们。


查看完整回答
反对 回复 2021-06-09
  • 1 回答
  • 0 关注
  • 110 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号