如何在pygame的“内存"中的每个瓷砖上绘制封面 [英] How to draw cover on each tile in 'memory' in pygame
问题描述
我是 pygame 的初学者,我的母语不是英语.
I am a beginner in pygame and I am not a English native speaker.
我的任务是编写一款名为记忆"的游戏.该游戏包含 8 对图片,每张图片上都有一个封面.本周,我们的任务是绘制封面,如果您单击一个封面,该封面将消失.不过,虽然我画画成功了.我仍然无法正确绘制封面.实际上,我不知道绘制封面和单击封面使其消失的方法.然而,我搜索了很多,也许因为我不是母语人士,我无法真正理解它是如何工作的.因此,我希望有人能在这里帮助我.我将在下面提供我的代码和图片.谢谢大家!
My assignment is coding a game called 'Memory'. This game contains 8 pairs pictures and an cover exists on each pictures. This week, our assignment is draw covers and if you click an cover, this cover will disappear. However, although I draw pictures successfully. I still cannot draw covers properly. Actually, I don't know the approaches that to draw an cover and to click an cover and make it disappear. I searched a lot however, maybe because I am not a native speaker, I cannot really understand how it works. Therefore, I hope someone could help me here. I will provide my code and pictures below. Thanks everyone!
# 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 云端硬盘中:
I upload these pictures in my Google Drive:
https://drive.google.com/folderview?id=0Bx-bEVazt-TWUnRSZlJVRmhfQm8&usp=sharing
推荐答案
带有绘图封面和手柄点击的示例(并在 2000 毫秒后隐藏卡片)
Example with drawing cover and handle click (and hide card after 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()
这篇关于如何在pygame的“内存"中的每个瓷砖上绘制封面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!