我正在通过制作一些基本游戏来学习pygame,在下面我正在尝试制作一个记忆拼图,其中有4种形状,其中4种颜色隐藏在黑卡/封面后面,用户必须点击一张黑卡/覆盖以显示其背后的形状,并且必须通过猜测将其与具有相同颜色的其他形状匹配。
到目前为止我为这些卡提供的代码
import pygame as pg
import sys
import random
window_wth = 720
window_hgt = 360
window_resolution = window_wth, window_hgt
fps = 15
card_wth = 72
card_hgt = 92
gap_x = 77
gap_y = 97
c1 = (random.randrange(255), random.randrange(255), random.randrange(255))
c2 = (random.randrange(255), random.randrange(255), random.randrange(255))
c3 = (random.randrange(255), random.randrange(255), random.randrange(255))
c4 = (random.randrange(255), random.randrange(255), random.randrange(255))
bkg_colour = (30, 30, 30)
pg.display.set_caption("Game")
main_display = pg.display.set_mode(window_resolution)
pos_x = int((window_wth % gap_x) / 2)
pos_y = int((window_hgt % gap_y) / 2)
def list_draw_rct(tt_pos_x, tt_pos_y):
rect_areas1 = []
for x in range(tt_pos_x, window_wth - gap_x, gap_x):
for y in range(tt_pos_y, window_hgt - gap_y, gap_y):
colour = (random.randrange(255), random.randrange(255), random.randrange(255))
rect_area1 = pg.Rect(x, y, card_wth, card_hgt)
rect_areas1.append(rect_area1)
# print(rect_areas1)
return rect_areas1
def main():
global fps_clock, main_display
fps_clock = pg.time.Clock()
while True:
for event in pg.event.get():
if event.type == pg.KEYDOWN and event.key == pg.K_s:
for i in range(len(list_draw_rct(pos_x, pos_y))):
colour = (random.randrange(255), random.randrange(255), random.randrange(255))
pg.draw.rect(main_display, colour, list_draw_rct(pos_x, pos_y)[i], 5)
pg.display.update(list_draw_rct(pos_x, pos_y)[:5] + list_draw_rct(pos_x,pos_y)[(6):])
fps_clock.tick(fps)
if event.type == pg.QUIT:
sys.exit()
if __name__ == '__main__':
main()
这里我已经硬编码删除第5个元素
if event.type == pg.KEYDOWN and event.key == pg.K_s:
for i in range(len(list_draw_rct(pos_x, pos_y))):
colour = (random.randrange(255), random.randrange(255), random.randrange(255))
pg.draw.rect(main_display, colour, list_draw_rct(pos_x, pos_y)[i], 5)
pg.display.update(list_draw_rct(pos_x, pos_y)[:5] + list_draw_rct(pos_x,pos_y)[(6):])
fps_clock.tick(fps)
但我不知道如何通过用户输入来做到这一点。
答案 0 :(得分:0)
我创建了一个卡片类,然后在创建时为每张卡片添加了一个矩形和颜色。然后我检查按下鼠标按钮然后获取鼠标位置并执行rect.collidepoint(pos)检查,并删除该卡:
end()