python做游戏怎么实现窗口_python和py游戏窗口类

你在draw_sprite()中有事件循环,所以它一直运行到游戏结束,window.set_background永远不会执行。在

您的代码构造不正确。在

我试着更正它,稍后再发送代码。在

编辑:

如何组织代码的简单示例。在

现在它在一个文件中。在Pygame中总是一个窗口,所以不需要使Panel+Widnow。在run()中有一个事件循环。所有代码都在窗口内(创建精灵,更改背景,绘图等)。在

我将精灵添加到(python)列表并从列表中绘制所有精灵-player is exception-所以我只能从列表中移除最后一个精灵:/If您需要更好的东西,请参见pygame.sprite.Sprite()和pygame.sprite.Group()。在

使用Arrows移动红球,Space暂停游戏,ESC退出。在

最后我附上了我的位图。在import pygame

#from pygame.locals import *

#

# class for single sprite

#

class MySprite():

def __init__(self, image, x, y):

self.image = pygame.image.load(image)

image_rect = self.image.get_rect()

# Rect class to use "Sprite collision detect" - in the future

# In rect you have sprite position and size

# You can use self.rect.x, self.rect.y, self.rect.width, self.rect.height

# and self.rect.center, self.rect.centerx, self.rect.top, self.rect.bottomright etc.

self.rect = pygame.rect.Rect(x, y, image_rect.width, image_rect.height)

def draw(self, screen):

screen.blit(self.image, self.rect)

#

# class for player

#

class MyPlayer(MySprite):

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值