Python之pygame基础

文章目录

一、下载

pip install pygame

Python之pygame基础

; 二、基础应用

2.1 生成窗口

代码如下(示例):

import pygame

pygame.init()

screen = pygame.display.set_mode((800,600))
screen.fill((255,255,255))
pygame.display.flip()

gRunning = True
while gRunning:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gRunning = False

pygame.quit()

这是一个渐变过程由黑变白(没有放GIF图)

Python之pygame基础

2.2 加入图片

代码如下(示例):


image = pygame.image.load(r'F:\python\python2021\python_gui\cdsc.jpg')

screen.blit(image,(0, 0))

Python之pygame基础

2.2 加入声音

代码如下(示例):


voice = pygame.mixer.music.load(r'F:\MP3音乐\千年之恋.mp3')
pygame.mixer.music.play()

2.3 改变颜色

代码如下(示例):

from pygame.color import THECOLORS

screen.fill(THECOLORS['red'])

Python之pygame基础

2.4 加入图形

代码如下(示例):


pygame.draw.rect(screen,THECOLORS['blue'],[0,0,200,400],0)
pygame.draw.circle(screen,THECOLORS['green'],[400,300],100,10)

Python之pygame基础

三、Pygame飞机

3.1 飞机跳跃

代码如下(示例):

gRunning = True
while gRunning:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gRunning = False
    pygame.time.delay(1000)
    pygame.bilt(img,(400,300))

    pygame.draw.rect(screen,THECOLORS['white'],img.get_rect(),0)
    pygame.display.flip()

Python之pygame基础

3.2 飞机碰壁回弹

代码如下(示例):


plane_x = 0
plane_y = 0

plane_speed_x = 5
plane_speed_y = 5
......

gRunning = True
while gRunning:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gRunning = False
    pygame.time.delay(20)
    pygame.draw.rect(screen,THECOLORS['white'],[plane_x,plane_y,img.get_width(),img.get_height()],0)
    plane_x = plane_x+plane_speed_x
    plane_y = plane_y+plane_speed_y
    if plane_x > 800-img.get_width() or plane_x < 0:
    plane_speed_x = - plane_speed_x
    if plane_y > 600-img.get_height() or plane_y < 0:
    plane_speed_y = - plane_speed_y
    screen.blit(img,(plane_x,plane_y))
    pygame.display.flip()

Python之pygame基础

3.3 模拟飞机

代码如下(示例):

class Plane:

    def __init__(self,img,position,speed,MaxWindow):
        self.img = img
        self.rect = img.get_rect()
        self.rect.left,self.rect.top = position
        self.speed = speed
        self.MaxWindow = MaxWindow

    def move(self):
        self.rect = self.rect.move(self.speed)
        if self.rect.left < 0 or self.rect.right >self.MaxWindow[0]:
            self.speed[0] = -self.speed[0]
        if self.rect.top< 0 or self.rect.bottom>self.MaxWindow[1]:
            self.speed[1] = -self.speed[1]

pygame = init()

W_size = (800,600)

screen = pygame.display.set_mode(W_size)
screen.fill(THECOLORES['white'])

planeImg = pygame.image.load('plane.png')

planes = []
for i in range(5)
    position = [random.randint(0,W_size[0]-planeImg.get_width(),random.randint(0,W_size[1]-planeImg.get_hright())]
    speed = [random.randint(-5,5),random.randint(-5,5)]
    planes.append(Plane(planeImg,position,speed,W_size))

pygame.display.flip()

gRunning = True
while gRunning:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gRunning = False
    pygame.time.delay(20)

    screen.fill(THECOLORES['white'])

    for plane in planes:
        plane.move()
        screen.blit(plane.img,plane.rect)

    pygame.display.flip()

pygame.quit()

Python之pygame基础

总结

问题1:使用相对路径加载图片出错

Python之pygame基础
PS F:\python\python2021> python -u “f:\python\python2021\python_gui\pygame1.py”
pygame 2.0.1 (SDL 2.0.14, Python 3.7.0)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File “f:\python\python2021\python_gui\pygame1.py”, line 14, in
image = pygame.image.load(img)
FileNotFoundError: No such file or directory.

Original: https://blog.csdn.net/HG0724/article/details/113003094
Author: 胜天半月子
Title: Python之pygame基础

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/781936/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球