本游戏要使用的是由Pycharm中的pygame模块来实现的,也可以在python中运行。
Pygame和sys模块
import pygame
import sys
random模块
需要在屏幕上随机生成小球
from random import randint
窗口尺寸改变
可以调节游戏屏幕的大小
elif event.type == pygame.VIDEORESIZE:
size = w,h = event.w,event.h
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
pygame.VIDEORESIZE 这是窗口大小改变事件,事件发生后,返回 event.size元组,包含新窗口的宽度和高度。
.size[0] 高度,也可以用event.w表示
.size[1] 宽度,也可以用event.h表示
返回参数仅在事件发生时有用
键盘控制挡板
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if board_rect.left > 0 and board_rect.left w - 186:
board_rect.left -= board_x
elif board_rect.left 0:
board_rect.left = 0
board_rect.top -= board_y
elif event.key == pygame.K_RIGHT:
if board_rect.right >= 186 and board_rect.right < w:
board_rect.right += board_x
elif board_rect.right >= w:
board_rect.bottom += board_y
鼠标控制
elif event.type == pygame.MOUSEMOTION:
if event.buttons[0] == 1:
if event.pos[0] >= 0 and event.pos[0] < w - 186:
board_rect.left = event.pos[0]
elif event.pos[0] >= w - 186 and event.pos[0] w:
board_rect.left = w - 186
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if event.pos[0] >= 0 and event.pos[0] < w - 186:
board_rect.left = event.pos[0]
if event.pos[0] >= w - 186 and event.pos[0] w:
board_rect.left = w - 186
挡板接住小球并得分
if ball_y >= h - 37 and (ball_x >= board_rect.left - 20 and ball_x board_rect.left + 206):
move_y = - move_y
score += points
count += 1
if count == 5:
count = 0
points += points
if move_x > 0:
move_x += 1
else:
move_x -= 1
move_y -= 1
小球未接住小球
if ball_y > h - 27 and (ball_x < board_rect.left - 20 or ball_x > board_rect.left + 206):
ball_y = 200
break
小球移动
ball_x += move_x
ball_y += move_y
if ball_x 20 or ball_x >= w - 20:
move_x = - move_x
if ball_y 20:
move_y = - move_y
显示分数
my_score = font.render(str(score), False, (255, 255, 0))
screen.blit(my_score, (w - 100, 30))
点击下面链接即可获取源码
我正在「AI高效率【工作、学习】」和朋友们讨论有趣的话题,你⼀起来吧?
https://t.zsxq.com/11KVfN0RC
公众号 @AI宅玩貳号玩家 作者,一个靠AI助力学习与工作的痴迷实践者。全网粉丝2万+,连续不间断更新小白也能玩的AI,让你提前2小时下班。
星球上面有什么:
电子竞赛专栏、深度学习专栏、AI辅助学习专栏、AI工具变现专栏等
适合人群
在校学生:希望我的经验可以帮助你解决你遇到问题,
开发人员:分享自己的经验,并且可以扩宽自己的道路,学习到更多的内容
希望用副业赚钱:我们有用AI变现的方式,可以帮助你实现用副业赚钱
硬性的标准其实限制不了无限可能的我们,所以啊!少年们加油吧!
Original: https://blog.csdn.net/qq_48764574/article/details/114699737
Author: 牧子川
Title: pygame小游戏(接球小游戏)
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/780894/
转载文章受原作者版权保护。转载请注明原作者出处!