簡體   English   中英

在 PyGame 中,Sprite 顯示但不會切換動畫或隨按鍵移動

[英]Sprite displays but won't switch animations or move with key presses in PyGame

構建一個簡單的 PyGame,我需要玩家精靈根據箭頭鍵移動和更改圖像。 我在 Sprite1 class 中添加了面向 function 並在游戲循環中按鍵的 if 語句中調用它,希望每次按下按鍵時它都會改變圖像,但它似乎不想在按鍵后更新精靈的圖像. 是我面對function的問題嗎? 還是用別的東西?

import pygame 
pygame.init() 

#game Window 
screen_width = 800
screen_height = 400

screen = pygame.display.set_mode((screen_width, screen_height)) 
pygame.display.set_caption('Warrior Quest') 

#game variables
main_menu = True

#background image
background_img =  pygame.image.load('background.png')
#button images 
start_img = pygame.image.load('startbutton.PNG')
cancel_img = pygame.image.load('cancelbutton.PNG')
title_img = pygame.image.load('warriorquestTile.PNG')

#background function 
def draw_bg(): 
  screen.blit(background_img, (0,0)) 

 
class Sprite1(pygame.sprite.Sprite):
  def __init__(self): 
    super().__init__()  
    self.faceUp = True
    self.faceDown = False
    self.faceLeft = False 
    self.faceRight = False
    self.image = pygame.image.load('player.png').convert_alpha()
    self.rect = self.image.get_rect()
  
  def draw(self):
    screen.blit(self.image, self.rect)

  def facing(self):
    if self.faceUp == True:
      self.image = pygame.image.load('player.png').convert_alpha()
      self.rect.w = self.image.get_rect().w
      self.rect.h = self.image.get_rect().h
    elif self.faceDown == True:
      self.image = pygame.image.load('playerDown.png').convert_alpha()
      self.rect = self.image.get_rect()
    elif self.faceLeft == True:
      self.image = pygame.image.load('playerLeft.png').convert_alpha()
      self.rect = self.image.get_rect()
    elif self.faceRight == True:
      self.image = pygame.image.load('playerRight.png').convert_alpha()
      self.rect = self.image.get_rect()
  

#setup player 
player = Sprite1()
player.rect.x = 400
player.rect.y = 380

class Button():
  def __init__(self,x,y,image):
    self.image = image
    self.rect = self.image.get_rect() 
    self.rect.x = x 
    self.rect.y = y 
    self.clicked = False 
   
  def draw(self):
    action = False
    
    #get mouse position
    pos = pygame.mouse.get_pos() 
    
    if self.rect.collidepoint(pos):
      if pygame.mouse.get_pressed()[0] == 1 and self.clicked == False:
        action = True 
        self.clicked = True 

    if pygame.mouse.get_pressed()[0] == 0:
      self.clicked = False 

    #draw Button 
    screen.blit(self.image, self.rect) 

    return action

 
#create buttons 
start_button = Button(screen_width // 2 -350, screen_height // 2, start_img) 
cancel_button = Button(screen_width // 2 + 150, screen_height // 2, cancel_img)
title_button = Button(300,400,title_img)
#game loop running 
running = True
while running:
  
  draw_bg()
  if main_menu == True: 
    
    if start_button.draw():
      main_menu = False
    if cancel_button.draw():
      running = False 
  else:  
    player.draw()
   
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player.rect.x>5:
      player.faceLeft = True 
      player.facing()
      player.rect.x -= 5
    if keys[pygame.K_RIGHT] and player.rect.x<790:
      player.faceRight = True 
      player.facing()
      player.rect.x += 5
    if keys[pygame.K_UP] and player.rect.y>10:
      player.faceUp = True 
      player.facing()
      player.rect.y -= 5
    if keys[pygame.K_DOWN]and player.rect.y<395:
      player.faceDown = True 
      player.facing()
      player.rect.y += 5
  
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      running = False
  
  pygame.display.update() 
pygame.quit()  

    

每次更新精靈時,都會將其矩形重置為圖像的矩形:

    if self.faceUp == True:
      self.image = pygame.image.load('player.png').convert_alpha()
      self.rect = self.image.get_rect()

相反,只更新寬度和高度:

    if self.faceUp == True:
      self.image = pygame.image.load('player.png').convert_alpha()
      self.rect.w = self.image.get_rect().w
      self.rect.h = self.image.get_rect().h

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. Surface在屏幕上的 position 處被blit 矩形的 position 可以通過關鍵字參數指定。 例如,可以使用關鍵字參數center指定矩形的中心。 這些關鍵字參數在返回之前應用於pygame.Rect的屬性(有關關鍵字參數的完整列表,請參見pygame.Rect )。

通過保持矩形的中心 position 來保持精靈的 position:

if self.faceUp == True:
    self.image = pygame.image.load('player.png').convert_alpha()
    self.rect = self.image.get_rect(center = self.rect.center)

此外,不要在應用程序循環中加載圖像。 加載圖像非常耗時,因為必須讀取和解釋圖像文件。 在應用程序開始時加載一次圖像:

設置新的face*屬性是不夠的,您還必須重置其他face*屬性。 我建議使用具有不同狀態的單個屬性face

class Sprite1(pygame.sprite.Sprite):
  def __init__(self): 
    super().__init__()  
    self.player_img = pygame.image.load('player.png').convert_alpha()
    self.player_down_img = pygame.image.load('playerDown.png').convert_alpha()
    self.player_left_img = pygame.image.load('playerLeft.png').convert_alpha()
    self.player_right_img = pygame.image.load('playerRight.png').convert_alpha()
    self.face = "up" 
    self.image = self.player_img
    self.rect = self.image.get_rect()
  
  def draw(self):
    screen.blit(self.image, self.rect)

  def facing(self):
    if self.face == "up" :
      self.image = self.player_img
    elif self.face == "down":
      self.image = self.player_down_img
    elif self.face == "left":
      self.image = self.player_left_img
    elif self.face == "right":
      self.image = self.player_right_img
    self.rect = self.image.get_rect(center = self.rect.center)
running = True
while running:
    # [...]

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT] and player.rect.x>5:
      player.face = "up"
      player.facing()
      player.rect.x -= 5
    if keys[pygame.K_RIGHT] and player.rect.x<790:
      player.face = "down" 
      player.facing()
      player.rect.x += 5
    if keys[pygame.K_UP] and player.rect.y>10:
      player.face = "left"
      player.facing()
      player.rect.y -= 5
    if keys[pygame.K_DOWN]and player.rect.y<395:
      player.face = "right": 
      player.facing()
      player.rect.y += 5

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM