为什么当我不按此PyGame脚本中的任何内容时,Sprite映像不会重置为默认值?
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)
,但我似乎没有什么可以做的。 've尝试添加所有内容,从尝试使X速度到0(无效),并且喜欢所有内容,因为您可以以某种方式弄清楚这些东西,我确实需要帮助
(在这里完整程序:)
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
I've tried and tried again to set the image to default (self.game.mario.get_sprite(0, 8, self.width, self.height)
but nothing I seem to do works. I've tried adding everything, from trying to make the x speed to 0 (didnt work), and like, everything. Since you guys can somehow figure this stuff out, and I really need help
(full program here: https://replit.com/@PikadaveStudios/Mario-Creepypasta-Game?v=1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认图像不会神奇地回来。您需要保留默认图像,并将默认图像设置在
animate
的开头:The default image doesn't magically come back. You need to keep the default image and set the default image at the beginning of
animate
: