在Python中如何使用pygame模块?

在Python中如何使用pygame模块?,第1张

这个很简单,首先要安装,命令如下:pip install pygame

然后使用,代码如下:

import pygame

import sys

#初始化

pygame.init()

size=width,height=1000,800

speed=[-5,3]

bg=(0,0,0)

#创建窗口大小

screen=pygame.display.set_mode(size)

#窗口标题

pygame.display.set_caption("跳动的足球")

#图片

a=pygame.image.load("111.png")

#获得图像的位置矩形

position =a.get_rect()

while True :

for event in pygame.event.get():

if event.type ==pygame.QUIT:

sys.exit()

#移动图像

position=position.move(speed)

if position.left<0 or position.right>width:

#翻转

a=pygame.transform.flip(a,True,False)

#反向移动

speed[0]=-speed[0]

if position.top<0 or position.bottom>height:

speed[1]=-speed[1]

screen.fill(bg)

screen.blit(a,position)

pygame.display.flip()

pygame.time.delay(10)

Python与Pygame程序标准框架:

import pygame, sys

from pygame.locals import *

pygame.init()

SCREEN = pygame.display.set_mode((*1*, *2*))

pygame.display.set_caption("*3*")

while True:

    for event in pygame.event.get():

        if event.type == QUIT:

            pygame.quit()

            sys.exit()

在*1*处输入你的窗口的宽度,

在*2*处输入你的窗口的高度,

在*3*处输入你的窗口的顶部显示的标题。

如果运行时你看到一个黑色的窗口,顶部写着你在*3*处写的文字,那么——恭喜你!你的Pygame安装成功了!


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11629113.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-17
下一篇2023-05-17

发表评论

登录后才能评论

评论列表(0条)

    保存