Pygame使用类随机化图像展示位置

时间:2016-06-07 20:20:48

标签: python class python-3.x pygame

我正在创建卡片/磁贴内存关联游戏。它是一个4x4网格的100x100像素图像,因此总共显示16个图像(8对)。

现在我想要的是每次游戏开始时图像以随机顺序显示在网格中,我觉得我接近使用在类中的列表中将它们混洗然后调用它while循环中的main函数的类。

这是我的代码:

# Memory Tiles Game
# A Memory Game which users can flip tiles in and remember their placement to match
# all of the tiles 

import pygame, sys, time
from pygame.locals import *

# User-defined classes
class memorygame(object):
   def __init__(self):
      imagelist = [image1,image2,image3,image4,image5,image6,image7
                   image8,image9]
      shuffle.imagelist
      screen.blit(imagelist[0],(0,0))
      screen.blit(imagelist[1],(104,0))
      screen.blit(imagelist[2],(208,0))
      screen.blit(imagelist[3],(312,0))
      screen.blit(imagelist[4],(0,104))
      screen.blit(imagelist[5],(104,104))
      screen.blit(imagelist[6],(208,104))
      screen.blit(imagelist[7],(312,104))
      shuffle.imagelist
      screen.blit(imagelist[0],(0,208))
      screen.blit(imagelist[1],(104,208))
      screen.blit(imagelist[2],(208,208))
      screen.blit(imagelist[3],(312,208))
      screen.blit(imagelist[4],(0,312))
      screen.blit(imagelist[5],(104,312))
      screen.blit(imagelist[6],(208,312))
      screen.blit(imagelist[7],(312,312))






# User-defined functions

def main():

   black = (  0,  0, 0)
   white = (255,255,255)
   red = (100,  0,  0)
   green = (  0,153,  0)
   blue = (  0,  0,255)
   image1 = pygame.image.load('image0.bmp')
   image2 = pygame.image.load('image1.bmp')
   image3 = pygame.image.load('image2.bmp')
   image4 = pygame.image.load('image3.bmp')
   image5 = pygame.image.load('image4.bmp')
   image6 = pygame.image.load('image5.bmp')
   image7 = pygame.image.load('image6.bmp')
   image8 = pygame.image.load('image7.bmp')
   image9 = pygame.image.load('image8.bmp')
   w = 500
   h = 400
   screen = pygame.display.set_mode((w, h))
   screen.fill((white))
   running = 1

   # Initialize pygame
   pygame.init()

   # Set window size and title, and frame delay
   surfaceSize = (500, 400) # example window size
   windowTitle = 'Memory Game' # example title
   frameDelay = 0.02 # smaller is faster game

   # Create the window
   surface = pygame.display.set_mode(surfaceSize, 0, 0)
   pygame.display.set_caption(windowTitle)

   # create and initialize objects
   gameOver = False
   #center = [200, 200] # example - replace

   # Draw objects
   # The next line is an example - replace it
   #pygame.draw.circle(surface, pygame.Color('green'), center, 50, 0)

   # Refresh the display
   pygame.display.update()

   # Loop forever
   while True:
      # Check event
      event = pygame.event.poll()
      if event.type == QUIT:
         pygame.quit()
         sys.exit()
      screen.fill((black))
      class memorygame(object) 

      # Handle additional events

      # Update and draw objects for the next frame
      gameOver = update(center, surface)

      # Refresh the display
      pygame.display.update()

      # Set the frame speed by pausing between frames
      time.sleep(frameDelay)

main()

它似乎没有编译,我不知道为什么。我在shell中遇到语法错误。

1 个答案:

答案 0 :(得分:0)

首先,你在memorygame类的imagelist的第一行末尾忘记了一个逗号。将其更改为

imagelist = [image1,image2,image3,image4,image5,image6,image7,
             image8,image9]

应修复第一个语法错误。

其次,我不确定您在主循环中使用行class memorygame(object)尝试实现的目标。你可能想在while循环之外创建一个类的实例,并调用一个函数来使while循环内的图像变形。

顺便说一下,图像变量是本地的,memorygame类不会看到它们,除非你明确地将它们作为全局变量,或者将它们传递给memorygame的__init__方法。

此外,你改变图像列表的方式并不完全正确。你应该随机导入,然后调用random.shuffle(imagelist)。