瓷砖块在pygame平台游戏中呈现隐形
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了瓷砖块在pygame平台游戏中呈现隐形相关的知识,希望对你有一定的参考价值。
我正在使用pygame开发平台游戏,并试图通过使用多维列表来更轻松地创建关卡。
问题是,当我去运行游戏时,我的所有块都在那里(因为除非我移动到任何一侧,否则我不会掉头)但是,块不会呈现绿色填充。我不知道如何解决它,非常感谢任何帮助。
这是我的代码:
import pygame as pg
vec = pg.math.Vector2
pg.init()
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 50, 0)
WIDTH = 600
HEIGHT = 300
PLAYER_FRICTION = -0.12
PLAYER_ACC = 0.5
PLAYER_GRAV = 0.5
JUMP = 8
JUMP_COUNTER = 2
screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption("John's cool game")
clock = pg.time.Clock()
class Player(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((30, 30))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH / 2, HEIGHT / 2)
self.pos = vec(WIDTH / 3, HEIGHT / 2)
self.vel = vec(0, 0)
self.acc = vec(0, 0)
self.speed = 0
def update(self):
self.speed = 0
self.acc = vec(0, PLAYER_GRAV)
keys = pg.key.get_pressed()
if keys[pg.K_LEFT]:
self.speed = -5
if keys[pg.K_RIGHT]:
self.speed = 5
self.pos.x += self.speed
# equations of motion
self.vel += self.acc
self.pos += self.vel + 0.5 * self.acc
self.rect.midbottom = self.pos
# Scrolling effect
if self.rect.left < 150:
self.pos.x -= self.speed
for block in blocks:
block.rect.x -= self.speed
for powerup in powerups:
powerup.rect.x -= self.speed
if self.rect.right > WIDTH-150:
self.pos.x -= self.speed
for block in blocks:
block.rect.x -= self.speed
if block.rect.right < 0:
block.kill()
for powerup in powerups:
powerup.rect.x -= self.speed
if self.rect.top <= HEIGHT/4:
self.pos.y += abs(self.vel.y)
for block in blocks:
block.rect.y += abs(self.vel.y)
def jump(self):
self.vel.y -= JUMP
def shoot(self):
bullet = Bullet(self.rect.x, self.rect.centery)
all_sprites.add(bullet)
bullets.add(bullet)
class Block(pg.sprite.Sprite):
def __init__(self, x, y):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((30, 30))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Powerup(pg.sprite.Sprite):
def __init__(self, x, y):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((15, 15))
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Bullet(pg.sprite.Sprite):
def __init__(self, x, y):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((5, 5))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speedx = 7
def update(self):
self.rect.x += self.speedx
if self.rect.right > WIDTH:
self.kill()
all_sprites = pg.sprite.Group()
blocks = pg.sprite.Group()
powerups = pg.sprite.Group()
bullets = pg.sprite.Group()
player = Player()
powerup = Powerup(WIDTH/2, (HEIGHT-HEIGHT/6)-50)
powerups.add(powerup)
all_sprites.add(player)
all_sprites.add(blocks)
all_sprites.add(powerups)
jumpCounter = JUMP_COUNTER
doubleJump = False
BLOCK_LIST = []
level_1 = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ,1 ,1 ,1 ,1 ,1]]
for y in range(0, len(level_1)):
for x in range(0, len(level_1[y])):
if level_1[y][x] == 1:
BLOCK_LIST.append(Block(x*30, y*30))
for block in BLOCK_LIST:
blocks.add(block)
# Game loop / events
gameOver = False
while not gameOver:
clock.tick(60)
for event in pg.event.get():
if event.type == pg.QUIT:
gameOver = True
if event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE and jumpCounter > 0:
player.jump()
jumpCounter -= 1
if event.key == pg.K_s:
player.shoot()
all_sprites.update()
screen.fill(CYAN)
all_sprites.draw(screen)
pg.display.update()
# Getting the powerup
gotPowerup = pg.sprite.spritecollide(player, powerups, True)
if gotPowerup:
print("Got powerup")
doubleJump = True
# Stop the player from falling through platform
if player.vel.y > 0:
hits = pg.sprite.spritecollide(player, blocks, False)
if hits:
player.pos.y = hits[0].rect.top + 1
player.vel.y = 0
if doubleJump:
jumpCounter = JUMP_COUNTER
else:
jumpCounter = JUMP_COUNTER-1
pg.quit()
quit()
答案
在向此组添加块后,将组blocks
添加到all_sprites
for block in BLOCK_LIST:
blocks.add(block)
all_sprites.add(blocks)
import pygame as pg
vec = pg.math.Vector2
# --- constants ---
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 50, 0)
WIDTH = 600
HEIGHT = 300
PLAYER_FRICTION = -0.12
PLAYER_ACC = 0.5
PLAYER_GRAV = 0.5
JUMP = 8
JUMP_COUNTER = 2
# --- classes ---
class Player(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((30, 30))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH / 2, HEIGHT / 2)
self.pos = vec(WIDTH / 3, HEIGHT / 2)
self.vel = vec(0, 0)
self.acc = vec(0, 0)
self.speed = 0
def update(self):
self.speed = 0
self.acc = vec(0, PLAYER_GRAV)
keys = pg.key.get_pressed()
if keys[pg.K_LEFT]:
self.speed = -5
if keys[pg.K_RIGHT]:
self.speed = 5
self.pos.x += self.speed
# equations of motion
self.vel += self.acc
self.pos += self.vel + 0.5 * self.acc
self.rect.midbottom = self.pos
# Scrolling effect
if self.rect.left < 150:
self.pos.x -= self.speed
for block in blocks:
block.rect.x -= self.speed
for powerup in powerups:
powerup.rect.x -= self.speed
if self.rect.right > WIDTH-150:
self.pos.x -= self.speed
for block in blocks:
block.rect.x -= self.speed
if block.rect.right < 0:
block.kill()
for powerup in powerups:
powerup.rect.x -= self.speed
if self.rect.top <= HEIGHT/4:
self.pos.y += abs(self.vel.y)
for block in blocks:
block.rect.y += abs(self.vel.y)
def jump(self):
self.vel.y -= JUMP
def shoot(self):
bullet = Bullet(self.rect.x, self.rect.centery)
all_sprites.add(bullet)
bullets.add(bullet)
class Block(pg.sprite.Sprite):
def __init__(self, x, y):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((30, 30))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Powerup(pg.sprite.Sprite):
def __init__(self, x, y):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((15, 15))
self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Bullet(pg.sprite.Sprite):
def __init__(self, x, y):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((5, 5))
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.speedx = 7
def update(self):
self.rect.x += self.speedx
if self.rect.right > WIDTH:
self.kill()
# --- main ---
level_1 = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0 ,0 ,0 ,0 ,0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ,1 ,1 ,1 ,1 ,1]]
# - init -
pg.init()
screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption("John's cool game")
# - objects -
all_sprites = pg.sprite.Group()
blocks = pg.sprite.Group()
powerups = pg.sprite.Group()
bullets = pg.sprite.Group()
player = Player()
powerup = Powerup(WIDTH/2, (HEIGHT-HEIGHT/6)-50)
powerups.add(powerup)
all_sprites.add(player)
all_sprites.add(blocks)
all_sprites.add(powerups)
jumpCounter = JUMP_COUNTER
doubleJump = False
for y in range(0, len(level_1)):
for x in range(0, len(level_1[y])):
if level_1[y][x] == 1:
block = Block(x*30, y*30)
blocks.add(block)
all_sprites.add(blocks)
# - Game loop / events -
clock = pg.time.Clock()
gameOver = False
while not gameOver:
clock.tick(60)
# - events -
for event in pg.event.get():
if event.type == pg.QUIT:
gameOver = True
if event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE and jumpCounter > 0:
player.jump()
jumpCounter -= 1
if event.key == pg.K_s:
player.shoot()
# - updates -
all_sprites.update()
# Getting the powerup
gotPowerup = pg.sprite.spritecollide(player, powerups, True)
if gotPowerup:
print("Got powerup")
doubleJump = True
# Stop the player from falling through platform
if player.vel.y > 0:
hits = pg.sprite.spritecollide(player, blocks, False)
if hits:
player.pos.y = hits[0].rect.top + 1
player.vel.y = 0
if doubleJump:
jumpCounter = JUMP_COUNTER
else:
jumpCounter = JUMP_COUNTER-1
# - draws -
screen.fill(CYAN)
all_sprites.draw(screen)
pg.display.update()
# - end -
pg.quit()
#quit()
以上是关于瓷砖块在pygame平台游戏中呈现隐形的主要内容,如果未能解决你的问题,请参考以下文章