好吧所以我真的不懂函数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
"""
我需要一些帮助。这就是final product的样子。
答案 0 :(得分:0)
首先,我不会为你做任务,但我可以通过给你以下线索尝试帮助:
每次在python脚本中使用import语句导入模块(另一个脚本的花哨名称)时,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)
希望有所帮助