创建一个单独的模块,其中包含使另一个代码在pygame中运行的函数



ok,所以我真的不理解函数ATM和我的任务是创建一个单独的python模块,它具有使程序运行所需的所有函数。这个课程是教授提供的,已经完成了,我不能更改。我并不是要求任何人坐在这里,自己完成所有的功能,虽然这将是惊人的,我只需要完成一两个,也许是一个解释他们是如何工作的。下面是给我的完整的程序。

import pygame
import lab06_bubbles as bubbles
import random
# @@@ NOTE: In this lab, I *DON'T* want you to change anything in this file. I want you to create
#           lab06_bubbles.py so that this works as described in the lab document (and video) @@@
# Pygame setup
win_width = 1200
win_height = 800
pygame.display.init()
pygame.font.init()
screen = pygame.display.set_mode((win_width, win_height))
font = pygame.font.SysFont("Courier New", 12)
clock = pygame.time.Clock()
done = False
numBubbles = random.randint(50, 100)                        # Initial number of bubbles
numWebs = 2                                                 # Initial number of connections between each bubble
paused = False                                              # Are we paused?
minAreaWidth = 50                                           # The minimum width of the bubble area
maxAreaWidth = win_width                                    # The maximum width of the bubble area
minAreaHeight = 50                                          # The minimum height of the bubble area
maxAreaHeight = win_height - 50                             # The maximum height of the bubble area
# Generate the initial play area and bubble list
currentArea = bubbles.randomArea(minAreaWidth, minAreaHeight, maxAreaWidth, maxAreaHeight)
bubbleList = bubbles.spawnBubbles(numBubbles, currentArea, 5, 20, 100)

# Game Loop
while not done:
    # @@@@@@@@@@@@@
    # @ UPDATE    @
    # @@@@@@@@@@@@@
    deltaTime = clock.tick() / 1000.0
    if paused:
       deltaTime = 0.0
    bubbles.updateBubbles(bubbleList, currentArea, deltaTime)
    # @@@@@@@@@@@@@
    # @ INPUT     @
    # @@@@@@@@@@@@@
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        done = True
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_ESCAPE:
            done = True
        elif event.key == pygame.K_LEFT:
            numBubbles -= 5
            if numBubbles < 5:
                numBubbles = 5
            bubbleList = bubbles.spawnBubbles(numBubbles, currentArea, 5, 20, 100)
        elif event.key == pygame.K_RIGHT:
            numBubbles += 5
            if numBubbles > 100:
                numBubbles = 100
            bubbleList = bubbles.spawnBubbles(numBubbles, currentArea, 5, 20, 100)
        elif event.key == pygame.K_UP:
            numWebs += 1
        elif event.key == pygame.K_DOWN:
            numWebs -= 1
            if numWebs < 0:
                numWebs = 0
        elif event.key == pygame.K_SPACE:
            currentArea = bubbles.randomArea(minAreaWidth, minAreaHeight, maxAreaWidth, maxAreaHeight)
            bubbleList = bubbles.spawnBubbles(numBubbles, currentArea, 5, 20, 100)
        elif event.key == pygame.K_p:
            paused = not paused
    # @@@@@@@@@@@@@
    # @ DRAW      @
    # @@@@@@@@@@@@@
    screen.fill((0,0,0))
    pygame.draw.rect(screen, (255,255,255), currentArea, 1)
    bubbles.drawBubbles(bubbleList, screen, numWebs)
    screen.blit(font.render("# Bubbles: " + str(len(bubbleList)) + "  area: " + str(currentArea) + 
                        "   num_webs: " + str(numWebs), False, (128,128,128)), (0, win_height - 48))
    screen.blit(font.render("[Left / Right] : decrease / increase number of bubbles    [space]: regenerate bounds [escape]: quit", 
                        False, (128,128,128)), (0, win_height - 32))
    screen.blit(font.render("[Up / Down] : decrease / increase number of connections    [p]: pause", False, 
                        (128,128,128)), (0, win_height - 16))
    pygame.display.flip()
# Pygame shutdown
pygame.font.quit()
pygame.display.quit()

这是给我的函数大纲模块,这是我不知道该怎么做的地方。

def randomArea(minW, minH, maxW, maxH):
 """
 :param minW: the minimum width of this box
 :param minH: the minimum height of this box
 :param maxW: the maximum width of this box (normally the width of the "playfield")
 :param maxH: the maximum height of this box (normally the height of the "playfield")
 :return: a pygame-style rect (x, y, w, h) that fits within the "playfield"
 """
def spawnBubbles(num, area, minSize, maxSize, maxSpeed):
 """
 :param num: the number of bubbles to create
 :param area: a pygame-style rect (x, y, w, h) that all bubbles should spawn completely inside
 :param minSize: the minimum size of a bubble
 :param maxSize: the maximum size of a bubble (should fit within the minimum size passed to
randomArea)
 :param maxSpeed: the maximum horizontal and vertical speed a bubble should have (in px / s)
 :return: A list of lists where each sub-list is of the form [x, y, radius, horiz_speed,
vert_speed]
 """
def getClosest(interest, L, num):
 """
 :param interest: the bubble of interest
 :param L: the list of all bubbles
 :param num: the number of closest bubbles to find
 :return: a list of num bubbles (not counting the interest) that are closest to the interest
object
 """
def updateBubbles(L, area, dt):
 """
 Makes all bubbles move. If a bubble hits the left / right sides of the area, negate their
horiz_speed and
 move them just inside the area; if a bubble hits the top / bottom of the area, negate their
vert_speed and
 move them just inside the area.
 :param L: The list of all bubbles
 :param area: the pygame-style rect bounding the play area.
 :param dt: The number of seconds that has passed since the last update
 :return: None
 """
def drawBubbles(L, surf, num_webs):
 """
 Draws the bubbles and the connections between them. This function calls the getClosest
function internally
 :param L: The list of all bubbles
 :param surf: The surface to draw to
 :param num_webs: The number of connections to draw between bubbles
 :return: None
 """

我只是需要一些帮助。这就是最终产品的样子。

首先,我不会帮你做作业,但我可以试着给你以下提示:

每次在python脚本中使用import语句导入一个模块(另一个脚本的奇特名称)时,python将在两个地方查找该模块:

  • 当前工作目录(通常是你正在处理的脚本所在的文件夹)
  • 如果在当前工作目录中找不到它,它将在Python安装的库中查找它。

所以你要做的第一件事,就是确保lab06_bubbles和你要运行的脚本在同一个目录下。

然后,由于您的教授将lab06_bubbles导入为bubbles,因此在他的程序中提到bubbles.function的任何地方,它都将在lab06_bubbles.py文件中查找该函数,您必须在该文件中构建程序所需的所有函数。

然后教授给你构建函数所需的所有信息:

  • 参数是什么
  • 函数应该返回什么

你,必须在中间施展魔法。

假设其中一个函数名为getDist,描述如下:

def getDist(pointa, pointb):
 """
 Takes two points, and return the distance between those points.
 :param pointa: a tuple with the x, y coordinates of point a
 :param pointb: a tuple with the x, y coordinates of point b
 :return: a float representing the distance between the two points
 """

则需要按照如下方式编写函数:

def getDist(pointa, pointb):
    xa,ya = pointa
    xb,yb = pointb
    dist_ab = ((xa-xb)**2+(ya-yb)**2)**0.5
    return float(dist_ab)

希望能有所帮助

相关内容

最新更新