python - 如何在pygame中让一个矩形依次出现?

标签 python pygame

我一直在学习 python,刚刚开始在 Pygame 中进行编码。我当前的程序是一个游戏,其中的角色是一只苍蝇,它必须避免被苍蝇拍杀死。游戏必须首先在屏幕上的任意位置随机显示一个半透明的黄色方 block 。这个方 block 代表了 2 秒内苍蝇拍击的位置,让玩家有时间飞出该击打区域。当拍子击中时,我想在游戏中较暗的方 block 的确切位置上生成一个不透明的方 block 。当该方 block 出现时,如果玩家位于该方 block 内,则会杀死玩家。尽管我计划显示消息并添加此类性质的其他内容,但不透明方 block (或拍击)应该终止游戏 session 。我已经创建了两个正方形,但我需要将不透明的正方形放在半透明的正方形之后,并且位于同一位置。每次打击后,我希望方 block 消失并重复该过程。如果有人对我如何做有任何见解,我将非常感激:1)在随机位置上将半透明的方 block 快速移动,2)让较暗的方 block 在2秒内“击中”同一位置,然后消失并重复。我尝试过使用 time.sleep() 函数,但没有成功。 非常感谢!!

(请原谅我有点困惑的代码!)

import pygame, random, time, sys, math
from pygame.locals import *

pygame.init()

display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Fly Dodger')

black = (0,0,0)
white = (255,255,255)
yellow = (255,255,0)

fly_width = 70

clock = pygame.time.Clock()

crashed = False

flyImg = pygame.image.load("Fly.png")
flyImg = pygame.transform.scale(flyImg, (80, 80))

#THe faintswatter is the faint image/square I want to appear first. 
faintSwatter = pygame.image.load("faintswatter.png")
faintSwatter = pygame.transform.scale(faintSwatter, (400, 510))

#The realswatter is the square that appears after the fainter one, in the same position. 
realswatter = pygame.image.load("swatter.png")
realswatter = pygame.transform.scale(realswatter, (400, 510))

windowImg = pygame.image.load("window.jpg")
windowImg = pygame.transform.scale(windowImg, (800,600))
pygame.display.update()

def fly(x,y):
    gameDisplay.blit(flyImg, (x,y))

def border():
    x_change = 0
    y_change = 0

def swatter_Function(swatterx, swattery, swaterw, swaterh, color):
    pygame.draw.rect(gameDisplay, color, [swatterx, swattery, swaterw, swaterh])

