python弹小球
本帖最后由 学习编程中的Ben 于 2022-12-30 07:00 编辑对不起了各位支持我的鱼油,这段时间我白天恐怕是不能上线了,
但我会在晚上11点以后进行统一回复,所以大家给我发消息想收到回复就等到第二天吧
其他话也不说了,照常晒个作品
这次的作品是一个小动画,许多小球、正方形会在屏幕内滑动,遇到边缘反弹,他们还会时不时闪烁!
效果图依旧在下方
代码:
import pygame
import sys
from pygame.sprite import Group
from pygame.sprite import Sprite
import random
# 小球个数
number = int(input("请输入小球的个数:"))
move_speed = int(input("请输入小球的移动速度:"))
up_left = 7
up_right = 9
down_left = 3
down_right = 1
black = (0, 0, 0)
blue = (0, 0, 255)
windows_width = 1200
windows_height = 600
r = 10
# 小球类
class Bound(Sprite):
def __init__(self, direction, color, circle_x, circle_y):
super(Bound, self).__init__()
self.circle_x = circle_x
self.circle_y = circle_y
self.color = color
self.start_direction = direction
# 检查边界,并设置反弹
def check_border(self):
if self.circle_y <= 0:
# 左上
if self.start_direction == up_left:
self.start_direction = down_left
# 右上
elif self.start_direction == up_right:
self.start_direction = down_right
elif self.circle_y >= windows_height:
# 右下
if self.start_direction == down_right:
self.start_direction = up_right
# 左下
elif self.start_direction == down_left:
self.start_direction = up_left
elif self.circle_x <= 0:
# 左上
if self.start_direction == up_left:
self.start_direction = up_right
# 左下
elif self.start_direction == down_left:
self.start_direction = down_right
elif self.circle_x >= windows_width:
# 右上
if self.start_direction == up_right:
self.start_direction = up_left
# 右下
elif self.start_direction == down_right:
self.start_direction = down_left
# 判断走的方向,并更新数据
def type_judge(self):
if self.start_direction == up_right:
self.circle_y -= move_speed
self.circle_x += move_speed
elif self.start_direction == up_left:
self.circle_y -= move_speed
self.circle_x -= move_speed
elif self.start_direction == down_left:
self.circle_y += move_speed
self.circle_x -= move_speed
elif self.start_direction == down_right:
self.circle_y += move_speed
self.circle_x += move_speed
def draw_buf(self, windows):
pygame.draw.circle(windows, self.color, (self.circle_x, self.circle_y), r)
class Sq(Sprite):
def __init__(self, direction, start_x, start_y, color):
super(Sq, self).__init__()
self.color = color
self.start_direction = direction
self.rect = pygame.Rect(start_x, start_y, 20, 20)
# 检查边界,并设置反弹
def check_border(self):
if self.rect.top <= 0:
# 左上
if self.start_direction == up_left:
self.start_direction = down_left
# 右上
elif self.start_direction == up_right:
self.start_direction = down_right
elif self.rect.bottom >= windows_height:
# 右下
if self.start_direction == down_right:
self.start_direction = up_right
# 左下
elif self.start_direction == down_left:
self.start_direction = up_left
elif self.rect.left <= 0:
# 左上
if self.start_direction == up_left:
self.start_direction = up_right
# 左下
elif self.start_direction == down_left:
self.start_direction = down_right
elif self.rect.right >= windows_width:
# 右上
if self.start_direction == up_right:
self.start_direction = up_left
# 右下
elif self.start_direction == down_right:
self.start_direction = down_left
# 判断走的方向,并更新数据
def type_judge(self):
if self.start_direction == up_right:
self.rect.top -= move_speed
self.rect.right += move_speed
elif self.start_direction == up_left:
self.rect.top -= move_speed
self.rect.left -= move_speed
elif self.start_direction == down_left:
self.rect.bottom += move_speed
self.rect.left -= move_speed
elif self.start_direction == down_right:
self.rect.bottom += move_speed
self.rect.right += move_speed
def draw_buf(self, windows):
pygame.draw.rect(windows, self.color, self.rect)
# 主程序
def run():
pygame.init()
windows = pygame.display.set_mode((windows_width, windows_height))
pygame.display.set_caption('弹球游戏')
# 小球组
bufs = Group()
for i in range(number//2):
direction = random.choice()
color1 = random.randint(0, 255)
color2 = random.randint(0, 255)
color3 = random.randint(0, 255)
color =
circle_x = random.randint(0, windows_width)
circle_y = random.randint(0, windows_height)
new_buf = Bound(direction, color, circle_x, circle_y)
bufs.add(new_buf)
for i in range(number//2):
direction = random.choice()
color1 = random.randint(0, 255)
color2 = random.randint(0, 255)
color3 = random.randint(0, 255)
color =
start_x = random.randint(0, windows_width)
start_y = random.randint(0, windows_height)
new_buf = Sq(direction, start_x, start_y, color)
bufs.add(new_buf)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
for buf in bufs:
buf.check_border()
buf.type_judge()
buf.draw_buf(windows)
font1 = pygame.font.Font('resoures/myfont.ttf', 100)
tip1 = font1.render("PYTHON 弹小球", True, (255, 0, 0))
windows.blit(tip1, (250, 0))
pygame.display.flip()
pygame.time.delay(1)
windows.fill(black)
if __name__ == '__main__':
run()
望大家能多评一些分!!!
祝我早日升到中级鱼油!!!bye!明天见! {:10_254:} 别啊……呜呜呜
别伤心哈!
本帖最后由 学习编程中的Ben 于 2022-12-30 08:10 编辑Mike_python小 发表于 2022-12-30 08:00
别啊……呜呜呜
早上7:30前我能和你聊天,现在聊就很不安全,被爸妈发现了就是锁喉一击
先不和你聊了,明天早上再聊吧,或今天11点以后聊 学习编程中的Ben 发表于 2022-12-30 08:09
早上7:30前我能和你聊天,现在聊就很不安全,被爸妈发现了就是锁喉一击
先不和你聊了,明天早上再聊吧 ...
我也一样啊 {:10_275:} 对不起了各位支持我的鱼油,这段时间我白天恐怕是不能上线了,
但我会在早上11点以后进行统一回复,所以大家给我发消息想收到回复就等到第二天吧 大伙来帮忙顶一下呀!咋都没人了呢?求评分!!! 听说评分的人会有好运!!! 求点评 来顶一下吧 还有人能评点分吗? @不二如是 大佬评点分呗!
页:
[1]