Python Pygame中的图像在屏幕上拖动像素

Python Pygame中的图像在屏幕上拖动像素,python,image,pygame,Python,Image,Pygame,我一直在尝试在pygame中创建一个移动的背景(由箭头键控制)。我的问题是,当图像通过窗口边界时,会产生拖动效果。所有结束像素分布在窗口的其余部分 这是我的运动: if pressed[pygame.K_RIGHT]: if activate == True: x-=1 left = False right = True bgx-=1 bg2x-=1 if pressed[p

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

这是我的运动:

    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
感谢您的帮助-谢谢

我知道我不是故意的,但这是我的全部代码

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()
import pygame,sys
从pygame.locals导入*
导入操作系统
game=“”
def home():
全球游戏
pygame.init()
screen=pygame.display.set_模式((1000750))
完成=错误
红色=(255,0,0)
绿色=(0255,0)
蓝色=(0,0255)
白色=(255255)
yList=[]
xList=[]
pygame.mouse.set_可见(False)
对于范围(255)内的l:
yList.append(-100)
xList.append(-100)
clock=pygame.time.clock()
信使=真
img_rect=pygame.transform.scale(pygame.image.load(“space.jpg”),(1000750)).get_rect()
bg=pygame.Surface((img_-rect.width,img_-rect.height),pygame.SRCALPHA)
背景填充((0,0,0,0))
blit(pygame.transform.scale(pygame.image.load(“space.jpg”),(1000750)),img_rect)
img_rect=pygame.transform.scale(pygame.image.load(“space.jpg”),(1000750)).get_rect()
bg2=pygame.Surface((img_-rect.width,img_-rect.height),pygame.SRCALPHA)
bg2.填充((0,0,0,0))
blit(pygame.transform.scale(pygame.image.load(“space.jpg”),(1000750)),imgrect)
bgx=1000
bgy=0
bg2x=0
bg2y=0
右=真
左=假
激活=错误
x=500
y=450
比例=100
def messenger(信息、颜色、尺寸):
全局文本
font=pygame.font.SysFont(“comicsansms”,size)
text=font.render(msg、True、color)
返回(文本)
虽然没有这样做:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
pressed=pygame.key.get_pressed()
如果按下[pygame.K_SPACE]:
激活=真
y=450
x=500
如果按下[pygame.K_RIGHT]:
如果激活==真:
x-=1
左=假
右=真
bgx-=1
bg2x-=1
如果按下[pygame.K_LEFT]:
如果激活==真:
x+=1
左=真
右=假
bgx+=1
bg2x+=1
如果bgx>=1000且right==True:
bgx=bg2x-1000
如果bg2x>=1000且right==True:
bg2x=bgx-1000

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

例如:

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


如果您将其放在blit之前,则在将您的图像放在其新位置之前,它应该将屏幕填充为黑色(清除以前的图像)。

现在显示的不是拖动黑色屏幕,而是拖动黑色屏幕。您是否确保fill命令位于blit图像的上方?
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()