文章目录
前言
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()
图片素材