def things_dodged(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Dodged: "+str(count), True, green)
    gameDisplay.blit(text, (0,0))

def message_display(text):
    largeText = pygame.font.Font("freesansbold.ttf",115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

def gameloop():
    x = (display_width * .45)
    y = (display_height * .4)
    x_change = 0
    y_change = 0

    gameExit = False

    while not gameExit:
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                crashed = True

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_LEFT:
                    x_change = -5

                elif event.key == pygame.K_RIGHT:
                    x_change = 5

                elif event.key == pygame.K_UP:
                    y_change = -5

                elif event.key == pygame.K_DOWN:
                    y_change = 5


        x += x_change
        y += y_change


            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0

        gameDisplay.blit(windowImg, (0,0))
        fly(x,y)
        gameDisplay.blit(faintSwatter,(1,1))
        gameDisplay.blit(realswatter, (2,2))
        pygame.display.update()

        if x > display_width - fly_width or x < 0:
            gameExit = True

        if y > display_height - fly_width or y < 0:
            gameExit = True



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

最佳答案

我做了一些重大改变..寻找#changes。

这些是我使用过的图像:/image/TrLyX.jpg

import pygame, random, time, sys, math
from pygame.locals import *

pygame.init()

display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Fly Dodger')

black = (0,0,0)
white = (255,255,255)
yellow = (255,255,0)

fly_width = 70

clock = pygame.time.Clock()

crashed = False

flyImg = pygame.image.load("Fly.png").convert() #changes
flyImg = pygame.transform.scale(flyImg, (80, 80))

#THe faintswatter is the faint image/square I want to appear first.
faintSwatter = pygame.image.load("faintswatter.png").convert() #changes
faintSwatter = pygame.transform.scale(faintSwatter, (400, 510))
faintSwatter.set_alpha(128) #changes


#The realswatter is the square that appears after the fainter one, in the same position.
realswatter = pygame.image.load("swatter.png").convert() #changes
realswatter = pygame.transform.scale(realswatter, (400, 510))

windowImg = pygame.image.load("window.jpg")
windowImg = pygame.transform.scale(windowImg, (800,600))
pygame.display.update()

def fly(x,y):
    gameDisplay.blit(flyImg, (x,y))

def border():
    x_change = 0
    y_change = 0

def swatter_Function(swatterx, swattery, swaterw, swaterh, color):
    pygame.draw.rect(gameDisplay, color, [swatterx, swattery, swaterw, swaterh])

def things_dodged(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Dodged: "+str(count), True, green)
    gameDisplay.blit(text, (0,0))

def message_display(text):
    largeText = pygame.font.Font("freesansbold.ttf",115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

def gameloop():
    x = (display_width * .45)
    y = (display_height * .4)
    x_change = 0
    y_change = 0

    #changes
    totalPlaytime = 0.0
    faintDisplay = True
    solidDisplay = False
    choosenCoords = False

    gameExit = False

    while not gameExit:
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                #changes
                gameExit = True

            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_LEFT:
                    x_change = -5

                elif event.key == pygame.K_RIGHT:
                    x_change = 5

                elif event.key == pygame.K_UP:
                    y_change = -5

                elif event.key == pygame.K_DOWN:
                    y_change = 5

            #changes - moved in the loop
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0

        #changes - moved out of the loop at the end
        x += x_change
        y += y_change

        gameDisplay.blit(windowImg, (0,0))
        fly(x,y)

        #changes
        if faintDisplay and not choosenCoords:
            xSwatter = random.randrange(0,display_width-400)
            ySwatter = random.randrange(0,display_height-510)
            choosenCoords = True
            faintSwatterBeginTime = totalPlaytime

        if faintDisplay:
            if totalPlaytime - faintSwatterBeginTime < 2.0:
                gameDisplay.blit(faintSwatter,(xSwatter,ySwatter))
            else:
                faintDisplay = False
                solidDisplay = True
                solidSwatterBeginTime = totalPlaytime

        if solidDisplay:
            if totalPlaytime - solidSwatterBeginTime < 0.5:
                gameDisplay.blit(realswatter, (xSwatter,ySwatter))
                if ((x+80 > xSwatter) and (x < xSwatter+400) and (y+80 > ySwatter) and (y < ySwatter+510)):
                    crashed = True
                    gameExit = True
            else:
                solidDisplay = False
                faintDisplay = True
                choosenCoords = False
        #changes end

        pygame.display.update()

        if x > display_width - fly_width or x < 0:
            gameExit = True

        if y > display_height - fly_width or y < 0:
            gameExit = True

        #pygame.display.update()

        #changes
        milliseconds = clock.tick(60)
        totalPlaytime += milliseconds/1000.0


gameloop()
pygame.quit()
quit()

我也是初学者..所以请原谅我这么冗长的代码。我所做的更改是:添加了时间计算,使扁平部分在一定程度上变得透明,添加了碰撞功能(您也可以使用 colliderect),添加了让微弱的拍子停留 2 秒、固体的拍子停留 0.5 秒的算法。

关于python - 如何在pygame中让一个矩形依次出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44491391/

相关文章:

python - 考虑报价的字符串拆分

python - 我如何断言列表与 pytest 相等

python - 在 Pandas 中结合屏蔽和索引

python - 随机砖颜色 -> TypeError : 'pygame.Color' object is not callable

python - Pygame 中的曲面绘制

python - 高效查找两个列表之间的元素差异

python - pandas.read_csv 未按分号分隔符对数据进行分区

python - 按下按键时做一些事情 | Python

python - PyGame 中 PAINT 程序的问题

python - 使用 pygame 绘制图元时出现工件?