Pygame 入门 --Pie游戏指导

1.打印文字

import sys

import pygame
from pygame.locals import  *
white =255,255,255;
blue=0,0,200
pygame.init()
screen=pygame.display.set_mode((600,500))
myfont=pygame.font.Font(None,60)
textImage=myfont.render("Hello Pygame",True,white)
while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()
        screen.fill(blue)
        screen.blit(textImage,(100,100))
        pygame.display.update()

2.绘制圆形

import sys

import pygame
from pygame.locals import  *
white =255,255,255;
blue=0,0,200
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Circles")
myfont=pygame.font.Font(None,60)
textImage=myfont.render("Hello Pygame",True,white)
while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()
        screen.fill((0,0,200))

        #draw a circle
        color =255,255,0
        posotion=300,250
        radius=100
        width=10
        pygame.draw.circle(screen,color,posotion,radius,width)
        pygame.display.update()

3.可自由移动矩形

import sys

import pygame
from pygame.locals import  *
white =255,255,255;
blue=0,0,200
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Rectangles")
pos_x=300
pos_y=250
vel_x=2
vel_y=1
myfont=pygame.font.Font(None,60)
textImage=myfont.render("Hello Pygame",True,white)
while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()
        screen.fill((0,0,200))

        # #draw a circle
        # color =255,255,0
        # posotion=300,250
        # radius=100
        # width=10
        # pygame.draw.circle(screen,color,posotion,radius,width)
        # pygame.display.update()

        #move a rectangles
        pos_x+=vel_x
        pos_y+=vel_y

        #keep rectangle on the screen
        if pos_x>500 or pos_x<0:
            vel_x=-vel_x
        if pos_y>500 or pos_y<0:
            vel_y=-vel_y

        #draw the rectangle
        color=255,255,0
        width=0 #solid fill
        pos=pos_x,pos_y,100,100
        pygame.draw.rect(screen,color,pos,width)
        pygame.display.update()

4.绘制弧形 

import sys
import  math
import pygame
from pygame.locals import  *
white =255,255,255;
blue=0,0,200
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Rectangles")
pos_x=300
pos_y=250
vel_x=2
vel_y=1
myfont=pygame.font.Font(None,60)
textImage=myfont.render("Hello Pygame",True,white)
while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()
        screen.fill((0,0,200))

        # #draw a circle
        # color =255,255,0
        # posotion=300,250
        # radius=100
        # width=10
        # pygame.draw.circle(screen,color,posotion,radius,width)
        # pygame.display.update()

        # #move a rectangles
        # pos_x+=vel_x
        # pos_y+=vel_y

        # #keep rectangle on the screen
        # if pos_x>500 or pos_x<0:
        #     vel_x=-vel_x
        # if pos_y>500 or pos_y<0:
        #     vel_y=-vel_y
        #
        # #draw the rectangle
        # color=255,255,0
        # width=0 #solid fill
        # pos=pos_x,pos_y,100,100
        # pygame.draw.rect(screen,color,pos,width)
        # pygame.display.update()


        #draw the arc
        color=255,0,255
        position=200,150,200,200
        start_angle=math.radians(0)
        end_angle=math.radians(180)
        width=8
        pygame.draw.arc(screen,color,position,start_angle,end_angle,width)
        pygame.display.update()

5.Pie游戏

import pygame
import math
import sys
from pygame.locals import *
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("The Pie Game - Press 1,2,3,4")
myfont = pygame.font.Font(None, 60)

color = 200, 80, 60
x = 300
y = 250
radius = 200
position = x-radius, y-radius, radius*2, radius*2
width = 8
pieces1 = False
pieces2 = False
pieces3 = False
pieces4 = False
start_angle1 = math.radians(0)
end_angle1 = math.radians(90)

start_angle2 = math.radians(90)
end_angle2 = math.radians(180)

start_angle3 = math.radians(180)
end_angle3 = math.radians(270)

start_angle4 = math.radians(270)
end_angle4 = math.radians(360)

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            sys.exit()
        elif event.type == KEYUP:
            if event.key == pygame.K_ESCAPE:
                sys.exit()
            elif event.key == pygame.K_1:
                pieces1 = True
            elif event.key == pygame.K_2:
                pieces2 = True
            elif event.key == pygame.K_3:
                pieces3 = True
            elif event.key == pygame.K_4:
                pieces4 = True
    # clear the screen
    screen.fill((0, 0, 200))

    # draw numbers
    text1 = myfont.render("1", True, color)
    screen.blit(text1, (x+radius/2, y-radius/2))
    text2 = myfont.render("2", True, color)
    screen.blit(text2, (x-radius/2, y-radius/2))
    text3 = myfont.render("3", True, color)
    screen.blit(text3, (x-radius/2, y+radius/2))
    text4 = myfont.render("4", True, color)
    screen.blit(text4, (x+radius/2, y+radius/2))

    # draw pieces
    if pieces1:
        pygame.draw.arc(screen, color, position, start_angle1, end_angle1, width)
        pygame.draw.line(screen, color, (x, y-radius), (x, y), width)
    if pieces2:
        pygame.draw.arc(screen, color, position, start_angle2, end_angle2, width)
        pygame.draw.line(screen, color, (x-radius, y), (x, y), width)
    if pieces3:
        pygame.draw.arc(screen, color, position, start_angle3, end_angle3, width)
        pygame.draw.line(screen, color, (x, y+radius), (x, y), width)
    if pieces4:
        pygame.draw.arc(screen, color, position, start_angle4, end_angle4, width)
        pygame.draw.line(screen, color, (x+radius, y), (x, y), width)

    if pieces1 and pieces2 and pieces3 and pieces4:
        color = 0, 255, 0

    pygame.display.update()

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭晋龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值