简体   繁体   English

我的 Pygame 精灵不会出现。 我的代码有什么问题?

[英]My Pygame sprite won't appear. What's wrong with my code?

I have been trying to make a little game using Pygame. This is my first time using Pygame and I have looked at many tutorials, but my sprite still won't appear.我一直在尝试用Pygame做一个小游戏。这是我第一次使用Pygame,我看了很多教程,但我的精灵仍然不会出现。 It only shows a black line.它只显示一条黑线。 How can I fix it?我该如何解决?

Xcord = 0
grey = (192,192,192)

import pygame, random, time

pygame.init()

import time

Color_line=(0,0,0)
screen = pygame.display.set_mode([1000, 500])
all_sprites_list = pygame.sprite.Group()

import pygame

grey = (192,192,192)
playerWidth = 50
playerHeight = 50
all_sprites_list = pygame.sprite.Group()

class Player(pygame.sprite.Sprite):
    def __init__(self, grey, playerWidth, playerHeight):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([50, 50])
        self.image.fill(grey)
        self.image.set_colorkey(grey)
        pygame.draw.rect(self.image, grey, [0, 0, playerWidth, playerHeight])
        self.rect = self.image.get_rect()

player = Player(grey, 50, 50)
player.rect.x = Xcord
player.rect.y = 400

def update(Player):
        pygame.sprite.Sprite.update(Player)
        player.rect.x = Xcord
        player.rect.y = 400

all_sprites_list.add(player)

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    screen.fill((255, 255, 255))

    all_sprites_list.update()

    pygame.draw.line(screen,Color_line,(0,500),(1000,500), 75)

    all_sprites_list.draw(screen)

    pygame.display.flip()

    Xcord =+ 50
    if Xcord == 400:
        Xcord == 0

pygame.quit()

I am kind of trying to make something similar to Google Chrome's no Wi-Fi dinosaur game .我正在尝试制作类似于 Google Chrome 的无 Wi-Fi 恐龙游戏

You have a few mistakes.你有一些错误。

First: you fill sprite with GRAY and you use set_key on GRAY , so the sprite is transparent and simply you can't see the sprite.首先:你用GRAY fill sprite 并在GRAY上使用set_key ,所以 sprite 是透明的,你根本看不到 sprite。

Second: the code runs very fast and the sprite leaves the window and you can't see the sprite.第二:代码运行速度非常快,精灵离开 window 就看不到精灵了。

Third: in the code if Xcord == 400: Xcord == 0 , you need = 0 instead of == 0 - and this is why the sprite leaves the window and never go back to position (0, 400)第三:在代码中if Xcord == 400: Xcord == 0 ,你需要= 0而不是== 0 - 这就是精灵离开 window 而从 go 回到 position (0, 400)的原因


Another problem is the big mess in the code - you even run some code two times.另一个问题是代码中的大混乱——您甚至将一些代码运行了两次。


My version with many changes.我的版本有很多变化。

# PEP8: all imports at start.
# PEP8: every module on a separate line
import pygame
import random
import time

# --- constants ---  # PEP8: `UPPER_CAS_NAMES`

GRAY = (192, 192, 192)
RED  = (255,   0,   0)

# --- classes ---  # PEP8: `CamelCaseName`

class Player(pygame.sprite.Sprite):

    def __init__(self, color, x, y, width, weight):  # you don't need prefix `player` in variables in class `Player`
        super().__init__()   # Python 3 method for running a function from the original class
        self.color = color

        self.image = pygame.Surface([width, weight])
        self.image.fill(color)
        #self.image.set_colorkey(color)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

    def update(self):
        self.rect.x += 5

        if self.rect.x >= 400:
            self.rect.x = 0

# --- main ---

color_line = (0,0,0)  # PEP8: spaces around `=`, space after `,`

pygame.init()
screen = pygame.display.set_mode([1000, 500])

all_sprites_list = pygame.sprite.Group()

player_width  = 50
player_weight = 50

player = Player(RED, 50, 400, 50, 50)  # color, x, y, width, height
all_sprites_list.add(player)

clock = pygame.time.Clock()

running = True
while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # - only updates -

    all_sprites_list.update()

    # - only draws -

    screen.fill((255, 255, 255))

    pygame.draw.line(screen, color_line, (0, 500), (1000, 500), 75)

    all_sprites_list.draw(screen)

    pygame.display.flip()

    clock.tick(30)  # Slow down to 30 FPS (frames per seconds)

pygame.quit()

PEP 8 -- Style Guide for Python Code PEP 8 -- Python 代码风格指南

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM