我正在尝试完成1个练习,但是我自己找不到解决方案。 我得到一个数字,函数应该使用递归来查找值:
if n == 1: return 1
elif n != 1: fun(n-1) + n
但是我想向我的函数提供2个值,即起点和终点,而我的函数会找到范围内的所有值(起点,终点)。
答案 0 :(得分:0)
使用两个参数定义函数。当您进行递归调用时,其中一个会减少,而另一个保持不变:
import pygame
white = (255, 255, 255)
black = (0, 0, 0)
class Game():
width = 800
height = 600
screen = pygame.display.set_mode((width, height))
def __init__(self):
pass
def fill_screen(self, color):
self.color = color
self.screen.fill(self.color)
class Player(pygame.sprite.Sprite):
lead_x = 800/2
lead_y = 600/2
velocity = 0.002
block_size = 10
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.draw.rect(game.screen, black, [self.lead_x, self.lead_y, self.block_size, self.block_size])
def move_player_x_left(self):
self.lead_x += -self.velocity
def move_player_x_right(self):
self.lead_x += self.velocity
def move_player_y_up(self):
self.lead_y += -self.velocity
def move_player_y_down(self):
self.lead_y += self.velocity
game = Game()
player = Player()
exitGame = False
while not exitGame:
game.fill_screen(white)
for event in pygame.event.get():
if event.type == pygame.QUIT:
exitGame = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_w:
player.move_player_y_up()
if event.key == pygame.K_s:
player.move_player_y_down()
if event.key == pygame.K_d:
player.move_player_x_right()
if event.key == pygame.K_a:
player.move_player_x_left()
pygame.display.update()
pygame.quit()
quit()
别忘了,当您进行递归调用时,必须返回结果。