python pygame 입문 강좌

8443 단어 pythonpygame
설치 하 다
cmd 명령 에 입력:pip install pygame설치 완료
둘째,첫 번 째 코드 인 스 턴 스
코드 가 빨리 안에 주석 이 있 으 니 모두 가 알 아 볼 수 있 을 것 이다.

import pygame
import sys
import pygame.locals


pygame.init()
#    

screen = pygame.display.set_mode((500, 600))
#        

pygame.display.set_caption("First Demo")
#        

Seashell = 255, 245, 238
#    RGB   

NavyBlue = 0, 0, 128
#    RGB   

while True:
    for event in pygame.event.get():
        if event.type == pygame.locals.QUIT or event.type == pygame.locals.KEYDOWN:
            #         ,       ,      
            sys.exit()
        else:
            pass
    screen.fill(Seashell)
    position = (250, 300)
    pygame.draw.circle(screen, color=NavyBlue, center=position, radius=100,  width=50)
    pygame.display.update()


실행 결과

이 실례 는 다음 과 같이 강조해 야 한다.
1.QUIT 는 닫 는 버튼 을 누 르 고 KEYDOWN 은 임의의 버튼 을 누 르 는 것 이 며 이 두 가 지 는 모두 pygame 내부 에서 스스로 정 의 된 상수 입 니 다.
2、색상 은 RGB 로 표시 가능
3.사각형 상자 그리 기

import pygame
import pygame.locals
import sys

pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Drawing Rectangles")
#     
Blue = 0, 0, 255
Purple = 160, 32, 240
while True:
    for event in pygame.event.get():
        if event.type == pygame.locals.QUIT or event.type == pygame.locals.KEYDOWN:
            #              ,         ,          
            sys.exit()
    pos = (300, 250, 100, 100)
    #     pos        ,           
    screen.fill(Purple)
    pygame.draw.rect(screen, Blue, pos, width=10)
    # width       ,screen                
    pygame.display.update()


코드 실행 결과;

4.사각형 상자 의 진급 버 전 그리 기

import pygame
import pygame.locals
import sys
import time


pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("Drawing Moving Rectangle")

color1 = 139, 0, 139
color2 = 104, 131, 139

px = 200
py = 300
#             ,               

vx = 10
vy = 20
#             ,             


while True:
    for event in pygame.event.get():
        if event.type in (pygame.locals.QUIT, pygame.locals.KEYDOWN):
            #                    ,      
            sys.exit()

    # vx = 10
    # vy = 20

    # px = 200
    # py = 300

    px += vx
    py += vy
    if px <= 0 or px + 100 >= 600:
        vx = - vx
    # else:
    #     px += vx


    if py <= 0 or py + 100 >= 500:
        vy = - vy
    # else:
    #     py += vy

    screen.fill(color1)

    pygame.draw.rect(screen, color2, (px, py, 100, 100))

    time.sleep(0.2)

    pygame.display.update()




5.직선 을 그립 니 다.

#     

import pygame
import pygame.locals
import sys
import time


color1 = 0, 80, 0

color2 = 100, 255, 200

pygame.init()
#    

screen = pygame.display.set_mode((600, 500))
#       

pygame.display.set_caption("Drawing Lines")
#            

while True:
    for event in pygame.event.get():
        if event.type == pygame.locals.QUIT or event.type == pygame.locals.KEYDOWN:
            sys.exit()
            #      ,                  ,         

    screen.fill(color1)

    pygame.draw.line(screen, color2, (150, 150), (450, 450), width=10)

    pygame.display.update()





코드 실행 결과 보기;

여기 서 우 리 는 라인 안의 각 매개 변수의 의 미 를 다시 한 번 상세 하 게 소개 합 니 다.

    pygame.draw.line(screen, color2, (150, 150), (450, 450), width=10)


