【pygame系列 第四课 弹球游戏 您所在的位置:网站首页 弹珠游戏获胜可得13亿 【pygame系列 第四课 弹球游戏

【pygame系列 第四课 弹球游戏

2024-01-27 06:45| 来源: 网络整理| 查看: 265

本课任务在上一课基础上,实现下面几个任务。

第五步:窗体底部绘制一个挡板

第六步:用鼠标控制挡板左右移动

第七步:小球碰到挡板反弹,碰到底部结束

第八步:绘制游戏得分

第九步:绘制游戏结束界面文字

5.窗体底部绘制一个挡板

代码:

import pygame import sys import random pygame.init() size =width,height=400,300 screen = pygame.display.set_mode(size) pygame.display.set_caption("弹球游戏") # 背景白色 bg = (255,255,255) # 球的颜色 红色 ball_color = (255,0,0) # 球的大小 半径 ball_size = 20 # 球的初始位置 设置在窗口中心位置 pos_x,pos_y = width//2-ball_size,height//2-ball_size # 设置球的 初始速度 speed_x=random.randint(1,5) speed_y=random.randint(1,5) # 设置挡板的 颜色 蓝色 board_color = (0,0,255) # 设置挡板的 宽和高 board_width,board_height=50,10 # 设置挡板的初始位置 board_x,board_y = (width-board_width)//2,height-board_height while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill(bg) # 在窗口上绘制一个圆形 球 pygame.draw.circle(screen,ball_color,[pos_x,pos_y],ball_size) # 在窗口上绘制一个挡板 pygame.draw.rect(screen,board_color,[board_x,board_y,board_width,board_height]) pygame.display.flip() # 等待 100ms 避免刷新太快 pygame.time.wait(100) if pos_x width-ball_size: speed_x = -speed_x if pos_y height-ball_size: speed_y = -speed_y # 更改位置 pos_x += speed_x pos_y += speed_

效果:

6.用鼠标控制挡板左右移动

代码:

import pygame import sys import random pygame.init() size =width,height=400,300 screen = pygame.display.set_mode(size) pygame.display.set_caption("弹球游戏") # 背景白色 bg = (255,255,255) # 球的颜色 红色 ball_color = (255,0,0) # 球的大小 半径 ball_size = 20 # 球的初始位置 设置在窗口中心位置 pos_x,pos_y = width//2-ball_size,height//2-ball_size # 设置球的 初始速度 speed_x=random.randint(1,5) speed_y=random.randint(1,5) # 设置挡板的 颜色 蓝色 board_color = (0,0,255) # 设置挡板的 宽和高 board_width,board_height=50,4 # 设置挡板的初始位置 board_x,board_y = (width-board_width)//2,height-board_height while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() # 检测鼠标移动事件 if event.type ==pygame.MOUSEMOTION: # 获取鼠标移动的位置 mouse_x,mouse_y=pygame.mouse.get_pos() # 防止挡板出右边的边界 if mouse_x>(width-board_width): mouse_x=width-board_width # 设置挡板的x坐标为鼠标位置,跟着鼠标移动 board_x=mouse_x screen.fill(bg) # 在窗口上绘制一个圆形 球 pygame.draw.circle(screen,ball_color,[pos_x,pos_y],ball_size) # 在窗口上绘制一个挡板 pygame.draw.rect(screen,board_color,[board_x,board_y,board_width,board_height]) pygame.display.flip() # 等待 100ms 避免刷新太快 pygame.time.wait(100) if pos_x width-ball_size: speed_x = -speed_x if pos_y height-ball_size: speed_y = -speed_y # 更改位置 pos_x += speed_x pos_y += speed_y

效果:

7.小球碰到挡板反弹,碰到底部结束

代码:

import pygame import sys import random pygame.init() size =width,height=400,300 screen = pygame.display.set_mode(size) pygame.display.set_caption("弹球游戏") # 背景白色 bg = (255,255,255) # 球的颜色 红色 ball_color = (255,0,0) # 球的大小 半径 ball_size = 20 # 球的初始位置 设置在窗口中心位置 pos_x,pos_y = width//2-ball_size,height//2-ball_size # 设置球的 初始速度 speed_x=random.randint(1,5) speed_y=random.randint(1,5) # 设置挡板的 颜色 蓝色 board_color = (0,0,255) # 设置挡板的 宽和高 board_width,board_height=50,4 # 设置挡板的初始位置 board_x,board_y = (width-board_width)//2,height-board_height while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() # 检测鼠标移动事件 if event.type ==pygame.MOUSEMOTION: # 获取鼠标移动的位置 mouse_x,mouse_y=pygame.mouse.get_pos() # 防止挡板出右边的边界 if mouse_x>(width-board_width): mouse_x=width-board_width # 设置挡板的x坐标为鼠标位置,跟着鼠标移动 board_x=mouse_x screen.fill(bg) # 在窗口上绘制一个圆形 球 pygame.draw.circle(screen,ball_color,[pos_x,pos_y],ball_size) # 在窗口上绘制一个挡板 pygame.draw.rect(screen,board_color,[board_x,board_y,board_width,board_height]) pygame.display.flip() # 等待 100ms 避免刷新太快 pygame.time.wait(100) if pos_x width-ball_size: speed_x = -speed_x if pos_y =height-ball_size-board_height: # 如果球与board接触,就反弹 if board_x (width-board_width): mouse_x=width-board_width # 设置挡板的x坐标为鼠标位置,跟着鼠标移动 board_x=mouse_x screen.fill(bg) # 在窗口上绘制一个圆形 球 pygame.draw.circle(screen,ball_color,[pos_x,pos_y],ball_size) # 在窗口上绘制一个挡板 pygame.draw.rect(screen,board_color,[board_x,board_y,board_width,board_height]) # 显示计分 text = font.render("score:"+str(score), True, (255,0,0)) screen.blit(text,(10,10)) pygame.display.flip() # 等待 100ms 避免刷新太快 pygame.time.wait(100) if pos_x width-ball_size: speed_x = -speed_x if pos_y =height-ball_size-board_height: # 如果球与board接触,就反弹 if board_x


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有