2017-06-12 42 views
1

我一直試圖在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 

而這正是我在圖像的blit:

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

回答

0

你包括screen.fill地方?這聽起來像是先前的圖像在繪製下一個圖像之前沒有被清理,創建了拖動效果。

一個例子:

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

如果你把你的blitting之前,它應該充滿屏幕黑色(清除以前的圖像),在其新位置塊傳輸您的圖片前。

+0

現在不是拖動黑屏而是顯示 –

+0

您是否確定填充命令位於blit圖像的上方? – Xanuthatusu