【发布时间】:2021-03-26 02:40:36
【问题描述】:
我正在尝试用 pygame 制作我的第一个游戏。
我希望游戏在背景中有这个动画
我正在使用 python 3.9.2
import pygame, math, random, sys
from pygame.locals import *
screen = pygame.display.set_mode((400, 400))
fps = 60
pygame.init()
mainClock = pygame.time.Clock()
# Colors
black = (0, 0, 0)
white = (255, 255, 255)
# Function for close the game ------------------------------------------------------------------------------------------------------------
def close_game():
pygame.quit()
sys.exit()
# Functions for drawing ------------------------------------------------------------------------------------------------------------------
def background():
screen.fill(black)
def canvas():
margin = pygame.draw.rect(screen, white, (50, 50, 300, 300), 1)
def bar_animation():
bar_width = 15
for b in range(0, 20):
bar_b_height = random.randint(10, 100)
bar_b = pygame.draw.rect(screen, white, (50 + (bar_width * b), 350 - bar_b_height, bar_width, bar_b_height), 0)
# Main game loop --------------------------------------------------------------------------------------------------------------------------
def main_loop():
running = True
while running:
background()
bar_animation()
canvas()
for event in pygame.event.get():
if event.type == QUIT:
close_game()
pass
pygame.display.update()
mainClock.tick(fps)
# Run game --
main_loop()
在该代码上,我重新创建了我想要的游戏背景。 我想让 bar_animation() 函数运行得更慢,而不会使整个游戏或其他函数运行得更慢...
我建议您执行代码以了解我在做什么
【问题讨论】:
标签: python python-3.x pygame