我正在尝试使用我自己的Pacman版本,目前正试图让窗口调整大小。我在这里读到了答案:https://reformatcode.com/code/python/pygame-how-do-i-resize-a-surface-and-keep-all-objects-within-proportionate-to-the-new-window-size。但在我的情况下它并没有帮助我。在我有2个屏幕,屏幕和假屏幕。如果我把所有东西设置为假屏幕上的blit它们出现但不更新它们(pacman不会移动)。如果我将它们设置为屏幕,它们会出现并更新,但不要调整大小。任何帮助或建议将不胜感激。顺便说一句,它仍然是一项进步的工作
import pygame
from Variables import pelletspawns #imports pellet spwans list from different script
from pygame.locals import *
#Initialisation
pygame.init()
pygame.display.set_caption("Pacman")
myfont = pygame.font.SysFont("monospace", 15)
screen = pygame.display.set_mode((448, 576)) #Creates screen object
fake_screen = screen.copy()
pic = pygame.surface.Surface((50,50))
pic.fill((255,100,100))
clock = pygame.time.Clock() #Creates clock object
Fin = False
#Declaring Variables
FPS = 60
MoveLeft = pygame.K_LEFT
MoveRight = pygame.K_RIGHT
MoveUp = pygame.K_UP
MoveDown = pygame.K_DOWN
#Load images
PACMAN_MAP = pygame.image.load("images/pacman_map.png").convert_alpha()
PACMANSPRITE = pygame.image.load("images/pacman.png").convert_alpha()
#pacmans class
class SpriteClass(pygame.sprite.Sprite):
def __init__(self, image, x, y):
self.image = image
self.y=y
self.x=x
self.rect = self.image.get_rect()
self.rect.left = self.x
self.rect.top = self.y
self.rect.width=16
self.rect.height=16
#draws Pacman
def draw(self, surface):
# blit yourself at your current position
surface.blit(self.image, (self.x, self.y))
# move pacman
def movement(self):
pressed= pygame.key.get_pressed()
if pressed[MoveUp]:
self.y -= 2
print('key pressed')
if pressed[MoveDown]:
self.y += 2
print('key pressed')
if pressed[MoveLeft]:
self.x -= 2
print('key pressed')
if pressed[MoveRight]:
self.x += 2
print('key pressed')
self.rect.left = self.x
self.rect.top = self.y
print(self.x,self.y)
#instances Pacman
Pacman = SpriteClass(PACMANSPRITE, 216 ,416)
#Function to spawn pellets
def SpawnPellets(pelletspawns):
pelletx=0 #the temp x co-ordinate for the pellet to spawn
pellety= -8 #the temp y co-ordinate for the pellet to spawn (starts at -ve 0.5(gridscpare) for allignment
for row in pelletspawns:
#adds 1 grid space to correctly align spawns
pellety += 16
for pellet in row:
pelletx= 16*pellet
pelletx -=8
pygame.draw.circle(screen,(255, 204, 153), (pelletx, pellety) , 5)
#main game loop
while not Fin:
#For event is used to close the program
for event in pygame.event.get():
if event.type == QUIT:
pygame.display.quit()
if event.type == VIDEORESIZE:
screen = pygame.display.set_mode(event.dict['size'],RESIZABLE)
screen.blit(pygame.transform.scale(fake_screen, event.dict['size']), (0, 0))
pygame.display.flip()
#calls movement function
Pacman.movement()
#blits pacman map as background
screen.blit(PACMAN_MAP, (0, 0))
#draws pacman
Pacman.draw(screen)
#Spawns pellets
SpawnPellets(pelletspawns)
#draws screen
pygame.display.flip()
clock.tick(FPS)
答案 0 :(得分:1)
我想通了,我需要搬家
screen.blit(pygame.transform.scale(fake_screen, event.dict['size']), (0, 0))
进入clock.tick
之上的核心游戏循环。我之前尝试过这个但遇到了一个关键错误,因为event.dict['size']
没有事件来获取大小,所以我在for event in pygame.event.get():
循环中创建了一个变量,然后将该变量传递到它所要求的位置为event.dict['size']
。这是我改变的代码部分:
#main game loop
while not Fin:
#For event is used to close the program
for event in pygame.event.get():
if event.type == QUIT:
pygame.display.quit()
if event.type == VIDEORESIZE:
size = event.dict['size']
screen = pygame.display.set_mode(size,RESIZABLE)
#calls movement function
Pacman.movement()
#blits pacman map as background
fake_screen.blit(PACMAN_MAP, (0, 0))
#draws pacman
Pacman.draw(fake_screen)
#Spawns pellets
SpawnPellets(pelletspawns)
#draws screen
screen.blit(pygame.transform.scale(fake_screen, size), (0, 0))
pygame.display.flip()
clock.tick(FPS)