专栏导读
-
🌸 欢迎来到Python办公自动化专栏—Python处理办公问题,解放您的双手
-
-
-
-
📕 此外还有python基础专栏:请点击——>Python基础学习专栏求订阅
-
文章作者技术和水平有限,如果文中出现错误,希望大家能指正🙏
-
❤️ 欢迎各位佬关注! ❤️
之前的课程
1、小球类设计
self.radius,半径
self.pos = [x,y],初始坐标
self.velocity = [2, -2],速度self.velocity[0]--x轴移动,self.velocity[1]--y轴移动,
pygame.draw.circle(窗口对象, 颜色(RGB-元组), 初始位置(列表[x,y]), 半径(整数))
如果小球的x轴坐标 < 小球半径,或者,小球的x轴坐标 > 屏幕的宽度就设置方向相反
同理y轴
class Ball:
def __init__(self, radius):
self.radius = radius
self.pos = [screen_width // 2, screen_height - 20 - radius]
self.velocity = [2, -2]
def draw(self, surface):
pygame.draw.circle(surface, WHITE, self.pos, self.radius)
def update(self):
self.pos[0] += self.velocity[0]
self.pos[1] += self.velocity[1]
if self.pos[0] < self.radius or self.pos[0] > (screen_width - self.radius):
self.velocity[0] *= -1
if self.pos[1] <= self.radius or self.pos[1] > (screen_height - self.radius):
self.velocity[1] *= -1
2、挡板类的设计
self.width = width
self.height = height
self.pos = [screen_width // 2 - width // 2, screen_height - 20]
self.velocity = [-5, 0]
-
4、挡板的绘制,x1,y1矩形左上角坐标点, x2,y2矩形右下角坐标点
pygame.draw.rect((窗口对象, 颜色(RGB-元组), (x1,y1, x2,y2), 0, 0)
接收键盘的事件(左键和右键),设置限制不可以超出屏幕外面
class Paddle:
def __init__(self, width, height):
self.width = width
self.height = height
self.pos = [screen_width // 2 - width // 2, screen_height - 20]
self.velocity = [-5, 0]
def draw(self, surface):
pygame.draw.rect(surface, WHITE, (self.pos[0], self.pos[1], self.width, self.height), 0, 0)
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and self.pos[0] > 0:
self.pos[0] += self.velocity[0]
if keys[pygame.K_RIGHT] and self.pos[0] < screen_width - self.width:
self.pos[0] -= self.velocity[0]
3、砖块类
pygame.draw.rect(surface, self.color, self.rect)
class Brick:
def __init__(self, x, y, width, height, color):
self.rect = pygame.Rect(x, y, width, height)
self.color = color
def draw(self, surface):
pygame.draw.rect(surface, self.color, self.rect)
4、砖块与小球的边界碰撞检测
def check_collision(ball, brick):
if (brick.rect.x - ball.radius <= ball.pos[0] <= brick.rect.x + brick.rect.width + ball.radius) and (
brick.rect.y <= ball.pos[1] <= brick.rect.y + brick.rect.height):
return 1
if (brick.rect.y - ball.radius <= ball.pos[1] <= brick.rect.y + brick.rect.height + ball.radius) and (
brick.rect.x <= ball.pos[0] <= brick.rect.x + brick.rect.width):
return 2
return 0
5、检测到碰撞,删除砖块,改变运动方向
def update_bricks(ball, bricks):
score = 0
for brick in bricks[:]:
if check_collision(ball, brick) == 1:
bricks.remove(brick)
ball.velocity[0] *= -1
score += 10
break
elif check_collision(ball, brick) == 2:
bricks.remove(brick)
ball.velocity[1] *= -1
score += 10
break
return score
完整版代码
import math
import random
import pygame
import sys
class Ball:
def __init__(self, radius):
self.radius = radius
self.pos = [screen_width // 2, screen_height - 20 - radius]
self.velocity = [2, -2]
def draw(self, surface):
pygame.draw.circle(surface, WHITE, self.pos, self.radius)
def update(self):
self.pos[0] += self.velocity[0]
self.pos[1] += self.velocity[1]
if self.pos[0] < self.radius or self.pos[0] > (screen_width - self.radius):
self.velocity[0] *= -1
if self.pos[1] <= self.radius or self.pos[1] > (screen_height - self.radius):
self.velocity[1] *= -1
class Paddle:
def __init__(self, width, height):
self.width = width
self.height = height
self.pos = [screen_width // 2 - width // 2, screen_height - 20]
self.velocity = [-5, 0]
def draw(self, surface):
pygame.draw.rect(surface, WHITE, (self.pos[0], self.pos[1], self.width, self.height), 0, 0)
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and self.pos[0] > 0:
self.pos[0] += self.velocity[0]
if keys[pygame.K_RIGHT] and self.pos[0] < screen_width - self.width:
self.pos[0] -= self.velocity[0]
class Brick:
def __init__(self, x, y, width, height, color):
self.rect = pygame.Rect(x, y, width, height)
self.color = color
def draw(self, surface):
pygame.draw.rect(surface, self.color, self.rect)
def check_collision(ball, brick):
if (brick.rect.x - ball.radius <= ball.pos[0] <= brick.rect.x + brick.rect.width + ball.radius) and (
brick.rect.y <= ball.pos[1] <= brick.rect.y + brick.rect.height):
return 1
if (brick.rect.y - ball.radius <= ball.pos[1] <= brick.rect.y + brick.rect.height + ball.radius) and (
brick.rect.x <= ball.pos[0] <= brick.rect.x + brick.rect.width):
return 2
return 0
def update_bricks(ball, bricks):
score = 0
for brick in bricks[:]:
if check_collision(ball, brick) == 1:
bricks.remove(brick)
ball.velocity[0] *= -1
score += 10
break
elif check_collision(ball, brick) == 2:
bricks.remove(brick)
ball.velocity[1] *= -1
score += 10
break
return score
def create_explosion(brick):
pass
def update_explosions(explosions, bricks):
for explosion in explosions[:]:
if explosion.is_finished():
explosions.remove(explosion)
if explosion.intersects(brick):
bricks.remove(brick)
if __name__ == '__main__':
pygame.init()
screen_width, screen_height = 640, 480
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Bounce Game')
clock = pygame.time.Clock()
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
ball = Ball(10)
paddle = Paddle(80, 10)
bricks = []
for x in range(0, screen_width, 80):
for y in range(0, screen_height // 4, 20):
brick = Brick(x + 2, y + 2, 80 - 2, 20 - 2, (255, 255, 255))
bricks.append(brick)
score = 0
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
ball.update()
paddle.update()
if ball.pos[1] + ball.radius > paddle.pos[1]:
if ball.pos[0] < paddle.pos[0] or ball.pos[0] > paddle.pos[0] + paddle.width:
score -= 1
else:
ss = abs(ball.pos[0] - (paddle.pos[0]+paddle.width//2)) / 20
ball.velocity[0] = 2+ss*2
ball.velocity[1] *= -1
screen.fill(BLACK)
ball.draw(screen)
paddle.draw(screen)
xx = random.randint(0, 255)
for brick in bricks:
brick.draw(screen)
if ball.pos[1] <= screen_height//2:
score += update_bricks(ball, bricks)
font = pygame.font.Font(None, 36)
score_text = font.render('Score: ' + str(score), True, RED)
screen.blit(score_text, (10, 10))
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()
总结
-
希望对初学者有帮助
-
致力于办公自动化的小小程序员一枚
-
希望能得到大家的【一个免费关注】!感谢
-
求个 🤞 关注 🤞
-
-
求个 ❤️ 喜欢 ❤️
-
-
求个 👍 收藏 👍
-