- Python
拯救黑洞项目步骤
- 2024-9-15 11:05:14 @
1、基础框架
import pygame
from pygame.locals import *
from sys import exit
# 初始化pygame
pygame.init()
# 设置窗口大小
screen = pygame.display.set_mode((800, 600))
# 设置游戏名称
pygame.display.set_caption('拯救黑洞')
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
# 填充屏幕背景颜色
screen.fill((255, 255, 255))
# 更新屏幕画面
pygame.display.update()
2、绘制黑洞
import pygame
from pygame.locals import *
from sys import exit
# 初始化pygame
pygame.init()
# 设置窗口大小
screen = pygame.display.set_mode((800, 600))
# 设置游戏名称
pygame.display.set_caption('拯救黑洞')
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
# 填充屏幕背景颜色
screen.fill((255, 255, 255))
# 绘制半径为100的黑洞pygame.draw.circle(归属于哪个窗口,颜色,位置,圆的半径)
c1 = pygame.draw.circle(screen, (0, 0, 0), (400, 300), 100)
# 更新屏幕画面
pygame.display.update()
3、绘制红球设置红球的运动系统
import pygame
from pygame.locals import *
from sys import exit
# 初始化变量
# 红球初始位置
x = 30
y = 30
# 红球的速度
x_speed = 2
y_speed =2
# 初始化pygame
pygame.init()
# 设置窗口大小
screen = pygame.display.set_mode((800, 600))
# 设置游戏名称
pygame.display.set_caption('拯救黑洞')
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
# 填充屏幕背景颜色
screen.fill((255, 255, 255))
# 绘制半径为100的黑洞pygame.draw.circle(归属于哪个窗口,颜色,位置,圆的半径)
c1 = pygame.draw.circle(screen, (0, 0, 0), (400, 300), 100)
# 绘制红球
c2 = pygame.draw.circle(screen, (255, 0, 0), (x, y), 30)
x += x_speed
y += y_speed
# 触边反弹
if x < 30 or x > 770:
x_speed = -x_speed
if y < 30 or y > 570:
y_speed = -y_speed
# 更新屏幕画面
pygame.display.update()
4、绘制文本图片
import pygame
from pygame.locals import *
from sys import exit
# 生成文本图片
def text_img(name, size, text, color):
font = pygame.font.SysFont(name, size)
image = font.render(text, True, color)
return image
# 初始化变量
# 红球初始位置
x = 30
y = 30
# 红球的速度
x_speed = 0.5
y_speed = 0.5
# 初始化pygame
pygame.init()
# 设置窗口大小
screen = pygame.display.set_mode((800, 600))
# 设置游戏名称
pygame.display.set_caption('拯救黑洞')
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
# 填充屏幕背景颜色
screen.fill((255, 255, 255))
# 绘制半径为100的黑洞pygame.draw.circle(归属于哪个窗口,颜色,位置,圆的半径)
c1 = pygame.draw.circle(screen, (0, 0, 0), (400, 300), 100)
# 绘制红球
c2 = pygame.draw.circle(screen, (255, 0, 0), (x, y), 30)
x += x_speed
y += y_speed
# 触边反弹
if x < 30 or x > 770:
x_speed = -x_speed
if y < 30 or y > 570:
y_speed = -y_speed
score = text_img('simhei', 25, '得分:' + str(num), (0, 0, 0))
screen.blit(score, (0, 0))
# 更新屏幕画面
pygame.display.update()
5、增加得分逻辑
import pygame
from pygame.locals import *
from sys import exit
# 生成文本图片
def text_img(name, size, text, color):
font = pygame.font.SysFont(name, size)
image = font.render(text, True, color)
return image
# 初始化变量
# 得分
num = 0
# 红球初始位置
x = 30
y = 30
# 红球的速度
x_speed = 0.5
y_speed = 0.5
# 初始化pygame
pygame.init()
# 设置窗口大小
screen = pygame.display.set_mode((800, 600))
# 设置游戏名称
pygame.display.set_caption('拯救黑洞')
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
elif event.type == MOUSEBUTTONDOWN:
if c1.contains(c2):
num += 1
# 填充屏幕背景颜色
screen.fill((255, 255, 255))
# 绘制半径为100的黑洞pygame.draw.circle(归属于哪个窗口,颜色,位置,圆的半径)
c1 = pygame.draw.circle(screen, (0, 0, 0), (400, 300), 100)
# 绘制红球
c2 = pygame.draw.circle(screen, (255, 0, 0), (x, y), 30)
x += x_speed
y += y_speed
# 触边反弹
if x < 30 or x > 770:
x_speed = -x_speed
if y < 30 or y > 570:
y_speed = -y_speed
score = text_img('simhei', 25, '得分:' + str(num), (0, 0, 0))
screen.blit(score, (0, 0))
# 更新屏幕画面
pygame.display.update()
0 条评论
目前还没有评论...