简体   繁体   中英

Python (pygame): How can I delete a line?

I'm using pygame to create a small scene. Right now, I'm working with lines.

I have an array of lines which are drawn to the screen and when a line is deleted from the array, I would like the line to disappear from the screen.

The problem I've found is that the line is drawn on the screen and remains static. I can't find a way to reset the screen (I'm using a JPEG as the background).

Is there a way to remove a drawn line from the screen?

Thanks

Although it does not seem very efficient, I think that the easiest and also the best way of doing it is by redrawing everything. In many many games the screen is totally redrawn at every frame, even without 3D cards (remember the old Doom games?). So drawing a few lines over a background will be very fast, even in python.

I would imagine something like that:

import pygame
import random

SCREEN_WIDTH = 320
SCREEN_HEIGHT = 200

class Line(object):
    def __init__(self, start_pos, end_pos, color, width):
        object.__init__(self)
        self.start_pos = start_pos
        self.end_pos = end_pos
        self.color = color
        self.width = width

def CreateRandomLine():
    rnd = random.randrange
    start_pos = (rnd(SCREEN_WIDTH), rnd(SCREEN_HEIGHT))
    end_pos = (rnd(SCREEN_WIDTH), rnd(SCREEN_HEIGHT))
    color = (rnd(255), rnd(255), rnd(255))
    width = rnd(10) + 1
    return Line(start_pos, end_pos, color, width)

def DrawScene(screen_surface, background_image, lines):
    screen_surface.blit(background_image, (0, 0))
    for line in lines:
        pygame.draw.line(screen_surface, line.color, \
                         line.start_pos, line.end_pos, line.width)

pygame.init()
screen_surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
background_image = pygame.Surface(((SCREEN_WIDTH, SCREEN_HEIGHT)))
background_image.fill((200, 100, 200)) # I kinda like purple.
# Alternatively, if you have a file for your background:
# background_image = pygame.image.load('background.png')
# background_image.convert()
lines = []
for i in range(10):
    lines.append(CreateRandomLine())

for frame_id in range(10):
    del lines[0] # Remove the oldest line, the one at index 0.
    lines.append(CreateRandomLine()) # Add a new line.
    DrawScene(screen_surface, background_image, lines)
    pygame.display.flip()
    pygame.time.wait(1000) # Wait one second between frames.

This script displays random lines on a background. There are 10 frames, each frame lasts one second. Between each frame, the first line is removed from the list of lines and a new line is added.

Just remove the pygame.time.wait and see how fast it goes :D.

If you use screen.fill([0,0,0]) it will fill in the background (or whatever you have set to be your background).

This will erase any lines drawn on the image, essentially removing anything drawn on the background.

You'll probably need to run the command to update the display, as explained in the documentation . The command to update the full display surface is:

pygame.display.update()

It may be possible to pass an argument to the function to only update the required part, which would be more efficient, but without the full code I can't tell you what that argument would be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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