弹球小游戏

文章目录


前言

pygame实现的一个弹球小游戏,但是有一个不可言喻的bug ,但是不知道怎么解决,有哪位大佬看到后,能出个解决方案,可以吗


废话少说,直接上代码

import pygame

# 资源初始化
pygame.init()
# 创建游戏窗口
w=800
h=600
dsd=(w,h)
screen = pygame.display.set_mode(dsd, pygame.RESIZABLE)
img_ball = pygame.image.load("./img/fall.png")
pygame.display.set_caption("狗头")
# img_ball.get_width(100)
my_clock = pygame.time.Clock()

img_ball_rect = img_ball.get_rect()

key = True
x = 1
y = 1
# mon_x,mon_y=0,0
moving = False
while key:
    # 事件处理
    for even in pygame.event.get():
        if even.type == pygame.QUIT:
            key = False
            break

        if even.type == pygame.KEYDOWN:
            if even.key == pygame.K_LEFT :
                x-=1
                # print("niu")
            if even.key == pygame.K_RIGHT and x<screen_width:
                x+=1
                pass
            elif even.key == pygame.K_UP :
                y-=1
                print(y)
                pass
            elif even.key == pygame.K_DOWN and y<screen_height:
                y+=1
                print(y)
                pass

        if even.type == pygame.VIDEORESIZE:
            dsd=even.size
            screen = pygame.display.set_mode(dsd, pygame.RESIZABLE)
        screen_width, screen_height = dsd

        if even.type == pygame.MOUSEBUTTONDOWN:
            if even.button == 1:
                moving = True
                x=0
                y=0
            print("按下")
            # continue

        if even.type == pygame.MOUSEBUTTONUP:
            if even.button == 1:
                moving = False
                x += 1
                y += 1
            print("释放")
        if even.type == pygame.MOUSEMOTION and moving:
            img_ball_rect.x, img_ball_rect.y = even.pos


    # 游戏内部逻辑
    img_ball_rect = img_ball_rect.move(x, y)
    if img_ball_rect.right >= screen_width or img_ball_rect.left <= 0:
        x = -x
    elif img_ball_rect.bottom >= screen_height or img_ball_rect.top <= 0:
        y = -y
    # 更新画面
    screen.fill((255, 255, 255))
    screen.blit(img_ball, img_ball_rect)
    pygame.display.flip()
    pygame.display.update()
    my_clock.tick(1000)
#     退出
pygame.quit()

图片素材

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值