纳西妲牌羽毛球 2023-02-19 16:00 采纳率: 75%
浏览 12
已结题

pygame制作打地鼠游戏时遇到的问题

大家好,我又双叒叕来提问了[doge]

最近在学pygame,学到制作打地鼠游戏时,我发现当我想通过点击来消除地鼠时,地鼠并不会消失

-------------------------------------------------------------下面是代码------------------------------------------------------------

import pygame
import random

pygame.init()
pygame.display.set_caption('打地鼠')
screen = pygame.display.set_mode((800, 600))
grass = pygame.image.load('D:\xxx\image\grass.png')
grass = pygame.transform.scale(grass, (800, 600))
hole = pygame.image.load('D:\xxx\image\hole.png')
mouse = pygame.image.load('D:\xxx\image\mouse.png')
hammer = pygame.image.load('D:\xxx\image\hammer.png')
mouse_list = []
mouse_list2 = []
rect_mouse = []
y = 3
x = 4

def new_mouse():
    while True:
        q = random.randint(0, x*y-1)
        abc = mouse_list[q]
        if mouse_list[q] not in mouse_list2:
            mouse_list2.append(abc)
            rect_mouse.append(pygame.Rect(abc[0], abc[1], 100, 112))
            break
        if len(mouse_list2) == x*y:
            break
    
#用户事件
EVENT = pygame.USEREVENT + 1
#计时器
pygame.time.set_timer(EVENT, 1000)
        
while True:
    pos = pygame.mouse.get_pos()
    
    hole_y = 200
    hole_x = 150
    screen.blit(grass, (0, 0))
    
    for i in range(y):
        hole_x = 150
        for o in range(x):
            mouse_list.append([hole_x, hole_y])
            screen.blit(hole, (hole_x, hole_y))
            hole_x += 150
        hole_y += 120
        
    for a in mouse_list2:
        screen.blit(mouse, [a[0], a[1]-40])
        
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
        if event.type == EVENT:
            new_mouse()
        if event.type == pygame.MOUSEBUTTONDOWN:
            for a in rect_mouse:
                if a.collidepoint(pos):
                    mouse_list2.remove([a[0], a[1]])
    
    screen.blit(hammer, [pos[0], pos[1]])

    pygame.display.update()
    pygame.time.Clock().tick(120)


-----------------------------------------下面是报错--------------------------------------

Traceback (most recent call last):
  File "D:\xxx\mouse.py", line 59, in <module>
    mouse_list2.remove([a[0], a[1]])
ValueError: list.remove(x): x not in list

希望大家给个解答,谢谢!

  • 写回答

4条回答 默认 最新

  • 梦想橡皮擦 Python领域优质创作者 2023-02-19 16:08
    关注

    方案来自 梦想橡皮擦 狂飙组基于 GPT 编写的 “程秘”


    你的代码中,当地鼠被点击时,虽然你使用了 collidepoint 方法来判断鼠标是否与地鼠重叠,但是你在移除地鼠时使用的是地鼠坐标的列表 [a[0], a[1]],而不是 rect_mouse 列表中的 Rect 对象。这样就导致了即使你点击了地鼠,它也不会被移除。

    你可以将 rect_mouse 列表改成存储 Rect 对象,然后在移除地鼠时使用 remove 方法移除对应的 Rect 对象。修改后的代码如下:

    import pygame
    import random
     
    pygame.init()
    pygame.display.set_caption('打地鼠')
    screen = pygame.display.set_mode((800, 600))
    grass = pygame.image.load('D:\xxx\image\grass.png')
    grass = pygame.transform.scale(grass, (800, 600))
    hole = pygame.image.load('D:\xxx\image\hole.png')
    mouse = pygame.image.load('D:\xxx\image\mouse.png')
    hammer = pygame.image.load('D:\xxx\image\hammer.png')
    mouse_list = []
    mouse_list2 = []
    rect_mouse = []
    y = 3
    x = 4
     
    def new_mouse():
        while True:
            q = random.randint(0, x*y-1)
            abc = mouse_list[q]
            if mouse_list[q] not in mouse_list2:
                mouse_list2.append(abc)
                rect_mouse.append(pygame.Rect(abc[0], abc[1], 100, 112))
                break
            if len(mouse_list2) == x*y:
                break
        
    #用户事件
    EVENT = pygame.USEREVENT + 1
    #计时器
    pygame.time.set_timer(EVENT, 1000)
            
    while True:
        pos = pygame.mouse.get_pos()
        
        hole_y = 200
        hole_x = 150
        screen.blit(grass, (0, 0))
        
        for i in range(y):
            hole_x = 150
            for o in range(x):
                mouse_list.append([hole_x, hole_y])
                screen.blit(hole, (hole_x, hole_y))
                hole_x += 150
            hole_y += 120
            
        for a in mouse_list2:
            screen.blit(mouse, [a[0], a[1]-40])
            
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            if event.type == EVENT:
                new_mouse()
            if event.type == pygame.MOUSEBUTTONDOWN:
                for a in rect_mouse:
                    if a.collidepoint(pos):
                        rect_mouse.remove(a)
                        mouse_list2.remove([a[0], a[1]])
        
        screen.blit(hammer, [pos[0], pos[1]])
     
        pygame.display.update()
        pygame.time.Clock().tick(120)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月3日
  • 已采纳回答 3月26日
  • 修改了问题 2月19日
  • 修改了问题 2月19日
  • 展开全部

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效