当我更新游戏显示时,你如何在pygame中绘制一个不会被删除的形状?

时间:2018-04-16 07:07:22

标签: python pygame lag

我正在尝试使用python和pygame创建我自己的brick breaker版本,我发现我的计算机每次运行主循环时都会努力绘制构成该级别的每个砖块。我需要知道如何绘制它们并保持它们被绘制,直到我真的需要更新它们。

import pygame
from random import randint
pygame.init()
pygame.mouse.set_visible(True)

#Time
FPS = 60
clock = pygame.time.Clock()

#Colors
black = (0, 0, 0)
grey = (127, 127, 127)
white = (255, 255, 255)

#Display
displayInfo = pygame.display.Info()

displayWidth = displayInfo.current_w - 200
displayHeight = displayInfo.current_h - 100

borderB = 10
borderS = 1

gameDisplay = pygame.display.set_mode((displayWidth, displayHeight)) #,pygame.FULLSCREEN)
pygame.display.set_caption("Brick Breaker")
gameDisplay.fill(white)
pygame.display.update()

#Blocks
blocks = []
blockSize = 20
blockCount = 20
speed = int(blockSize / 2)


"""Classes"""

#Player
class Player():
    def __init__(self):
        #Size
        self.width = blockSize * 5
        self.height = blockSize

        #Pos
        self.x = displayWidth / 2 - self.width / 2
        self.y = displayHeight - borderB - self.height

    def move(self):
        mousePos = pygame.mouse.get_pos()
        #Check border left
        if not mousePos[0] >= borderS:
            mousePos = (borderS, mousePos[1])
        #Check border right
        elif not mousePos[0] + self.width <= displayWidth - borderS:
            mousePos = (displayWidth - borderS - self.width, mousePos[1])

        self.x = mousePos[0]

class Block():
    def __init__(self, x = 0, y = 0):
        #Size
        self.width = blockSize * 2
        self.height = blockSize

        #Position
        self.x = x
        self.y = y

    def giveDetails(self):
        print(self.x, self.y, self.width, self.height)


class Ball():
    def __init__(self):
        self.radius = int(blockSize / 2)
        self.width = blockSize

        #Co-ordinates
        self.x = int(playerBoard.x + playerBoard.width / 2)
        self.y = int(playerBoard.y - self.radius)

        self.yChange = - speed
        self.xChange = 10

    def move(self):
        if self.x - self.radius + borderS <= 0 or self.x + self.radius >=     displayWidth - borderS:
            self.xChange *= -1
        if self.y - self.radius <= 0 or self.y + self.radius >=     displayHeight - borderB:
            self.yChange *= -1  

        self.x += self.xChange
        self.y += self.yChange



"""Functions"""

def create_level():
    global blocks
    blocks = []
    xPos = blockSize
    yPos = blockSize

    #Down, across
    accross = int((displayWidth - blockSize * 2 - borderS * 2) / (blockSize     * 2 + 2))
    levelData = [1, accross]
    for row in range(levelData[0]):
        yPos += blockSize + 1
        xPos = blockSize
        for num in range(levelData[1]):
            xPos += blockSize * 2 + 1
            _ = Block(xPos, yPos)
            _.name = "block" + str(num)
            blocks.append(_)


def main_loop():
    global playerBoard, ball
    gameExit = False
    pygame.mouse.set_visible(False)

    playerBoard = Player()
    ball = Ball()
    #This creates a list called 'blocks' with all the block objects
    create_level()

    while not gameExit:
       for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
                break
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    menu()
                    gameExit = True
                    break
            if event.type == pygame.MOUSEMOTION:
                playerBoard.move()

        ball.move()

        gameDisplay.fill(white) #Clear Screen

        #Draw Player
        pygame.draw.rect(gameDisplay, grey, (playerBoard.x, playerBoard.y, 
    playerBoard.width, playerBoard.height))

        """Draw Blocks, this is where the main problem is as it runs this     loop 100 
    times or so as there are about 100 blocks. This obviously means that the 
    display.update has a hard time."""

        for item in blocks:
            pygame.draw.rect(gameDisplay, black, (item.x, item.y,     item.width, item.height))

        #Draw ball
        pygame.draw.circle(gameDisplay, black, (ball.x, ball.y), ball.radius)

        pygame.display.update()

        clock.tick(FPS)

main_loop()

pygame.quit()
quit()

0 个答案:

没有答案