我试图“动画”一个精灵,只要加载一组4个图像反复,而分配的左/右按钮被按下。它基本上是有效的,只是图像一直在闪烁,并不像我想的那样“切换”。它的速度也大大减慢。作为一个Pygame/Python的初学者,我的大脑完全被炸飞了。在
import pygame
import os
from test_menu import drawmenu
from game_over import player1wins
from game_over import player2wins
from game_over import tie
pygame.init()
while True:
screen_size = (1080, 400)
screen = pygame.display.set_mode(screen_size)
background = (255,255,255)
image = pygame.image.load("background(1).png")
imagerect = image.get_rect()
wall_left = pygame.Rect(0,0,1,950)
wall_right = pygame.Rect(1080,0,1,950)
square1 = pygame.Rect(750, 350, 20, 20)
square2 = pygame.Rect(330, 350, 20, 20)
IMAGE = pygame.image.load("player1standright.png").convert_alpha()
rect = IMAGE.get_rect()
rect.x = 100
rect.y = 225
x_movement = 0
y_movement = 0
x2_movement = 0
y2_movement = 0
accelx1 = 0
accely1 = 0
accelx2 = 0
accely2 = 0
jumping = False
jumping2 = False
#This is the function that does it
def player1runright(images1right, IMAGE, image, imagerect):
i = -1
while i <= 2:
pygame.time.delay(0)
i += 1
print (i)
IMAGE = pygame.image.load(images1right[i])
print (images1right[i])
screen.blit(IMAGE, rect)
images1right = ["player1runright-1.png","player1runright-2.png","player1runright-3.png","player1runright-4.png"]
def health_bars(player1_health, player2_health):
if player1_health > 75:
player1_health_color = (62, 219, 41)
elif player1_health > 50:
player1_health_color = (239, 212, 9)
else:
player1_health_color = (239, 9, 9)
if player2_health > 75:
player2_health_color = (62, 219, 41)
elif player2_health > 50:
player2_health_color = (239, 212, 9)
else:
player2_health_color = (239, 9, 9)
pygame.draw.rect(screen, player1_health_color, (70, 50, player1_health, 25))
pygame.draw.rect(screen, player2_health_color, (900, 50, player2_health, 25))
player1_health = int(100)
player2_health = int(100)
drawmenu()
while True:
screen.fill(background)
screen.blit(image, imagerect)
pygame.draw.rect(screen, (255,0,0), square1)
pygame.draw.rect(screen, (0,0,255), square2)
pygame.draw.rect(screen, (0,0,0), wall_left)
pygame.draw.rect(screen, (0,0,0), wall_right)
health_bars(player1_health, player2_health)
pygame.display.update()
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
exit()
keys_pressed = pygame.key.get_pressed()
if keys_pressed[pygame.K_d] != 1:
screen.blit(IMAGE, rect)
#MOVEMENT P1
if keys_pressed[pygame.K_LEFT] == 1:
x_movement += -1
elif keys_pressed[pygame.K_RIGHT] == 1:
x_movement += 1
else:
x_movement = 0
#MOVEMENT P2
if keys_pressed[pygame.K_a] == 1:
x2_movement += -1
elif keys_pressed[pygame.K_d] == 1:
x2_movement += 1
player1runright(images1right, IMAGE, image, imagerect)
else:
x2_movement = 0
#MAX SPEED P1
if x_movement < -7:
x_movement = -7
if x_movement > 7:
x_movement = 7
if y_movement < -7:
y_movement = -7
if y_movement > 7:
y_movement = 7
#MAX SPEED P2
if x2_movement < -7:
x2_movement = -7
if x2_movement > 7:
x2_movement = 7
if y2_movement < -7:
y2_movement = -7
if y2_movement > 7:
y2_movement = 7
#COLLISION (WALL) P1
if square1.colliderect(wall_left):
player2_health -= 0.5
x_movement = 0
y_movement = 0
if keys_pressed[pygame.K_RIGHT] == 1:
x_movement += 1
if square1.colliderect(wall_right):
player2_health -= 0.5
x_movement = 0
y_movement = 0
if keys_pressed[pygame.K_LEFT] == 1:
x_movement += -1
#COLLISION (WALL) P2
if rect.colliderect(wall_left):
player1_health -= 0.5
x2_movement = 0
y2_movement = 0
rect.x = 0
rect.y = 0
if keys_pressed[pygame.K_d] == 1:
x2_movement += 1
rect.x += 1
if rect.colliderect(wall_right):
player1_health -= 0.5
x2_movement = 0
y2_movement = 0
rect.x = 0
rect.y = 0
if keys_pressed[pygame.K_a] == 1:
x2_movement += -1
rect.x += -1
#COLLISION (ATTACK) P1
if square1.colliderect(rect) and keys_pressed[pygame.K_g] == 1:
player2_health -= 1
#COLLISION (ATTACK) P2
if rect.colliderect(square1) and keys_pressed[pygame.K_p] == 1:
player1_health -= 1
#JUMPING P1
if keys_pressed[pygame.K_w] == 1:
if jumping2 == False:
jumping2 = True
y2_movement = -30
accely2 = 1
rect = rect.move(x2_movement, y2_movement)
x2_movement += accelx2
y2_movement += accely2
if jumping2 == True:
if rect.y >= 225:
jumping2 = False
rect.y = 225
y2_movement = 0
accely2 = 0
#JUMPING P2
if keys_pressed[pygame.K_UP] == 1:
if jumping == False:
jumping = True
y_movement = -30
accely1 = 1
square1 = square1.move(x_movement, y_movement)
x_movement += accelx1
y_movement += accely1
if jumping == True:
if square1.y >= 350:
jumping = False
square1.y = 350
y_movement = 0
accely1 = 0
pygame.display.update()
pygame.time.delay(20)
if player2_health == 0 and player1_health !=0:
pygame.time.delay(400)
player1wins()
pygame.time.delay(3000)
drawmenu()
break
elif player1_health == 0 and player2_health !=0:
pygame.time.delay(400)
player2wins()
pygame.time.delay(3000)
drawmenu()
break
if player1_health == 0 and player2_health == 0:
pygame.time.delay(400)
tie()
pygame.time.delay(3000)
drawmenu()
break
(我知道代码有点长,但我不知道我是否可以删掉其中的任何一个。)
你的blit rect是错误的(至少)两个原因:
player1runright
中,使用一个名为imagerect
的参数,但是使用rect
进行blit,上面定义为来自"player1standright.png"
的rect。那个矩形永远不变。在player1runright
时,传递imagerect
的值,该值定义为"background(1).png"
中的rect。同样,这是永远不会改变的,它不是你想要使用的rect。在这两个问题都是由于变量“泄漏”到其他范围造成的。有时这是需要的,但在这里它只是导致错误。我会考虑重新组织代码(除非有意捕获包含范围,否则不要嵌套函数)以帮助查找这些错误。在
关于减速:不要每帧都加载图像。文件加载缓慢。装载一次,然后存放在某处。在
闪烁是因为每帧更新两次屏幕。删除第一行
pygame.display.update()
,它应该可以工作了。通常每个帧应该只有一个pygame.display.update
或pygame.display.flip
调用。在我通过连续删除程序的一部分,直到只剩下一个最小的例子,就知道了。每次删除某些内容后都要检查错误是否仍然存在。在
动画的问题与此无关,但这里有一个快速的解决方案。首先,必须删除
while
函数中的while
循环,因为它将立即在每帧中逐个地blit所有图像。在如果按了d键,我只需每帧增加一个索引变量,从动画列表中取出相应的图像,并将其分配给保存当前图像的变量,然后进行blit操作。在
另外,看看this answer。在
相关问题 更多 >
编程相关推荐