为什么当我在这个 pygame 脚本中没有按下任何东西时,精灵图像没有重置为默认值?
Why is the sprite image not resetting to default when I'm not pressing anything in this pygame script?
import pygame
from config import *
from spritesheet import *
import math
class Player(pygame.sprite.Sprite):
def __init__(self, game, x, y):
self.game = game
self._layer = PLAYER_LAYER
self.groups = self.game.all_sprites
pygame.sprite.Sprite.__init__(self, self.groups)
self.x = x * TILESIZE2
self.y = y * TILESIZE2
self.width = PLAYERSIZEX
self.height = PLAYERSIZEY
self.x_change = 0
self.y_change = 0
self.facing = 'right'
self.animation_loop = 0
self.allowed_to_animate = 1
self.image = self.game.mario.get_sprite(0, 8, self.width, self.height)
self.image = pygame.transform.scale(self.image, (TILESIZE2, TILESIZE2))
self.rect = self.image.get_rect()
self.rect.x = self.x
self.rect.y = self.y
self.vel_y = 0
self.jump = False
self.inAir = False
def update(self):
self.movement()
self.animate()
self.rect.x += self.x_change
self.collide_blocks('x')
self.rect.y += self.vel_y
self.collide_blocks('y')
if self.facing == "right":
self.game.mario.get_sprite(0, 8, self.width, self.height)
self.x_change = 0
self.image = pygame.transform.scale(self.image, (TILESIZE2, TILESIZE2))
def movement(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
for sprite in self.game.all_sprites:
sprite.rect.x += PLAYER_SPEED
self.x_change -= PLAYER_SPEED
self.facing = "left"
if keys[pygame.K_RIGHT]:
for sprite in self.game.all_sprites:
sprite.rect.x -= PLAYER_SPEED
self.x_change += PLAYER_SPEED
self.facing = "right"
if keys[pygame.K_z] and self.jump == False and self.inAir == False:
self.vel_y = -15
self.jump = True
self.inAir = True
if keys[pygame.K_z] == False:
self.jump = False
self.vel_y += 1
if self.vel_y > 10:
self.vel_y = 10
def collide_blocks(self, direction):
if direction == "x":
hits = pygame.sprite.spritecollide(self, self.game.blocks, False)
if hits:
if self.x_change > 0:
self.rect.x = hits[0].rect.left - self.rect.width
for sprite in self.game.all_sprites:
sprite.rect.x += PLAYER_SPEED
if self.x_change < 0:
self.rect.x = hits[0].rect.right
for sprite in self.game.all_sprites:
sprite.rect.x -= PLAYER_SPEED
if direction == "y":
hits = pygame.sprite.spritecollide(self, self.game.blocks, False)
if hits:
if self.vel_y > 0:
self.rect.y = hits[0].rect.top - self.rect.height
self.inAir = False
if self.vel_y < 0:
self.rect.y = hits[0].rect.bottom
self.inAir = False
def animate(self):
run_anim_right = [self.game.mario.get_sprite(20, 8, self.width, self.height),
self.game.mario.get_sprite(38, 8, self.width, self.height),
self.game.mario.get_sprite(56, 8, self.width, self.height),]
run_anim_left = [self.game.mario.get_sprite(76, 25, self.width, self.height),
self.game.mario.get_sprite(58, 25, self.width, self.height),
self.game.mario.get_sprite(40, 25, self.width, self.height),]
jump_right = self.game.mario.get_sprite(96, 8, self.width, self.height)
jump_left = self.game.mario.get_sprite(0, 25, self.width, self.height)
if self.facing == "right":
if self.inAir == False:
if self.x_change == 0:
self.animation_loop = 0
self.game.mario.get_sprite(0, 8, self.width, self.height)
self.image = pygame.transform.scale(self.image, (TILESIZE2, TILESIZE2))
else:
self.image = run_anim_right[math.floor(self.animation_loop)]
self.animation_loop += 0.1
if self.animation_loop >= 3:
self.animation_loop = 0
else:
self.image = jump_right
if self.facing == "left":
if self.inAir == False:
if self.x_change == 0:
self.animation_loop = 0
self.game.mario.get_sprite(96, 25, self.width, self.height)
self.image = pygame.transform.scale(self.image, (TILESIZE2, TILESIZE2))
else:
self.image = run_anim_left[math.floor(self.animation_loop)]
self.animation_loop += 0.1
if self.animation_loop >= 3:
self.animation_loop = 0
else:
self.image = jump_left
我已经尝试并再次尝试将图像设置为默认值 (self.game.mario.get_sprite(0, 8, self.width, self.height)
,但我似乎没有做任何工作。我已经尝试添加所有内容,从尝试将 x 速度设置为 0(没有工作),等等。既然你们能以某种方式解决这些问题,我真的需要帮助
(完整程序在这里:https://replit.com/@PikadaveStudios/Mario-Creepypasta-Game?v=1)
默认图像不会神奇地恢复。需要保留默认图片,在animate
开头设置默认图片:
class Player(pygame.sprite.Sprite):
def __init__(self, game, x, y):
# [...]
self.default_image = self.game.mario.get_sprite(0, 8, self.width, self.height)
self.image = self.default_image.copy()
# [...]
def animate(self):
self.image = self.default_image.copy()
# [...]
import pygame
from config import *
from spritesheet import *
import math
class Player(pygame.sprite.Sprite):
def __init__(self, game, x, y):
self.game = game
self._layer = PLAYER_LAYER
self.groups = self.game.all_sprites
pygame.sprite.Sprite.__init__(self, self.groups)
self.x = x * TILESIZE2
self.y = y * TILESIZE2
self.width = PLAYERSIZEX
self.height = PLAYERSIZEY
self.x_change = 0
self.y_change = 0
self.facing = 'right'
self.animation_loop = 0
self.allowed_to_animate = 1
self.image = self.game.mario.get_sprite(0, 8, self.width, self.height)
self.image = pygame.transform.scale(self.image, (TILESIZE2, TILESIZE2))
self.rect = self.image.get_rect()
self.rect.x = self.x
self.rect.y = self.y
self.vel_y = 0
self.jump = False
self.inAir = False
def update(self):
self.movement()
self.animate()
self.rect.x += self.x_change
self.collide_blocks('x')
self.rect.y += self.vel_y
self.collide_blocks('y')
if self.facing == "right":
self.game.mario.get_sprite(0, 8, self.width, self.height)
self.x_change = 0
self.image = pygame.transform.scale(self.image, (TILESIZE2, TILESIZE2))
def movement(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
for sprite in self.game.all_sprites:
sprite.rect.x += PLAYER_SPEED
self.x_change -= PLAYER_SPEED
self.facing = "left"
if keys[pygame.K_RIGHT]:
for sprite in self.game.all_sprites:
sprite.rect.x -= PLAYER_SPEED
self.x_change += PLAYER_SPEED
self.facing = "right"
if keys[pygame.K_z] and self.jump == False and self.inAir == False:
self.vel_y = -15
self.jump = True
self.inAir = True
if keys[pygame.K_z] == False:
self.jump = False
self.vel_y += 1
if self.vel_y > 10:
self.vel_y = 10
def collide_blocks(self, direction):
if direction == "x":
hits = pygame.sprite.spritecollide(self, self.game.blocks, False)
if hits:
if self.x_change > 0:
self.rect.x = hits[0].rect.left - self.rect.width
for sprite in self.game.all_sprites:
sprite.rect.x += PLAYER_SPEED
if self.x_change < 0:
self.rect.x = hits[0].rect.right
for sprite in self.game.all_sprites:
sprite.rect.x -= PLAYER_SPEED
if direction == "y":
hits = pygame.sprite.spritecollide(self, self.game.blocks, False)
if hits:
if self.vel_y > 0:
self.rect.y = hits[0].rect.top - self.rect.height
self.inAir = False
if self.vel_y < 0:
self.rect.y = hits[0].rect.bottom
self.inAir = False
def animate(self):
run_anim_right = [self.game.mario.get_sprite(20, 8, self.width, self.height),
self.game.mario.get_sprite(38, 8, self.width, self.height),
self.game.mario.get_sprite(56, 8, self.width, self.height),]
run_anim_left = [self.game.mario.get_sprite(76, 25, self.width, self.height),
self.game.mario.get_sprite(58, 25, self.width, self.height),
self.game.mario.get_sprite(40, 25, self.width, self.height),]
jump_right = self.game.mario.get_sprite(96, 8, self.width, self.height)
jump_left = self.game.mario.get_sprite(0, 25, self.width, self.height)
if self.facing == "right":
if self.inAir == False:
if self.x_change == 0:
self.animation_loop = 0
self.game.mario.get_sprite(0, 8, self.width, self.height)
self.image = pygame.transform.scale(self.image, (TILESIZE2, TILESIZE2))
else:
self.image = run_anim_right[math.floor(self.animation_loop)]
self.animation_loop += 0.1
if self.animation_loop >= 3:
self.animation_loop = 0
else:
self.image = jump_right
if self.facing == "left":
if self.inAir == False:
if self.x_change == 0:
self.animation_loop = 0
self.game.mario.get_sprite(96, 25, self.width, self.height)
self.image = pygame.transform.scale(self.image, (TILESIZE2, TILESIZE2))
else:
self.image = run_anim_left[math.floor(self.animation_loop)]
self.animation_loop += 0.1
if self.animation_loop >= 3:
self.animation_loop = 0
else:
self.image = jump_left
我已经尝试并再次尝试将图像设置为默认值 (self.game.mario.get_sprite(0, 8, self.width, self.height)
,但我似乎没有做任何工作。我已经尝试添加所有内容,从尝试将 x 速度设置为 0(没有工作),等等。既然你们能以某种方式解决这些问题,我真的需要帮助
(完整程序在这里:https://replit.com/@PikadaveStudios/Mario-Creepypasta-Game?v=1)
默认图像不会神奇地恢复。需要保留默认图片,在animate
开头设置默认图片:
class Player(pygame.sprite.Sprite):
def __init__(self, game, x, y):
# [...]
self.default_image = self.game.mario.get_sprite(0, 8, self.width, self.height)
self.image = self.default_image.copy()
# [...]
def animate(self):
self.image = self.default_image.copy()
# [...]