鱼C论坛

 找回密码
 立即注册
查看: 1349|回复: 2

pygame做一个斗地主代码,向大佬求助

[复制链接]
发表于 2023-10-25 20:42:33 From FishC Mobile | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#我定义一个更新精灵位置的函数updatp,但是鼠标点击后#不会变,一直搞不懂
import pygame
import random

class Nm():
    def __init__(self):
        self.pa=[None]*17

class Dz(Nm):
    def __init__(self):
        self.pa=[None]*20

def funa():
    num1=["黑桃","梅花","红心","方块"]
    num2=['3','4','5','6','7','8','9','10','J','Q','K','A','2']
    nums=[None]*54
    kls=0
    for i in num1:
        for j in num2:
            nums[kls]=i+j
            kls+=1
    nums[52]="大鬼"
    nums[53]="小鬼"
    return nums        

def funb(nm1,nm2,dz,nums):     
    kls=0
    while kls<17:
        
        card1 = random.choice(nums)
        if card1 in nums:
            nm1.pa[kls]=card1
            nums.remove(card1)
        card2 = random.choice(nums)
        if card2 in nums:
            nm2.pa[kls]=card2
            nums.remove(card2)
        card3 = random.choice(nums)
        if card3 in nums:
            dz.pa[kls]=card3
            nums.remove(card3)
        kls += 1
    kls = 17
    while kls < 20:
        card4 = random.choice(nums)
        if card4 in nums:
            dz.pa[kls]=card4
            nums.remove(card4)
        kls += 1

class Zhipai(pygame.sprite.Sprite):
    def __init__(self,name,x_l,y_l=440):
        super().__init__()
        self.image=pygame.image.load(name)
        self.rect=self.image.get_rect()
        self.rect.x=x_l*240
        self.rect.y=y_l

    def updatp(self):
        self.rect.y-=100
        
def main():
    nums=funa()                     #定义总牌
    nm1=Nm()
    nm2=Nm()
    dz=Dz()
    funb(nm1,nm2,dz,nums)           #随机给三个人发牌
   
    pygame.init()
    clock=pygame.time.Clock()
    screen = pygame.display.set_mode((960,640))
    pg=pygame.image.load("C:/Users/86184/Desktop/新建文件夹/doul.png")
    pl_1=Zhipai("C:/Users/86184/Desktop/新建文件夹/dour.png",1)
    pl_2=Zhipai("C:/Users/86184/Desktop/新建文件夹/dour.png",2)
    pl_3=Zhipai("C:/Users/86184/Desktop/新建文件夹/dour.png",3)
    screen.blit(pg,(0,0))
    enemy_group=pygame.sprite.Group(pl_1,pl_2,pl_3)
    running=True
    while running:
        clock.tick(60)
        for event_ in pygame.event.get():
            if event_.type==pygame.QUIT:
                running=False
            elif event_!=None:
                print(event_)
            elif event_.type==pygame.MOUSEBUTTONDOWN:
                pos=pygame.mouse.get_pos()
                if pos[0]>=480 and pos[0]<=480+60 and pos[1]>=440 and pos[1]<=440+78:
                    pl_2.updatp()               

        screen.blit(pg,(0,0))
        enemy_group.draw(screen)
        pygame.display.update()
    pygame.quit()
    print(nm1.pa)
    print(nm2.pa)
    print(dz.pa)
    exit()


if __name__=="__main__":
    mgs=main()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-25 20:42:46 | 显示全部楼层
问题出在updatp函数中,虽然代码中调用了updatp函数,但是实际上未对精灵位置进行更新。在updatp函数中,需要将精灵的y坐标减小100,以实现向上移动的效果。在游戏循环中,需要在每次循环结束后调用updatp函数来更新精灵位置。

修改updatp函数如下:
def updatp(self):
    self.rect.y -= 100

修改游戏循环部分的代码如下:
while running:
    clock.tick(60)
    for event_ in pygame.event.get():
        if event_.type == pygame.QUIT:
            running = False
        elif event_ != None:
            print(event_)
        elif event_.type == pygame.MOUSEBUTTONDOWN:
            pos = pygame.mouse.get_pos()
            if pos[0] >= 480 and pos[0] <= 480 + 60 and pos[1] >= 440 and pos[1] <= 440 + 78:
                pl_2.updatp()

    # 更新精灵位置
    pl_1.updatp()
    pl_2.updatp()
    pl_3.updatp()

    screen.blit(pg, (0, 0))
    enemy_group.draw(screen)
    pygame.display.update()

这样就能够在鼠标点击后实现精灵位置的更新了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-10-25 20:45:39 From FishC Mobile | 显示全部楼层
isdkz 发表于 2023-10-25 20:42
问题出在updatp函数中,虽然代码中调用了updatp函数,但是实际上未对精灵位置进行更新。在updatp函数中,需 ...

为什么在if判断鼠标位置点击后,调用了一次updatp函数,但它不会更位置?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 13:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表