我一直在嘗試在pygame中制作一款類似于塔防游戲的游戲。本質上,有僵尸走在路上,你必須射擊它們(十字準線是你的滑鼠)。我有 3 個檔案:crosshair.py、spritesheet.py 和 main.py。我可以從我的精靈表中獲得行走影片,但是,當我希望它向上或向下行走時,它所看的方向不會改變。
這是精靈表的樣子:

這是我的代碼:
spritesheet.py:
import pygame
class SpriteSheet(pygame.sprite.Sprite):
def __init__(self, width, height, sheet):
super().__init__()
self.action = 2
self.width = width
self.height = height
self.sheet = sheet
def get_image(self):
self.animation_list = []
for _ in range(4):
temp_list = []
for count in range(4):
self.image = pygame.Surface((self.width, self.height))
self.image.blit(self.sheet, (0, 0), (count * self.width, self.action * self.height, self.width, self.height))
self.image.set_colorkey('Black')
temp_list.append(self.image)
self.animation_list.append(temp_list)
主要.py:
import pygame
from crosshair import Crosshair
from spritesheet import SpriteSheet
pygame.init()
# Clock/Time
clock = pygame.time.Clock()
FPS = 60
# Make Screen
screen_w = 1000
screen_h = 650
screen = pygame.display.set_mode((screen_w, screen_h))
crosshair = Crosshair('crosshair_white.png', screen)
# Load background
bg = pygame.image.load('background.jpg').convert_alpha()
bg = pygame.transform.scale(bg, (screen_w, screen_h))
# Handle Zombie Sheet
zombie_sheet = pygame.image.load('zombie_img.png').convert_alpha()
sprite_sheet = SpriteSheet(32, 48, zombie_sheet)
sprite_sheet.get_image()
frame = 0
run = True
zombie_x = 0
zombie_y = 400
while run:
screen.fill('Black')
screen.blit(bg, (0, 0))
# Zombie Management
screen.blit(sprite_sheet.animation_list[sprite_sheet.action][int(frame)], (zombie_x, zombie_y))
if frame > 3.9:
frame = 0
else:
frame = 0.07
dx = 0
dy = 0
if zombie_x < 300:
dx = 0.5
elif 300 <= zombie_x < 390:
dx = 0.5
dy = -0.25
elif 390 <= zombie_x < 460 and zombie_y > 210:
dy = -0.5
elif zombie_x < 460 and zombie_y <= 210:
dx = 0.5
dy = -0.25
elif 460 <= zombie_x < 510:
dx = 0.5
elif 510 <= zombie_x < 570:
dx = 0.5
dy = 0.25
elif zombie_y < 270:
dy = 0.5
elif 570 <= zombie_x < 630 and zombie_y >= 270:
dx = 0.5
dy = 0.5
elif 630 <= zombie_x < 760:
dx = 0.5
elif 760 <= zombie_x < 830:
dx = 0.5
dy = 0.5
elif zombie_x >= 830:
dx = 0.5
zombie_x = dx
zombie_y = dy
crosshair.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
crosshair.shoot()
pygame.display.update()
clock.tick(FPS)
pygame.quit()
我不會發布 crosshair.py 的代碼,因為我不認為它是必要的。
基本上,我想通過以下方式更改此處的操作sprite_sheet.action = 3:
elif 390 <= zombie_x < 460 and zombie_y > 210:
------->
dy = -0.5
However, it doesn't change, and I know this problem isn't due to the surfaces I made being incorrect, because if I manually change self.action to a different number, it works.
If any other information is needed, I'll be happy to provide, but just know that these are the only two files I have except for crosshair.py.
Thanks in advance.
uj5u.com熱心網友回復:
我沒有測驗它,但我認為你為所有方向創建相同的影像,get_image()因為它們都使用相同的self.action
for _ in range(4):
# ... code ...
self.image.blit(self.sheet, (0, 0),
(count * self.width,
self.action * self.height, # <--- self.action
self.width,
self.height))
但他們應該使用來自for _ in range(4)
for direction in range(4): # <--- direction
# ... code ...
self.image.blit(self.sheet, (0, 0),
(count * self.width,
direction * self.height, # <--- direction
self.width,
self.height))
全功能:
def get_image(self):
self.animation_list = []
for direction in range(4): # <--- direction
temp_list = []
for count in range(4):
self.image = pygame.Surface((self.width, self.height))
self.image.blit(self.sheet, (0, 0),
(count * self.width,
direction * self.height, # <--- direction from `for
self.width,
self.height))
self.image.set_colorkey('Black')
temp_list.append(self.image)
self.animation_list.append(temp_list)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/438389.html
標籤:python animation pygame sprite
上一篇:標志影片向你走來
