使用pygame加载图片 [英] Image loading using pygame

查看:173
本文介绍了使用pygame加载图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的背景网格的代码

This is the code for my background grid

Background = pygame.display.set_mode((900 ,900))
Green = (45,198,14)
Background.fill(Green)

for i in range(0, 900, 50):
    pygame.draw.line(Background, (0, 0, 0), (0, i), (900, i))
    pygame.draw.line(Background, (0, 0, 0), (i, 0), (i, 900))
pygame.display.update()

while pygame.event.wait().type != pygame.QUIT:
    pass

我无法将任何图像加载到它上面,我是否必须将其转换为 n 图像然后是精灵,或者有没有办法将图像加载到网格上.我曾尝试使用 blit 功能,但它不会将图像放到网格上.

I can not get any images to load onto it would i have to convert this into n image and then a sprite or is there a way for me to load images onto the grid. I have tried to use the blit function but it wont put images onto the grid.

RedInfantry= pygame.image.load("H:\computer science\6.2\Coursework\Week\Red team\InfantryRedV20.gif").convert()


while True:
    Background.blit(RedInfantry,(0,0))

如果我将图像变成精灵,它是否允许我移动和删除网格上的图像?我是否必须在图像中创建网格以将其他图像闪烁到其上.

If i turn the images into a sprite would it allow me to move and remove images on the grid? Would i have to create the grid into the image to blit other images onto it.

我想使用的步兵图像

推荐答案

创建一个网格:

grid = [[None for i in range(0, 900, 50)] for j in range(0, 900, 50)]

将图像分配给网格中的字段.例如:

Assign the images to the field in the grid. e.g:

grid[3][2] = RedInfantry

在应用程序循环中绘制网格中的图像:

Draw the images in the grid in the application loop:

for i in range(len(grid)):
    for j in range(len(grid[i])):
        x, y = i * 50, j * 50
        image = grid[i][j]
        if image != None:
            Background.blit(RedInfantry,(x, y))

此外,我建议在主应用程序循环中连续绘制整个场景:

Furthermore I recommend to draw the entire scene continuously in the main application loop:

import pygame 
pygame.init()

Background = pygame.display.set_mode((900 ,900))
Green = (45,198,14)

RedInfantry = pygame.Surface((50, 50))
RedInfantry.fill((255, 0, 0))

grid = [[None for i in range(0, 900, 50)] for j in range(0, 900, 50)]
grid[3][2] = RedInfantry

run = True
while run:
    # handle events
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    # draw background
    Background.fill(Green)

    # draw scene
    for i in range(0, 900, 50):
        pygame.draw.line(Background, (0, 0, 0), (0, i), (900, i))
        pygame.draw.line(Background, (0, 0, 0), (i, 0), (i, 900))
    for i in range(len(grid)):
        for j in range(len(grid[i])):
            x, y = i * 50, j * 50
            image = grid[i][j]
            if image != None:
                Background.blit(image,(x, y))

    # update dispaly
    pygame.display.update()

这篇关于使用pygame加载图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