能否让游戏显示在屏幕中央,一旦按下运行键。如果可以,怎么做?

问题描述 投票:0回答:1
#enable pygame mode
import pygame
pygame.init()

#create screen
screen = pygame.display.set_mode((1000,600))

#Title + Logo
pygame.display.set_caption("Space Invader")
icon = pygame.image.load("chicken.png")
pygame.display.set_icon(icon)

#Player icon
player_icon = pygame.image.load("spaceship.png")
playerX = 400
playerY = 500

player_changeX = 0
player_changeY = 0

def player(x, y):
    screen.blit(player_icon, (x, y))


surface.blit(image,((1920/2)-(image.get_width()/2),(1080/2)- 
   (image.get_height()/2)))



#game loop
running = True
while running:

    # backround colour  RGB
    screen.fill((0, 0, 0))


    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        #If key pressed check wether its right or left
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player_changeX = -1
            if event.key == pygame.K_RIGHT:
                player_changeX = 1
            if event.key == pygame.K_UP:
                player_changeY = -1
            if event.key == pygame.K_DOWN:
                player_changeY = 1
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key ==pygame.K_RIGHT:
            player_changeX = 0
        if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
            player_changeY = 0
# If player reaches boarder
    if playerX >= 936:
        player_changeX = -1
    if playerX <= 0:
        player_changeX = 1
    if playerY <= 0:
        player_changeY = 1
    if playerY >= 550:
        player_changeY = -1


#Player change in coordinates
    playerX += player_changeX
    playerY += player_changeY

    player(playerX, playerY)
    pygame.display.update()

我正在创建一个简单的游戏,因为我刚刚进入编程,我想知道你是否可以使游戏屏幕出现在你自己屏幕的中心,因为当我运行它时,它一直出现在我的屏幕底部,然后我必须手动将其移动到中心。如果可以请你告诉我怎么做。希望我的问题表述的够好。谢谢你的任何帮助。

python pygame pycharm
1个回答
0
投票

这要看情况,如果是图片你可以做到。

surface.blit(image,((screenwidth/2)-(image.get_width()/2),(screenheight/2)-(image.get_height()/2)))

请告诉我这是不同的

© www.soinside.com 2019 - 2024. All rights reserved.