我在pygame中的代码目前无法正常运行

时间:2020-07-22 17:24:08

标签: python pygame

所以我制作了这个脚本

import pygame
import time
from pygame.locals import *
red = (255,0,0)
black = (0,0,0)

a = 25
b = 25
pygame.init
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption("AppliedShapes")
while True:
  for event in pygame.event.get():
      if event.type == QUIT:
        pygame.quit()
        exit()
  screen.fill(black)


  pygame.draw.circle(screen,red,(320,240),(a),0)
  time.sleep(0.7)
  if a == 250:
      a = a-b
  elif a < 250:
      a = a+b

  pygame.display.update()

我正在尝试使此脚本使圆变大十倍,并使它们变小,但它不起作用。它可以完美地变大,但无法正常工作。你能帮我吗?

2 个答案:

答案 0 :(得分:3)

如果ba <= 25反转a >= 250

while True:
  # [...]
  
  a += b
  if a <= 25 or a >= 250:
      b = -b

完整代码:

import pygame
import time
from pygame.locals import * 

red = (255, 0, 0)
black = (0, 0, 0)
a = 25
b = 25
pygame.init()
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption("AppliedShapes")
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            exit()
    screen.fill(black)
    pygame.draw.circle(screen, red, (320,240), a, 0)
    time.sleep(0.7)
    pygame.display.update()

    a += b
    if a <= 25 or a >= 250:
        b = -b

答案 1 :(得分:1)

尝试一下:

import pygame 
import time 
from pygame.locals import * 

red = (255,0,0) 
black = (0,0,0)

a = 25
b = 25
pygame.init
screen = pygame.display.set_mode((640,480))
pygame.display.set_caption("AppliedShapes")
increasing = True
while True:
  for event in pygame.event.get():
      if event.type == QUIT:
        pygame.quit()
        exit()
  screen.fill(black)


  pygame.draw.circle(screen,red,(320,240),(a),0)
  time.sleep(0.7)
  if a == 250:
    increasing = False
  if increasing:
    a += b
  else:
    a -= b

  pygame.display.update()