简体   繁体   English

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

[英]Pygame moving image over rectangle with different colors

I am new to python and pygame and I'm trying to move a image over drawed rectangles with changing colors. 我是python和pygame的新手,我正在尝试将图像移到具有变化颜色的绘制矩形上。 Whenever I run this code the image moves but it creates a track of the image. 每当我运行此代码时,图像都会移动,但是会创建图像的轨迹。

I know that I need to blit the background of the image in the game loop but how can I blit the background if the background is not an image? 我知道我需要在游戏循环中为图像加背景,但是如果背景不是图像,如何加背景呢?

Or do I need to draw my rectangles in a different way? 还是我需要以其他方式绘制矩形?

Full code: 完整代码:

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()

You have to somehow redraw all the objects (rectangles) in your background at every update. 您必须在每次更新时以某种方式在背景中重新绘制所有对象(矩形)。

The simplest way to do this is just to call all the drawing code again, before updating your foreground object. 最简单的方法是在更新前景对象之前再次调用所有图形代码。 Another way, if the background does not change after created, is to blit these background objects into a separate Surface object, and blit that one to the screen on every update. 如果背景在创建后没有改变,则另一种方法是将这些背景对象变成单独的Surface对象,并在每次更新时将其变成屏幕对象。

More sophisticated ways would be to save the background under your foreground objects are prior to drawing them, and then, on the next redraw, first redraw the background and then save the background again and draw the foreground objects on the new position. 更复杂的方法是先在前景对象下保存背景,然后再绘制背景,然后在下一次重绘时先重绘背景,然后再次保存背景,然后在新位置上绘制前景对象。 It is easier to do one of the former methods. 使用前一种方法更容易。

Your code could be written like this: 您的代码可以这样写:

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()

You can just move the two lines that draw your rects into the main while 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()

If the background should be static, you could also draw the rects onto a background surface once and then blit this surf onto the screen in the main loop as jsbueno suggests. 如果背景是静态的,您还可以将rects绘制到背景表面上一次,然后按照jsbueno的建议将此冲浪blit到主循环中的screen

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

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