两件事。有人能帮我理解为什么速度不上升吗?我打印了VEL,它确实上升了,但有些东西使它下降了。如果有人能告诉我如何循环音乐,这样音乐就不会结束,那也太好了。(太空音乐)。如果你还想知道什么,请告诉我。我之所以把这个游戏作为笔记来制作,是因为我正在为一个项目学习更多关于python和pygame的知识。如果您不知道正在做什么或感到困惑,请告诉我。我没有包括这些文件,因为我认为你真的不需要下载来弄清楚,但如果需要的话我可以
import pygame
import random
from replit import audio
pygame.init()
bg = pygame.image.load("spacebg.png")
player = pygame.image.load("UFO.png")
obstacle = pygame.image.load("Asteroid.png")
highscore = 0
score = 0
counter = 0
multiplier = 1
level = 1
obstacle1x = random.randint(128,512)
obstacle1y = random.randint(128,512)
obstacle2x = random.randint(128,512)
obstacle2y = random.randint(128,512)
obstacle3x = random.randint(128,512)
obstacle3y = random.randint(128,512)
coinx = random.randint(128,512)
coiny = random.randint(128,512)
font = pygame.font.SysFont("arial", 30, True)
compwin = pygame.display.set_mode((512,512))
pygame.display.set_caption("RICH ALIEN DON'T DIE")
x=50
y=50
vel = 5
vel1 = random.randint(-5,5)
vel2 = random.randint(-5,5)
vel3 = random.randint(-5,5)
vel4 = random.randint(-5,5)
vel5 = random.randint(-5,5)
vel6 = random.randint(-5,5)
source1 = audio.play_file("space music.wav")
run = True
while run:
pygame.time.delay(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x-= vel
if keys[pygame.K_RIGHT]and x < 512 - 64 - vel:
x+= vel
if keys[pygame.K_UP] and y > vel:
y-= vel
if keys[pygame.K_DOWN] and y < 512 - 64 - vel:
y+= vel
hitbox1 = pygame.draw.rect(compwin,(0,0,0), (obstacle1x,obstacle1y,64,64) )
hitbox2 = pygame.draw.rect(compwin,(0,0,0), (obstacle2x,obstacle2y,64,64) )
hitbox3 = pygame.draw.rect(compwin,(0,0,0), (obstacle3x,obstacle3y,64,64) )
hitbox = pygame.draw.rect(compwin,(0,0,0), (x,y,64,64) )
compwin.blit(bg,(0,0))
obstacle1x = obstacle1x +vel1
obstacle1y = obstacle1y +vel2
obstacle2x = obstacle2x +vel3
obstacle2y = obstacle2y +vel4
obstacle3x = obstacle3x +vel5
obstacle3y = obstacle3y +vel6
if obstacle1x < 0 or obstacle1x > 512 - vel1:
vel1 = vel1 * -1
if obstacle1y < 0 or obstacle1y > 512 - vel2:
vel2 = vel2 * -1
if obstacle2x < 0 or obstacle2x > 512 - vel3:
vel3 = vel3 * -1
if obstacle2y < 0 or obstacle2y > 512 - vel4:
vel4 = vel4 * -1
if obstacle3x < 0 or obstacle3x > 512 - vel5:
vel5 = vel5 * -1
if obstacle3y < 0 or obstacle3y > 512 - vel6:
vel6 = vel6 * -1
compwin.blit(player,(x,y))
compwin.blit(obstacle,(obstacle1x,obstacle1y))
compwin.blit(obstacle,(obstacle2x,obstacle2y))
compwin.blit(obstacle,(obstacle3x,obstacle3y))
coin = pygame.draw.circle(compwin,(212,175,55),(coinx, coiny,),(16))
if score > highscore:
highscore = score
text = font.render("Score: " +str(score), 1,(255,255,255))
text2 = font.render("Highscore: " + str(highscore), 1, (255, 255, 255))
text3 = font.render("Level: " +str(level), 1,(255,255,255))
compwin.blit(text, (55, 10))
compwin.blit(text2, (255, 10))
compwin.blit(text3, (190, 482))
if hitbox.colliderect(hitbox1) or hitbox.colliderect(hitbox2, ) or hitbox.colliderect(hitbox3):
source = audio.play_file("mixkit-truck-crash-with-explosion-1616.wav")
obstacle1x = random.randint(128,512)
obstacle1y = random.randint(128,512)
obstacle2x = random.randint(128,512)
obstacle2y = random.randint(128,512)
obstacle3x = random.randint(128,512)
obstacle3y = random.randint(128,512)
coinx = random.randint(128,512)
coiny = random.randint(128,512)
x=50
y=50
score = 0
counter = 0
multiplier = 1
level = 1
elif hitbox.colliderect(coin):
score += multiplier
counter+=1
for i in [vel1,vel2,vel3,vel4,vel5,vel6]:
print(i)
coinx = random.randint(10,512)
coiny = random.randint(10,512)
if counter == 5:
multiplier += 1
counter = 0
level+= 1
for i in [vel1,vel2,vel3,vel4,vel5,vel6]:
if i < 0:
i += -1
else:
i+= 1
else:
pygame.display.update()
pygame.quit()
@Rabbid76的意思是,在这样做时:
i
在每次迭代时都成为vel1
、vel2
的副本,依此类推。 但是i
并不是指这些变量。所以基本上,你的循环 没有效果。换句话说,它相当于:相反,您可以用列表替换
velI
变量:然后可以将for循环重写为:
现在它可以工作了,因为这里
i
是列表中某个项目的索引。 因此,您可以直接更新列表中的项目,而不是副本当然,如果你这样做了,你需要改变你的程序},{}通过{}等等
vel1
通过{最后一件事:您可以使用列表理解来简化代码
相关问题 更多 >
编程相关推荐