繁体   English   中英

pygame在具有不同颜色的矩形上移动图像

[英]Pygame moving image over rectangle with different colors

我是python和pygame的新手,我正在尝试将图像移到具有变化颜色的绘制矩形上。 每当我运行此代码时,图像都会移动,但是会创建图像的轨迹。

我知道我需要在游戏循环中为图像加背景,但是如果背景不是图像,如何加背景呢?

还是我需要以其他方式绘制矩形?

完整代码:

import pygame


pygame.init()
screen = pygame.display.set_mode((600,200))
pygame.draw.rect(screen, (175,171,171), [0, 0, 600, 200])
pygame.draw.rect(screen, (255,192,0), [200, 0, 200, 200])
clock = pygame.time.Clock()
# load your own image here (preferably not wider than 30px)
truck = pygame.image.load('your_image.png').convert_alpha()


class Truck:
    def __init__(self, image, x, y, speed):
        self.speed = speed
        self.image = image
        self.pos = image.get_rect().move(x, y)

    def move(self):
        self.pos = self.pos.move(self.speed, 0)




def game_loop():
    newTruck = Truck(truck, 0, 50, 1)
    gameExit = False
    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True

        newTruck.move()
        screen.blit(newTruck.image, newTruck.pos)
        clock.tick(60)
        pygame.display.update()

game_loop()

您必须在每次更新时以某种方式在背景中重新绘制所有对象(矩形)。

最简单的方法是在更新前景对象之前再次调用所有图形代码。 如果背景在创建后没有改变,则另一种方法是将这些背景对象变成单独的Surface对象,并在每次更新时将其变成屏幕对象。

更复杂的方法是先在前景对象下保存背景,然后再绘制背景,然后在下一次重绘时先重绘背景,然后再次保存背景,然后在新位置上绘制前景对象。 使用前一种方法更容易。

您的代码可以这样写:

import pygame

pygame.init()
SIZE = (600,200)

screen = pygame.display.set_mode(SIZE)

bg_image = None

def draw_background(screen):
    global bg_image
    if not bg_image:
        bg_image = pygame.Surface((SIZE))
        pygame.draw.rect(bg_image, (175,171,171), [0, 0, 600, 200])
        pygame.draw.rect(bg_image, (255,192,0), [200, 0, 200, 200])
        ...
        # Draw whatever you want inside this if body

    screen.blit(bg_image, (0, 0))

...

class Truck:
    ...

def game_loop():
    newTruck = Truck(truck, 0, 50, 1)
    gameExit = False
    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True

        newTruck.move()
        draw_background()
        screen.blit(newTruck.image, newTruck.pos)
        clock.tick(60)
        pygame.display.update()

game_loop()

您可以将将rect绘制的两条线移动到主while循环中:

def game_loop():
    newTruck = Truck(truck, 0, 50, 1)
    gameExit = False
    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True

        newTruck.move()
        # Draw the background items first, then the foreground images.
        # If the rects don't cover the whole screen, you can use
        # `screen.fill(some_color)` to clear it.
        pygame.draw.rect(screen, (175,171,171), [0, 0, 600, 200])
        pygame.draw.rect(screen, (255,192,0), [200, 0, 200, 200])
        screen.blit(newTruck.image, newTruck.pos)
        clock.tick(60)
        pygame.display.update()

如果背景是静态的,您还可以将rects绘制到背景表面上一次,然后按照jsbueno的建议将此冲浪blit到主循环中的screen

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM