pygame学习的第一天

pygame最小开发框架:

import pygame, sys

pygame.init()
screen = pygame.display.set_mode((600, 480))
pygame.display.set_caption("Pygame游戏之旅")

while True:
    for event in pygame.event.get():
        if event == pygame.QUIT:
            sys.exit()
    pygame.display.update()

 

pygame壁球小游戏(不可控制版):

import pygame, sys

pygame.init()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.jpg")
ballrect = ball.get_rect()
fps = 300
fclock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event == pygame.QUIT:
            sys.exit()
    ballrect = ballrect.move(speed[0], speed[1])
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    screen.fill(BLACK)
    screen.blit(ball, ballrect)
    fclock.tick(fps)
    pygame.display.update()

pygame壁球小游戏(键盘控制速度版):

import pygame, sys

pygame.init()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.jpg")
ballrect = ball.get_rect()
fps = 100
fclock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                speed[0] -= 1
            elif event.key == pygame.K_RIGHT:
                speed[0] += 1
            elif event.key == pygame.K_DOWN:
                speed[1] += 1
            elif event.key == pygame.K_UP:
                speed[1] -= 1

    ballrect = ballrect.move(speed[0], speed[1])
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    screen.fill(BLACK)
    screen.blit(ball, ballrect)
    fclock.tick(fps)
    pygame.display.update()

游戏屏幕的设置:

pygame.display.set_mode(r = (0, 0), flags = 0)
r是游戏屏幕的分辨率,采用(width, height)方式输入
flags用来控制显示类型,可用|组合使用,常用显示标签如下:
pygame.RESIZABLE    窗口大小可调
pygame.NOFRAME      窗口没有边界显示
pygame.FULLSCREEN  窗口全屏显示

pygame壁球游戏(可调整窗口大小版):

import pygame, sys

pygame.init()
vInfo = pygame.display.Info()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.jpg")
ballrect = ball.get_rect()
fps = 100
fclock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                speed[0] -= 1
            elif event.key == pygame.K_RIGHT:
                speed[0] += 1
            elif event.key == pygame.K_DOWN:
                speed[1] += 1
            elif event.key == pygame.K_UP:
                speed[1] -= 1
            elif event.key == pygame.K_ESCAPE:
                sys.exit()
        elif event.type == pygame.VIDEORESIZE:
            size = width, height = event.size[0], event.size[1]
            screen = pygame.display.set_mode(size, pygame.RESIZABLE)
    ballrect = ballrect.move(speed[0], speed[1])
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    screen.fill(BLACK)
    screen.blit(ball, ballrect)
    fclock.tick(fps)
    pygame.display.update()

pygame壁纸游戏(全屏版):

import pygame, sys

pygame.init()
vInfo = pygame.display.Info()
size = width, height = vInfo.current_w, vInfo.current_h
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size, pygame.FULLSCREEN)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.jpg")
ballrect = ball.get_rect()
fps = 100
fclock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                speed[0] -= 1
            elif event.key == pygame.K_RIGHT:
                speed[0] += 1
            elif event.key == pygame.K_DOWN:
                speed[1] += 1
            elif event.key == pygame.K_UP:
                speed[1] -= 1
            elif event.key == pygame.K_ESCAPE:
                sys.exit()
    ballrect = ballrect.move(speed[0], speed[1])
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    screen.fill(BLACK)
    screen.blit(ball, ballrect)
    fclock.tick(fps)
    pygame.display.update()

pygame壁球游戏(最小化可暂停版):

import pygame, sys

pygame.init()
vInfo = pygame.display.Info()
size = width, height = 600, 400
speed = [1, 1]
BLACK = 0, 0, 0
screen = pygame.display.set_mode(size, pygame.RESIZABLE)
pygame.display.set_caption("Pygame壁球")
ball = pygame.image.load("PYG02-ball.jpg")
ballrect = ball.get_rect()
fps = 100
fclock = pygame.time.Clock()

while True:
    for event in pygame.event.get():
        if event == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                speed[0] -= 1
            elif event.key == pygame.K_RIGHT:
                speed[0] += 1
            elif event.key == pygame.K_DOWN:
                speed[1] += 1
            elif event.key == pygame.K_UP:
                speed[1] -= 1
            elif event.key == pygame.K_ESCAPE:
                sys.exit()
        elif event.type == pygame.VIDEORESIZE:
            size = width, height = event.size[0], event.size[1]
            screen = pygame.display.set_mode(size, pygame.RESIZABLE)
    if pygame.display.get_active():
        ballrect = ballrect.move(speed[0], speed[1])
    if ballrect.left < 0 or ballrect.right > width:
        speed[0] = -speed[0]
    if ballrect.top < 0 or ballrect.bottom > height:
        speed[1] = -speed[1]

    screen.fill(BLACK)
    screen.blit(ball, ballrect)
    fclock.tick(fps)
    pygame.display.update()

 

posted @ 2020-02-26 20:19  SwiftAC  阅读(139)  评论(0编辑  收藏  举报