如何减慢python中这些移动形状的速度? [复制]
Posted
技术标签:
【中文标题】如何减慢python中这些移动形状的速度? [复制]【英文标题】:How to slow down the speed of these moving shapes in python? [duplicate] 【发布时间】:2021-03-25 21:11:34 【问题描述】:我正在用我自己的版本制作太空入侵者。我没有使用子弹,而是使用从敌方飞船随机发射的激光。但是,激光移动得太快了,如果我使用time.sleep(1)
,整个程序将每秒冻结。这是我的代码,帮助我找出我可以做些什么来修复它。
# importing packages
import pygame
import random
import time
# Setting a display caption
pygame.display.set_caption("Space Invaders")
spaceship = pygame.image.load("spaceship2.png")
blue_enemy = pygame.image.load("blue_enemy.png")
green_enemy = pygame.image.load("green_enemy.png")
orange_enemy = pygame.image.load("orange_enemy.png")
pink_enemy = pygame.image.load("pink_enemy.png")
yellow_enemy = pygame.image.load("yellow_enemy.png")
# Setting a display width and height and then creating it
display_width = 700
display_height = 500
display_size = [display_width, display_height]
game_display = pygame.display.set_mode(display_size)
def message(sentence, color, x, y, font_type, display):
sentence = font_type.render(str.encode(sentence), True, color)
display.blit(sentence, [x, y])
def main():
# Spaceship coordinates
spaceship_x = 300
spaceship_y = 375
spaceship_x_change = 0
blue_enemy_health = 5
green_enemy_health = 5
orange_enemy_health = 5
pink_enemy_health = 5
yellow_enemy_health = 5
# Initializing pygame
pygame.init()
# Creating colors
black = (0, 0, 0)
red = (0, 0, 0)
white = (255, 255, 255)
seconds_counter = pygame.time.get_ticks()
done = False
# Creating a loop to keep program running
while not done:
# --- Event Processing and controls
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
spaceship_x_change = 7
elif event.key == pygame.K_LEFT:
spaceship_x_change = -7
elif event.key == pygame.K_r:
red = (255, 0, 0)
elif event.type == pygame.KEYUP:
spaceship_x_change = 0
red = (0, 0, 0)
spaceship_x += spaceship_x_change
# Timer
seconds = (pygame.time.get_ticks() - seconds_counter) / 1000
# Preventing the ship from going off the screen
if spaceship_x > display_width - 140:
spaceship_x -= 10
if spaceship_x < 1:
spaceship_x += 10
# Setting Display color
game_display.fill(black)
laser_coords = [70, 209, 348, 505, 630]
# this is where i make the random lasers
# Creating a spaceship, lasers, and enemies
pygame.draw.rect(game_display, red, [spaceship_x + 69, 70, 4, 310])
pygame.draw.rect(game_display, white, [random.choice(laser_coords), 85, 4, 200])
game_display.blit(spaceship, (spaceship_x, spaceship_y))
message(str(blue_enemy_health), white, 65, 10, font, game_display)
game_display.blit(blue_enemy, (20, 25))
message(str(green_enemy_health), white, 203, 10, font, game_display)
game_display.blit(green_enemy, (160, 25))
message(str(orange_enemy_health), white, 341, 10, font, game_display)
game_display.blit(orange_enemy, (300, 25))
message(str(pink_enemy_health), white, 496, 10, font, game_display)
game_display.blit(pink_enemy, (440, 25))
message(str(yellow_enemy_health), white, 623, 10, font, game_display)
game_display.blit(yellow_enemy, (580, 25))
# Updating Screen so changes take places
pygame.display.update()
# Setting FPS
FPS = pygame.time.Clock()
FPS.tick(60)
另外,我还没有包含我的所有代码。
【问题讨论】:
嘿,我相信这对你有帮助:***.com/questions/18948981/… 【参考方案1】:在 pygame 中可以通过调用pygame.time.get_ticks()
来获取系统时间,它返回自调用pygame.init()
以来的毫秒数。请参阅pygame.time
模块。
计算需要改变激光位置的时间点。获取当前时间大于计算时间的新随机位置。计算需要再次改变位置的新随机时间点:
def main():
# [...]
change_laser_coord_time = 0
laser_coords = [70, 209, 348, 505, 630]
laser_coord_x = random.choice(laser_coords)
# Creating a loop to keep program running
while not done:
# [...]
current_time = pygame.time.get_ticks()
if current_time > change_laser_coord_time:
change_laser_coord_time = current_time + 1000
laser_coord_x = random.choice(laser_coords)
# [...]
pygame.draw.rect(game_display, white, [laser_coord_x, 85, 4, 200])
【讨论】:
以上是关于如何减慢python中这些移动形状的速度? [复制]的主要内容,如果未能解决你的问题,请参考以下文章
Google Maps API v3 中的 OVER_QUERY_LIMIT:如何在 Javascript 中暂停/延迟以减慢速度?