我是pygame的初学者,我不是英语母语人士。
我的任务是编写一款名为“记忆”的游戏。这个游戏包含8对图片,每张图片上都有一个封面。本周,我们的作业是画封面,如果你点击一个封面,这个封面就会消失。不过,虽然我成功地画了照片。我还是不能正确画盖子。实际上,我不知道绘制封面和点击封面并让它消失的方法。我搜索了很多,也许是因为我不是母语人士,我真的不明白它是如何工作的。因此,我希望有人能在这里帮助我。我将在下面提供我的代码和图片。谢谢大家!
# This is version 1 for 'Memory'.
# This version contains complete tile grid, but not the score.
# All 8 pairs of two tiles should be exposed when the game starts.
# Each time the game is played, the tiles must be in random locations in the grid.
# Player actions must be ignored.
import pygame, sys, time, random
from pygame.locals import *
pygame.init()
# User-defined Class
class Tile:
bgColor=pygame.Color('black')
BorderWidth= 3
def __init__(self, x, y, image, surface):
self.x = x
self.y = y
self.surface = surface
self.image= image
def DrawTile(self):
self.surface.blit( self.image , (self.x, self.y))
class Memory:
boardWidthSize=4
boardHeightSize=4
def __init__(self, surface):
self.surface = surface
self.board = []
self.cover = []
self.board2= []
def createImages(self):
#load images from file
self.cover=[]
self.imageNames = ['image1.bmp','image2.bmp','image3.bmp','image4.bmp','image5.bmp','image6.bmp','image7.bmp','image8.bmp','image1.bmp','image2.bmp','image3.bmp','image4.bmp','image5.bmp','image6.bmp','image7.bmp','image8.bmp']
self.images=[]
for name in self.imageNames :
pic = pygame.image.load(name)
self.images.append(pic)
random.shuffle(self.images)
self.cover.append(pygame.image.load('image0.bmp'))
def createTile(self):
board =[]
board2=[]
#loop through the loaded images and create tile objects
for rowIndex in range(0,Memory.boardWidthSize):
row = []
row2=[]
for columnIndex in range(0,Memory.boardHeightSize):
width = 100
x = columnIndex*width
height = 100
y = rowIndex*height
tile = Tile(x, y, self.images[rowIndex*4+columnIndex], self.surface)
cover= Tile(x, y, self.cover, self.surface)
row.append(tile)
row2.append(cover)
self.board.append(row)
self.board2.append(row2)
def GetScore(self):
position=(400,0)
FontSize=50
FontColor=pygame.Color('White')
String='Score : '
font=pygame.font.SysFont(None, FontSize, True)
surface1=font.render(str(pygame.time.get_ticks()/1000), True, FontColor,0)
self.surface.blit(surface1,position)
def draw(self):
for row in self.board:
for tile in row:
tile.DrawTile()
def update(self):
if False:
return True
else:
self.createTile()
return False
def main():
surfaceSize = (500, 400)
windowTitle = 'Memory'
# No frame delay since no moving objects
gameOver = False
# Create the window
surface = pygame.display.set_mode(surfaceSize, 0, 0)
pygame.display.set_caption(windowTitle)
memory = Memory(surface)
memory.createImages()
memory.createTile()
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
memory.draw()
memory.GetScore()
gameOver = memory.update()
pygame.display.update()
main()
我在Google云端硬盘中上传这些图片:
https://drive.google.com/folderview?id=0Bx-bEVazt-TWUnRSZlJVRmhfQm8&usp=sharing
答案 0 :(得分:0)
绘图封面和手柄点击(并在2000ms后隐藏卡片)
的示例#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygame
class Tile:
def __init__(self, x, y, image, cover):
self.image = image
self.cover = cover
self.rect = pygame.Rect(x, y, 60, 60)
self.covered = True
self.time_to_cover = None
def draw(self, screen):
# draw cover or image
if self.covered:
screen.blit(self.cover, self.rect)
else:
screen.blit(self.image, self.rect)
def update(self):
# hide card (after 2000ms)
if not self.covered and pygame.time.get_ticks() >= self.time_to_cover:
self.covered = True
def handle_event(self, event):
# check left button click
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
# check position
if self.rect.collidepoint(event.pos):
self.covered = not self.covered
if not self.covered:
# if uncovered then set +2000ms to cover
self.time_to_cover = pygame.time.get_ticks() + 2000
#----------------------------------------------------------------------
# init
pygame.init()
screen = pygame.display.set_mode((320,320))
# create images
img = pygame.surface.Surface((60, 60))
img.fill((255,0,0))
cov = pygame.surface.Surface((60, 60))
cov.fill((0,255,0))
# create tiles
tiles = []
for y in range(5):
for x in range(5):
tiles.append( Tile(x*65, y*65, img, cov) )
# mainloop
clock = pygame.time.Clock()
running = True
while running:
# events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
for x in tiles:
x.handle_event(event)
# updates
for x in tiles:
x.update()
# draws
for x in tiles:
x.draw(screen)
pygame.display.flip()
# clock
clock.tick(25)
pygame.quit()