简体   繁体   中英

Pygame screen display issue

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

Whenever I use pygame, even simple displays like this are skewed for me. it shows 0,0 at around the middle of my display screen and i dont know why. Basically, it is showing - x values on the x axis help! I am using python 2.7 and this seems to not be a coding issue, but rather something else. please help! ty

I've not been able to replicate your problem with the above code in Python 2.7.12, the image is a red, fifty-pixel square:

演示代码

Here's an extended demo that will draw the image around the cursor position based on the mouse button clicked. Perhaps that'll help you get towards the behaviour you're after.

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

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