一:简介
- pygame专门用于多媒体(如电子游戏开发),其中包含对图像、声音、视频、事件、碰撞等支持,
- 如果想开发3D游戏,pygame就显得力不从心了,可以看看panda3d。
- pygame中的draw模块可以在窗口上绘图如线条、矩形、多边形、圆、椭圆、圆弧等。
- pygame中的image模块用来加载图片。
pip install pygame
二:大球吃小球案例
单击鼠标在鼠标单击位置产生一个随机大小,随机颜色的圆,然后不停的移动。再单击一个圆出来,当两个圆相撞的时候将小球隐藏掉,大球的半径变大。文章来源:https://www.toymoban.com/news/detail-538595.html
import pygame
from enum import Enum, unique
from math import sqrt
from random import randint
@unique
class Color(Enum):
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (242, 242, 242)
@staticmethod
def random_color():
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
return r, g, b
class Ball:
def __init__(self, x, y, radius, sx, sy, color=Color.RED):
self.x = x
self.y = y
self.radius = radius
self.sx = sx
self.sy = sy
self.color = color
self.alive = True
def move(self, screen):
"""移动"""
self.x += self.sx
self.y += self.sy
if self.x - self.radius <= 0 or self.x + self.radius >= screen.get_width():
self.sx = - self.sx
if self.y - self.radius <= 0 or self.y + self.radius >= screen.get_height():
self.sy = - self.sy
def eat(self, other):
"""吃其它球"""
if self.alive and other.alive and self != other:
dx, dy = self.x - other.x, self.y - other.y
distance = sqrt(dx ** 2 + dy ** 2)
if distance < self.radius + other.radius and self.radius > other.radius:
other.alive = False
self.radius = self.radius + int(other.radius * 0.146)
def draw(self, screen):
pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius, 0)
def main():
# 所有球
balls = []
pygame.init()
# 设置窗口标题
pygame.display.set_caption('大球吃小球')
# 显示窗口并设置窗口尺寸
screen = pygame.display.set_mode((800, 600))
# 加载图片
# ball_img = pygame.image.load('ball.png')
# 在窗口上渲染图片
# screen.blit(ball_img, (50, 50))
# 开启一个事件循环:用于处理发生的事件
running = True
while running:
# 从消息队列中获取事件并对事件进行处理
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# 鼠标事件
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
# 获取鼠标点击的位置
x, y = event.pos
radius = randint(10, 100)
sx, sy = randint(-10, 10), randint(-10, 10)
color = Color.random_color()
ball = Ball(x, y, radius, sx, sy, color)
balls.append(ball)
# 设置窗口的背景色RGB
screen.fill((255, 255, 255))
for ball in balls:
if ball.alive:
ball.draw(screen)
else:
balls.remove(ball)
# 刷新当前窗口(渲染窗口将绘制的图形呈现出来)
pygame.display.flip()
# 每隔50毫秒就改变小球的位置在刷新窗口
pygame.time.delay(50)
for ball in balls:
ball.move(screen)
for other in balls:
ball.eat(other)
if __name__ == '__main__':
main()
文章来源地址https://www.toymoban.com/news/detail-538595.html
到了这里,关于Python游戏篇:pygame的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!