当前位置: 首页 > 工具软件 > 变色方块 > 使用案例 >

Python学习笔记_Pygame(基础学习 pie小游戏和 方块碰撞变色)

孙言
2023-12-01

下载Python3.6.5对应的pygame-1.9.4-cp36-cp36m-win32.whl文件在PIP中安装

1.画弧

import pygame
from pygame import *
import sys
import math
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("画弧")
while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()
    screen.fill((0,0,200))
    #draw acr
    color=255,0,255
    positions=200,150,200,200
    strat_angle=math.radians(0)
    end_angle=math.radians(180)
    width=5
    pygame.draw.arc(screen,color,positions,strat_angle,end_angle,width)
    pygame.display.update()

2.画圆

import pygame
from pygame.locals import *
import sys
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("Drawing Circles")
while True:
    for event in pygame.event.get():
        if event.type in(QUIT,KEYDOWN):
            sys.exit()
    screen.fill((0,0,200))
    color=255,255,0
    position=300,250
    radius=100
    width=10
    pygame.draw.circle(screen,color,position,radius,width)
    pygame.display.update()

3.画线

import pygame
from pygame import *
import sys
import random
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("画线")
while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()
    screen.fill((0,80,0))
    #draw   the line
    color=100,255,200
    width=10
    P_1 = random.randint(0,600),300
    P_2 =random.randint(0,600),300
    pygame.draw.line(screen,color,P_1,P_2,width)
    pygame.display.update()

4.画方块 变色

import pygame
import sys
from pygame.locals import *
import random
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("移动方块")
pos_x = 300
pos_y = 250
vel_x = 0.3
vel_y = 0.3
color = 255,255,0#方块色
Color=0,255,255#背景色
while True:
    for event in pygame.event.get():
        if event.type in (QUIT,KEYDOWN):
            sys.exit()
    screen.fill(Color)#RGB
    #move the rect
    pos_x += vel_x
    pos_y += vel_y
    #keep rect on the screen
    if pos_x > 500 or pos_x < 0:
        vel_x = -vel_x
        color = random.randint(0,255),random.randint(0,255),random.randint(0,255)
        Color = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
    if pos_y > 400 or pos_y < 0:
        vel_y = -vel_y
        color = random.randint(0,255),random.randint(0,255),random.randint(0,255)
        Color = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
    #draw rect
    width = 0#solid fill
    pos = pos_x,pos_y,100,100
    pygame.draw.rect(screen,color,pos,width)
    pygame.display.update()

5.PIe小游戏

import math
import pygame
from pygame.locals import *
import sys
pygame.init()
screen=pygame.display.set_mode((600,500))
myfont = pygame.font.Font(None,60)
pygame.display.set_caption("Pie游戏1,2,3,4")
Color=200,80,60
width = 4
x = 300
y = 250
radius = 200
position = x-radius, y-radius, radius*2, radius*2
piece1 = False
piece2 = False
piece3 = False
piece4 = False
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:
                piece1 = True
            elif event.key == pygame.K_2:
                piece2 = True
            elif event.key == pygame.K_3:
                piece3 = True
            elif event.key == pygame.K_4:
                piece4 = True
    #clean display
    screen.fill((0,0,200))
    #draw the four numbers
    textimg1=myfont.render("1",True,Color)
    screen.blit(textimg1,(x + radius/2-20,y-radius/2))
    textimg2 = myfont.render("2", True, Color)
    screen.blit(textimg2, (x - radius / 2, y - radius / 2))
    textimg3 = myfont.render("3", True, Color)
    screen.blit(textimg3, (x - radius / 2, y + radius / 2-20))
    textimg4 = myfont.render("4", True, Color)
    screen.blit(textimg4, (x + radius / 2 - 20, y + radius / 2-20))
    #should the pieces be drawn?
    if piece1:
        start_angle=math.radians(0)
        end_angle=math.radians(90)
        pygame.draw.arc(screen,Color,position,start_angle,end_angle,width)
        pygame.draw.line(screen,Color,(x,y),(x,y-radius),width)
        pygame.draw.line(screen,Color,(x,y),(x+radius,y),width)
    if piece2:
            start_angle = math.radians(90)
            end_angle = math.radians(180)
            pygame.draw.arc(screen, Color, position, start_angle, end_angle, width)
            pygame.draw.line(screen, Color, (x, y), (x, y - radius), width)
            pygame.draw.line(screen, Color, (x, y), (x - radius, y), width)
    if piece3:
            start_angle = math.radians(180)
            end_angle = math.radians(270)
            pygame.draw.arc(screen, Color, position, start_angle,end_angle, width)
            pygame.draw.line(screen, Color, (x, y), (x-radius, y), width)
            pygame.draw.line(screen, Color, (x, y), (x, y+radius), width)
    if piece3:
                start_angle = math.radians(270)
                end_angle = math.radians(360)
                pygame.draw.arc(screen, Color, position, start_angle, end_angle, width)
                pygame.draw.line(screen, Color, (x, y), (x , y+radius), width)
                pygame.draw.line(screen, Color, (x, y), (x+radius, y), width)
     #is the pie finished
    if piece1 and piece2 and piece3 and piece4:
        Color=0,255,0
    pygame.display.update()

 

 类似资料: