这里,我不会更多的去叙述关于pygame和python安装的问题,如果有这方面的问题,可以通过去百度来看如何修改,也可以加我v信:15182225438 ,我有过Mac和Ubuntu上的安装经验,我个人所使用的IDE是Pycharm,如果有相关的安装问题也可以私信我。
做为一个程序员,很多时候我们都是以helloworld 来开始一个程序的,今天我们就以Hello Pygame来开始我们Pygame的学习
第一个程序主要将标题设置为Hello Pygame,从文件目录下面读取两张图片,一张是背景图片,一张是鼠标移动的时候跟着鼠标移动的图片。代码如下,关于程序的问题,可以在文章下面评论,大家一起探讨研究
import pygame #imports the pygame package
from pygame.locals import * #imports a number of functions and con- stants (values that do not change) into the top-level namespace.eg :QUIT
"""
define images and initializing pygame
"""
background_image_filename = 'sushiplate.png'
mouse_image_filename = 'face.png'
pygame.init()
# create a display surface /resolution,flag,depth
screen = pygame.display.set_mode((640, 480), 0, 32)
# set the title
pygame.display.set_caption("Hello, Pygame!")
# load the two images for the background and mouse cursor.
background = pygame.image.load(background_image_filename).convert()
mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha() # you can change the convert_alpha() to convert() to see the effect
while True:
for event in pygame.event.get():
if event.type == QUIT:
exit()
# blits the background image to the screen
screen.blit(background, (0,0))
# get the coordinate of the mouse
x, y = pygame.mouse.get_pos()
x-= mouse_cursor.get_width() / 2
y-= mouse_cursor.get_height() / 2
# blits the face image to the screen
screen.blit(mouse_cursor, (x, y))
# updata to ensure that the image we have created in memory is shown to the user without flicker.
pygame.display.update()