1.첫 번 째 인자:화면 을 표시 할 사람 을 설정 합 니 다.
2.두 번 째 매개 변수:직선 구간 의 색상 설정
3.세 번 째 논술:출발점,즉 시작 하 는 위치 설정
4.네 번 째 매개 변수:종점,즉 정지 위치 설정
5.다섯 번 째 매개 변수:선의 폭 을 설정 하거나 굵기 정도
마지막 으로 한 마디 만 더 하 자.하나 더 하 는 거 잊 지 말고.pygame.display.update()화면 업데이트
6.포물선 을 그립 니 다.
우선,우 리 는 원 의 일부분,즉 진정한 원호 형 을 그립 니 다.

import math
import pygame
import pygame.locals
import sys
import time


color1 = 144, 238, 144
color2 = 0, 0, 139


pygame.init()
pygame.display.set_caption("Drawing Arcs")
screen = pygame.display.set_mode((600, 500))

while True:
    for event in pygame.event.get():
        if event.type in (pygame.locals.QUIT, pygame.locals.KEYDOWN):
            sys.exit()

    screen.fill(color1)

    ang1 = math.radians(45)
    #        
    ang2 = math.radians(315)
    #         

    #      
    """
       ;
         ,           
         ,           
         ,      
       :    x          
         ,      
       :    y          
    
      ,                ,              ,
             
    """
    rect1 = 100, 50, 400, 400
    #       ----      
    rect2 = 200, 200, 200, 100
    #       ----       

    pygame.draw.arc(screen, color2, rect1, ang1, ang2, width=10)
    #        
    """
         :  
         :  
         :     
         :     
         :     
    """

    pygame.display.update()


다음 에 우 리 는 타원형 의 일부분 을 그립 니 다.
이전 실례 에서 설명 한 바 와 같이 이 사각형 상자 의 길이 와 너비 가 같 지 않다 면,이 방법 을 사용 하면 축 소 된 타원형 을 그 릴 수 있다.
여 기 는 단지 이전 인 스 턴 스 의 rect 1 을 rect 2 로 바 꾸 었 을 뿐 다른 부분 은 아무런 변화 가 없습니다.

import math
import pygame
import pygame.locals
import sys
import time


color1 = 144, 238, 144
color2 = 0, 0, 139


pygame.init()
pygame.display.set_caption("Drawing Arcs")
screen = pygame.display.set_mode((600, 500))

while True:
    for event in pygame.event.get():
        if event.type in (pygame.locals.QUIT, pygame.locals.KEYDOWN):
            sys.exit()

    screen.fill(color1)

    ang1 = math.radians(45)
    #        
    ang2 = math.radians(315)
    #         

    #      
    """
       ;
         ,           
         ,           
         ,      
       :    x          
         ,      
       :    y          
    
      ,                ,              ,
             
    """
    rect1 = 100, 50, 400, 400
    #       ----      
    rect2 = 200, 200, 200, 100
    #       ----       

    pygame.draw.arc(screen, color2, rect2, ang1, ang2, width=10)
    #        
    """
         :  
         :  
         :     
         :     
         :     
    """

    pygame.display.update()

실행 코드 의 결 과 는 다음 그림 과 같다.

우 리 는 여기 서 확실히 수직 방향의 너 비 를 압축 하여 원래 의 원형 을 타원 으로 만 드 는 동시에 우 리 는 원호 형 을 타원 원호 형 으로 바 꾸 는 것 을 볼 수 있다.
종합 하면 이상 은 제 첫 번 째 노트 입 니 다.나중에 업 데 이 트 를 할 것 입 니 다.이것 은 첫 번 째 입 니 다.나중에 얼마나 생각 하지 못 했 는 지 모 르 겠 지만 어쨌든 제 가 pygame 을 다 배 울 때 까지 계속 업 데 이 트 를 할 것 입 니 다.나중에 페이지 게임,모 바 일 게임 등 을 배 울 기회 가 있 는 지 살 펴 보 겠 습 니 다.화 이 팅!
이상 은 python pygame 입문 튜 토리 얼 의 상세 한 내용 입 니 다.python pygame 입문 에 관 한 자 료 는 다른 관련 글 을 주목 하 세 요!

좋은 웹페이지 즐겨찾기