拯救黑洞(一) (1)掌握pygame中draw方法的使用;

(2)掌握turtle中的坐标系与pygame中的区别;

(3)熟悉物体运动的原理并实现物体的运动及触边反弹;

(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


# 分数初始值为0
num = 0
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()
        elif event.type == MOUSEBUTTONDOWN:
            if c1.contains(c2):
                num += 1
    # 填充屏幕背景颜色
    screen.fill((255, 255, 255))
    # 绘制半径为100的黑洞
    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 条评论

目前还没有评论...