Pygame中的图像是跨屏幕拖动像素

时间:2017-06-12 18:21:46

标签: python image pygame

我一直在尝试在pygame中创建一个移动的背景(由箭头键控制)。我的问题是,当图像通过窗口的边框时,它会产生拖动效果。所有末端像素都分布在窗口的其余部分。

this is what it looks like

这是我的动作:

    if pressed[pygame.K_RIGHT]:
        if activate == True:
            x-=1
        left = False
        right = True
        bgx-=1
        bg2x-=1
    if pressed[pygame.K_LEFT]:
        if activate == True:
            x+=1
        left = True
        right = False
        bgx+=1
        bg2x+=1 

这是图像到达窗口的末尾:

    if bgx >= 1000 and right == True:
        bgx = bg2x-1000
    if bg2x >=1000 and right ==True:
        bg2x = bgx-1000
    if bgx <= -1000 and left == True:
        bgx = bg2x+1000
    if bg2x <=-1000 and left ==True:
        bg2x = bgx+1000
    if x <=-20 and right == True:
        x = 1000
    if x>=1000 and left == True:
        x=-20

这就是我在图像中快速的地方:

        screen.blit(bg,(bgx,bgy))
        screen.blit(bg2,(bg2x,bg2y))

感谢任何帮助 - 谢谢!

我知道我不应该,但这是我的整个代码

import pygame,sys
from pygame.locals import *
import os

game = ""
def home():
    global game
    pygame.init()
    screen = pygame.display.set_mode((1000,750))
    done = False
    red= (255,0,0)
    green = (0,255,0)
    blue = (0,0,255)
    white = (255,255,255)
    yList = []
    xList = []
    pygame.mouse.set_visible(False)
    for l in range(255):
        yList.append(-100)
        xList.append(-100)
    clock=pygame.time.Clock()
    messenger = True
    img_rect = pygame.transform.scale(pygame.image.load("space.jpg"),(1000,750)).get_rect()
    bg = pygame.Surface((img_rect.width, img_rect.height), pygame.SRCALPHA)
    bg.fill((0, 0, 0, 0))
    bg.blit(pygame.transform.scale(pygame.image.load("space.jpg"),(1000,750)), img_rect)
    img_rect = pygame.transform.scale(pygame.image.load("space.jpg"),(1000,750)).get_rect()
    bg2 = pygame.Surface((img_rect.width, img_rect.height), pygame.SRCALPHA)
    bg2.fill((0, 0, 0, 0))
    bg2.blit(pygame.transform.scale(pygame.image.load("space.jpg"),(1000,750)), img_rect)
    bgx = 1000
    bgy=0
    bg2x=0
    bg2y=0
    right = True
    left = False
    activate = False
    x=500
    y=450
    scale = 100
    def messenger(msg, colour, size):
        global text
        font = pygame.font.SysFont("comicsansms", size)

        text = font.render(msg, True, colour)
        return(text)
    while not done:
            for event in pygame.event.get():
                    if event.type    == pygame.QUIT:
                            done = True
            pressed = pygame.key.get_pressed()
            if pressed[pygame.K_SPACE]:
                activate = True
                y=450
                x=500
            if pressed[pygame.K_RIGHT]:
                if activate == True:
                    x-=1
                left = False
                right = True
                bgx-=1
                bg2x-=1
            if pressed[pygame.K_LEFT]:
                if activate == True:
                    x+=1
                left = True
                right = False
                bgx+=1
                bg2x+=1
            if bgx >= 1000 and right == True:
                bgx = bg2x-1000
            if bg2x >=1000 and right ==True:
                bg2x = bgx-1000
            if bgx <= -1000 and left == True:
                bgx = bg2x+1000
            if bg2x <=-1000 and left ==True:
                bg2x = bgx+1000
            if x <=-20 and right == True:
                x = 1000
            if x>=1000 and left == True:
                x=-20
            screen.blit(bg2,(bg2x,bg2y))
            screen.blit(bg,(bgx,bgy))

            if activate == True:
                y-=0.3
                scale+=1
                pygame.draw.rect(screen,(255,0,0),(x,y,(scale*10)/100,(scale*50)/100))
            pygame.draw.polygon(screen, (0,0,0), [(450,750),(550,750),(525,450),(475,450)])
            pygame.draw.circle(screen,(255,0,0),(500,450),25)
            pygame.display.flip()
            clock.tick(60)
    pygame.quit()
home()

1 个答案:

答案 0 :(得分:0)

你是否在任何地方都包含了screen.fill?听起来之前的图像在绘制下一个图像之前没有被清理,从而产生了拖动效果。

一个例子:

screen.fill((0,0,0))

如果你在blitting之前放置它,它应该将屏幕填满黑色(清除上一个图像),然后在新位置模仿图像。