Python Pygame如何摆脱动画周围的盒子?
我有一个键盘动画,它可以工作,但我的问题是,图像周围有一个框,有没有办法删除这个?代码张贴在上面,请随时向我展示代码示例,因为我是pygames新手。这可能是图像质量问题,或者您必须使用或加载图像Python Pygame如何摆脱动画周围的盒子?,python,animation,pygame,Python,Animation,Pygame,我有一个键盘动画,它可以工作,但我的问题是,图像周围有一个框,有没有办法删除这个?代码张贴在上面,请随时向我展示代码示例,因为我是pygames新手。这可能是图像质量问题,或者您必须使用或加载图像 import pygame class Sprite: def __init__(self, x, y, curren_time): self.rect = pygame.Rect(x, y, 100, 110) self.images = []
import pygame
class Sprite:
def __init__(self, x, y, curren_time):
self.rect = pygame.Rect(x, y, 100, 110)
self.images = []
#Tells pygame the image list that you want to stitch together
for x in range(10):
img = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST6/Cernunnos" + str(x) +".PNG")
#Append the image to each other to create the animation effect
self.images.append( img )
self.current_image = 0
self.time_num = 100
self.time_target = curren_time + self.time_num
def update(self, curren_time):
if curren_time >= self.time_target:
self.time_target = curren_time + self.time_num
self.current_image += 1
if self.current_image == len(self.images):
self.current_image = 0
def render(self, window):
window.blit(self.images[self.current_image], self.rect)
#Colors
black = (0,0,0)
white = (255,255,255)
def main():
pygame.init()
window = pygame.display.set_mode((800,600))
pygame.display.set_caption("Sprites")
move_x, move_y = 0, 0
clock = pygame.time.Clock()
curren_time = pygame.time.get_ticks()
player = Sprite(110,100,curren_time)
font = pygame.font.SysFont(None, 150)
pause_text = font.render("PAUSE",1,white)
pause_rect = pause_text.get_rect( center = window.get_rect().center )
#Adding how many places the image moves when using the key function
state_game = True
state_pause = False
#So while True
while state_game:
curren_time = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
state_game = False
elif event.type == pygame.KEYDOWN:
if event.key ==pygame.K_ESCAPE:
state_game = False
elif event.key == pygame.K_SPACE:
state_pause = not state_pause
if event.key == pygame.K_LEFT:
move_x = -3
elif event.key == pygame.K_RIGHT:
move_x = 3
elif event.key == pygame.K_UP:
move_y = -3
elif event.key == pygame.K_DOWN:
move_y = 3
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_LEFT, pygame.K_RIGHT):
move_x = 0
elif event.key in (pygame.K_UP, pygame.K_DOWN):
move_y = 0
if not state_pause:
player.rect.x += move_x
player.rect.y += move_y
player.update(curren_time)
#Fills the window with a color
window.fill(white)
player.render(window)
if state_pause:
window.blit(pause_text,pause_rect)
pygame.display.flip()
clock.tick(50)
pygame.quit()
if __name__ == '__main__':
main()
#Code created by furas
编辑: 应该是:
img = pygame.image.load("...").convert()
img = pygame.image.load("...").convert_alpha()
你能将其添加到当前代码中吗?或者我必须更改它,因为当我添加时,它当前似乎没有拾取它。我添加了示例,但可能图像有问题-我无法测试它。
Traceback(最近一次调用):文件“C:\Users\Trevor\SkyDrive\Documents\TEST6\TEST3.py”,第138行,在main()文件中“C:\Users\Trevor\SkyDrive\Documents\TEST6\TEST3.py”,第120行,在主播放器渲染(窗口)文件“C:\Users\Trevor\SkyDrive\Documents\TEST6\TEST3.py”,第38行,在渲染窗口.blit(self.images[self.current\u image],self.rect)中TypeError:参数1必须是pygame.Surface,而不是内置函数或方法
是否在结尾使用了转换alpha()
和()
?
for x in range(10):
img = pygame.image.load("C:/Users/Trevor/SkyDrive/Documents/TEST6/Cernunnos" + str(x) +".PNG").convert_alpha()