该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
citingspace ::gdi_test::game_test::gemgem
function print_line(s)
endf
function FPS()
return 20 // frames per second to update the screen
endf
function WINDOWDEFAULTWIDTH()
return 1024 // width of the program's window, in pixels
endf
function WINDOWDEFAULTHEIGHT()
return 728 // height in pixels
endf
function BOARDWIDTH()
return 8 // how many columns in the board
endf
function BOARDHEIGHT()
return 8 // how many rows in the board
endf
function ScalingRatio()
return 2
endf
function GEMIMAGESIZE(windowWidth, windowHeight)
if windowWidth < windowHeight
return 64 * windowWidth / 640 // width & height of each space in pixels
else
return 64 * windowHeight / 640
endif
endf
function NUMGEMIMAGES()
// NUMGEMIMAGES is the number of gem types. You will need .png image
// files named gem0.png, gem1.png, etc. up to gem(N-1).png.
return 7 // game needs at least 5 types of gems to work
endf
function NUMMATCHSOUNDS()
// NUMMATCHSOUNDS is the number of different sounds to choose from when
// a match is made. The .wav files are named match0.wav, match1.wav, etc.
return 6
endf
function MOVERATE()
return 25 // 1 to 100, and 100 has to be its integer times. larger num means faster animations
endf
function DEDUCTSPEED()
return 800 // reduces score by 1 point every DEDUCTSPEED milliseconds.
endf
function PURPLE()
return [255, 0, 255]
endf
function LIGHTBLUE()
return [170, 190, 255]
endf
function BLUE()
return [0, 0, 255]
endf
function RED()
return [255, 100, 100]
endf
function BLACK()
return [0, 0, 0]
endf
function BROWN()
return [85, 65, 0]
endf
function HIGHLIGHTCOLOR()
return PURPLE() // color of the selected gem's border
endf
function BGCOLOR()
return LIGHTBLUE() // background color on the screen
endf
function GRIDCOLOR()
return BLUE() // color of the game board
endf
function GAMEOVERCOLOR()
return RED() // color of the "Game over" text.
endf
function GAMEOVERBGCOLOR()
return BLACK() // background color of the "Game over" text.
endf
function SCORECOLOR()
return BROWN() // color of the text for the player's score
endf
// The amount of space to the sides of the board to the edge of the window
// is used several times, so calculate it once here and store in variables.
function XMARGIN(windowWidth, windowHeight)
return floor((windowWidth - GEMIMAGESIZE(windowWidth, windowHeight) * BOARDWIDTH()) / 2)
endf
function YMARGIN(windowWidth, windowHeight)
return floor((windowHeight - GEMIMAGESIZE(windowWidth, windowHeight) * BOARDHEIGHT()) / 2)
endf
// constants for direction values
function UP()
return "up"
endf
function DOWN()
return "down"
endf
function LEFT()
return "left"
endf
function RIGHT()
return "right"
endf
function EMPTY_SPACE()
return -1// an arbitrary, nonpositive value
endf
function ROWABOVEBOARD()
return "row above board" // an arbitrary, noninteger value
endf
Help
This program has "gem data structures", which are basically dictionaries
with the following keys:
'x' and 'y' - The location of the gem on the board. 0,0 is the top left.
There is also a ROWABOVEBOARD row that 'y' can be set to,
to indicate that it is above the board.
'direction' - one of the four constant variables UP, DOWN, LEFT, RIGHT.
This is the direction the gem is moving.
'imageNum' - The integer index into GEMIMAGES to denote which image
this gem uses.
Endh
function start_gemgem()
variable DISPLAYSURF, GEMIMAGES, BOARDRECTS
// Initial set up
DISPLAYSURF = open_screen_display("Gem gem", BGCOLOR(), true, [WINDOWDEFAULTWIDTH(), WINDOWDEFAULTHEIGHT()], false)
variable windowWidth = get_display_size(DISPLAYSURF)[0], windowHeight = get_display_size(DISPLAYSURF)[1]
variable shrinkingRatio = 1/ScalingRatio()
variable xMargin = XMARGIN(windowWidth, windowHeight)
variable yMargin = YMARGIN(windowWidth, windowHeight)
variable gemImgSize = GEMIMAGESIZE(windowWidth, windowHeight)
variable mvRate = MOVERATE()
// Load the images of the GEMs
GEMIMAGES = alloc_array([NUMGEMIMAGES()])
@build_asset copy_to_resource(get_upper_level_path(get_src_file_path()), "charts")
for variable idx = 1 to NUMGEMIMAGES()
variable gemImage
if is_mfp_app()
gemImage = load_image_from_zip(get_asset_file_path("resource"), "charts/gem" + idx + ".png", 1)
else
gemImage = load_image(get_upper_level_path(get_src_file_path()) + "gem" + idx + ".png")
endif
variable gemImageSize = get_image_size(gemImage)
GEMIMAGES[idx - 1] = clone_image(gemImage, 0, 0, gemImageSize[0], gemImageSize[1], gemImgSize * shrinkingRatio, gemImgSize * shrinkingRatio)
next
// No need to load the sounds. Sounds can be loaded on the spot.
// Create pygame.Rect objects for each board space to
// do board-coordinate-to-pixel-coordinate conversions.
BOARDRECTS = alloc_array([BOARDWIDTH(), BOARDHEIGHT()])
for variable x = 0 to BOARDWIDTH() - 1
for variable y = 0 to BOARDHEIGHT() - 1
BOARDRECTS[x,y] = [xMargin + (x * gemImgSize), yMargin + (y * gemImgSize), _
gemImgSize, gemImgSize]
next
next
variable up = UP(), down = DOWN(), left = LEFT(), right = RIGHT()
variable rowAboveBoard = ROWABOVEBOARD()
variable boardImageDisplay = open_image_display(null)
set_display_size(boardImageDisplay, windowWidth, windowHeight)
for variable x = 0 to BOARDWIDTH() - 1
for variable y = 0 to BOARDHEIGHT() - 1
draw_rect("gemgem", boardImageDisplay, [BOARDRECTS[x][y][0], BOARDRECTS[x][y][1]], _
BOARDRECTS[x][y][2], BOARDRECTS[x][y][3], GRIDCOLOR(), 1)
next
next
update_display(boardImageDisplay)
variable boardImage = get_display_snapshot(boardImageDisplay, false)
set_display_bgrnd_image(DISPLAYSURF, boardImage, 0)
while run_game(DISPLAYSURF, GEMIMAGES, BOARDRECTS, gemImgSize, windowWidth, windowHeight, _
xMargin, yMargin, mvRate, up, down, left, right, rowAboveBoard)
loop
shutdown_display(boardImageDisplay)
endf
// for gem, a 3-element or 4-element array stores gem information. In particular, gem[0] is imageNum, gem[1] is x, gem[2] is y, gem[3] is direction.
// for point, a 3-element array stores point information. In particular, point[0] is point, point[1] is x, point[2] is y.
function run_game(DISPLAYSURF, GEMIMAGES, BOARDRECTS, gemImgSize, windowWidth, windowHeight, xMargin, yMargin, mvRate, up, down, left, right, rowAboveBoard)
// Plays through a single game. When the game is over, this function returns.
// initalize the board
variable gameBoard = get_blank_board()
variable score = 0
variable gemsImageAndDisplay = init_Board_And_Animate(DISPLAYSURF, GEMIMAGES, BOARDRECTS, gameBoard, windowWidth, windowHeight, mvRate) // drop the initial gems.
variable gemsImage = gemsImageAndDisplay[0]
variable gemsImageDisplay = gemsImageAndDisplay[1]
// now gemsImageDisplay is updated.
// initialize variables for the start of a new game
variable firstSelectedGem = Null
variable lastMouseDownX = Null
variable lastMouseDownY = Null
variable gameIsOver = False
variable lastScoreDeduction = now()
variable clickContinueTextSurf = Null
variable infoNum = 0, infoX = 1, infoY = 2, infoDirect = 3
variable shrinkingRatio = 1/ScalingRatio(), scaledRatio = scalingRatio()
while true // main game loop
drop_old_painting_requests("gemgem", DISPLAYSURF)
variable clickedSpace = null
do
variable giEvent = pull_event(DISPLAYSURF)
if giEvent == Null
// no event to handle
break
elseif get_event_type_name(giEvent) == "GDI_CLOSE"
// quit
return false
elseif get_event_type_name(giEvent) == "POINTER_UP"
if gameIsOver
return true // after games ends, click to start a new game
endif
// we do not consider drag.
clickedSpace = check_For_Gem_Click([lastMouseDownX, lastMouseDownY], BOARDRECTS)
elseif get_event_type_name(giEvent) == "POINTER_DOWN"
// this is the start of a mouse click or mouse drag
lastMouseDownX = get_event_info(giEvent, "x")
lastMouseDownY = get_event_info(giEvent, "y")
endif
until false
if and(clickedSpace != Null, firstSelectedGem == Null)
......