Pygame屏幕显示问题

时间:2017-12-05 01:55:40

标签: python display

import pygame
pygame.init()


display_width = (640)
display_height = (480)

title = pygame.display.set_caption("test")
IMG = pygame.image.load("image.png")
screen = pygame.display.set_mode((display_width,display_height))

screen.blit(IMG,(1,1))

pygame.display.update()

每当我使用pygame时,即使是这样的简单显示也会让我感到歪斜。它在我的显示屏中间显示0,0,我不知道为什么。基本上,它显示 - x轴上的x值有帮助! 我使用的是python 2.7,这似乎不是编码问题,而是其他问题。请帮忙! TY

1 个答案:

答案 0 :(得分:0)

我无法使用Python 2.7.12中的上述代码复制您的问题,图像为红色,50像素的正方形:

Demo code

这是一个扩展演示,它将根据点击的鼠标按钮在光标位置周围绘制图像。也许这会帮助你实现你所追求的行为。

import pygame

if __name__ == "__main__":
    pygame.init()
    screen_width, screen_height = 640, 480
    screen = pygame.display.set_mode((screen_width, screen_height))
    pygame.display.set_caption('Blit Demo')
    clock = pygame.time.Clock() #for limiting FPS
    FPS = 10
    exit_demo = False
    # start with a white background
    screen.fill(pygame.Color("white"))
    img = pygame.image.load("image.png")
    width, height = img.get_size()
    pos = (1,1)  # initial position to draw the image
    # main loop
    while not exit_demo:
        for event in pygame.event.get():            
            if event.type == pygame.QUIT:
                exit_demo = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    # fill the screen with white, erasing everything
                    screen.fill(pygame.Color("white"))
            elif event.type == pygame.MOUSEBUTTONUP:
                if event.button == 1:  # left
                    pos = (event.pos[0] - width, event.pos[1] - height)                   
                elif event.button == 2: # middle
                    pos = (event.pos[0] - width // 2, event.pos[1] - height // 2)
                elif event.button == 3: # right
                    pos = event.pos

        # draw the image here
        screen.blit(img, pos)
        # update screen
        pygame.display.update()
        clock.tick(FPS)
    pygame.quit()
    quit()