猫史档案馆


【Python作品分享】打砖块【教程贴】

用户:王Jerry ゚X´王Jerry ゚X´查看:0 回复:1 评论:0 创建时间:2020-06-14T12:58:03


【作品展示】

center_image

 

【作品介绍】

打砖块

 

【作品源代码】

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

pygame.init()
windows_size = (width, height) = (800, 600)
surface = pygame.display.set_mode(windows_size)
pygame.display.set_caption("打砖块游戏")
background = pygame.Color(0, 0, 0)

fpsClock = pygame.time.Clock()
#导入挡板
bat = pygame.image.load("bat.png")
bat_width = bat.get_width()
bat_rect = bat.get_rect()

playerX = 24
playerY = 540
mousex, mousey = (playerX, playerY)
#导入小球
ball = pygame.image.load("ball.png")
ball_width = ball.get_width()
ball_height = ball.get_height()
ball_rect = ball.get_rect()
ball_startY = 200
speed = 3
bx, by = (playerX, ball_startY)
sx, sy = (speed, speed)
ballServed = False
ball_rect.topleft = (bx, by)

row = 5
column = 10
#导入砖块
brick = pygame.image.load("brick.png")
brick_width = brick.get_width()
brick_height = brick.get_height()
brick_height_space = brick_height + 8
brick_startX = 245
brick_startY = 100
bricks = []

for y in range(row):
    brickY = (y* brick_height_space) + brick_startY
    for x in range(column):
        brickX = (x* brick_width) + brick_startX
        bricks.append(Rect(brickX, brickY, brick_width, brick_height))
while True:
    surface.fill(background)
    surface.blit(bat, bat_rect)
    surface.blit(ball, ball_rect)
    for b in bricks:
        surface.blit(brick,b)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        elif event.type == MOUSEMOTION:
            mousex, mousey = event.pos
            if mousex < width - bat_width:
                bat_rect.topleft = (mousex, playerY)
            else:
                bat_rect.topleft = (width - bat_width, playerY)
        elif event.type == MOUSEBUTTONUP:
            if not ballServed:
                ballServed = True
        
        if ballServed:
            ball_rect.topleft = (bx, by)
            bx += sx
            by += sy
        if by <= 0:
            by = 0
            sy *= -1
        if bx <= 0:
            bx = 0
            sx *= -1
        if bx >= width - ball_width:
            bx = width - ball_width
            sx *= -1
        if by >= height - ball_height:
            ballServed = False
            bx, by = (playerX, ball_startY)
            bat_rect.topleft = (bx, by)
        if ball_rect.colliderect(bat_rect):
            by = playerY - ball_width
            sy *= -1

        brickHitIndex = ball_rect.collidelist(bricks)
        if brickHitIndex >= 0:
            hb = bricks[brickHitIndex]
            mx = bx + ball_width/2
            my = by + ball_height/2

            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第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
艾小德九世艾小德九世

来我们工作室吗?

点赞0


评论