【学习记录】PYGAME实现打砖块游戏

资源下载:

https://github.com/Apress/python-pygame-raspberry-pi-game-dev-2e/tree/master/projects/bricks

代码展示:

from turtle import back
import pygame,os,sys
from pygame.locals import *


pygame.init()
fpsClock = pygame.time.Clock()

mainSurface = pygame.display.set_mode((800,600))
pygame.display.set_caption('Bricks')
black = pygame.Color(0,0,0)

'''bat init''' 
bat = pygame.image.load('bat.png')
playerY = 540
batRect = bat.get_rect()
mousex,mousey = (0,playerY)

''' ball init''' 
ball = pygame.image.load('ball.png')
ballRect = ball.get_rect()
ballStartY = 200
ballSpeed  = 3
ballServed = False
bx,by = (24,ballStartY)
sx,sy = (ballSpeed,ballSpeed)
ballRect.topleft = (bx,by)

''' brick init'''
brick  = pygame.image.load('brick.png')
bricks = []
for y in range(5):
    brickY = (y*24) +100
    for x in range(10):
        brickX = (x*31) +245
        width = brick.get_width()
        height = brick.get_height()
        rect = Rect(brickX,brickY,width,height)
        bricks.append(rect)


while True:
    mainSurface.fill(black)

    '''brick draw'''
    for b in bricks:
        mainSurface.blit(brick,b)

    '''bat and ball draw'''
    mainSurface.blit(bat,batRect)
    mainSurface.blit(ball,ballRect)
    
    '''events'''
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == MOUSEMOTION:
            mousex,mousey = event.pos
            if (mousex < 800 -55):
                batRect.topleft = (mousex,playerY)
            else:
                batRect.topleft = (800-55,playerY)
        elif event.type == MOUSEBUTTONUP and not ballServed:
            ballServed = True


    '''main game logic'''
    if ballServed:
        bx += sx
        by += sy
        ballRect.topleft = (bx,by)
    if (by <= 0):
        by = 0
        sy *= -1

    if (bx <= 0):
        bx = 0
        sx *= -1
    if (bx >= 800 -8):
        bx = 800 -8
        sx *= -1
    
    if (by >= 600 -8):
        ballServed = False
        bx,by = (24,ballStartY)
        ballSpeed = 3
        sx,sy = (ballSpeed,ballSpeed)
        ballRect.topleft=(bx,by)

    if ballRect.colliderect(batRect):
        by = playerY - 8
        sy *= -1



    '''collision detection'''
    # batHitIndex = batRect.collidelist(ballRect)
    # if batHitIndex:
    #     by += 1
    #     print('touch')
    
    brickHitIndex = ballRect.collidelist(bricks)
    if brickHitIndex >= 0:
        hb = bricks[brickHitIndex]
        mx = bx +4
        my = by+4
        if mx > hb.x + hb.width or mx< hb.x:
            sx *= -1
        else:
            sy *= -1
        del (bricks[brickHitIndex])
    
    pygame.display.update()
    fpsClock.tick(30)
    

实现截图:

参考:

《Python, Pygame, and Raspberry Pi Game Development by Sloan Kelly》

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值