Нет изображения в окне, pygame
При запуске кода должна появляться картинка, но она не появляется, просто фон.
Думал что проблема в разрешении картинки, сменил его на 34*17, результатов не дало.
Интерпретатор ошибок не выдал. Помогите пожалуйста исправить код.
код:
import pygame
FPS = 60
W = 700 # ширина экрана
H = 300 # высота экрана
WHITE = (255, 255, 255)
LEFT="to the left"
RIGHT="to the right"
UP="to the up"
DOWN="to the down"
STOP="stop"
pygame.init()
sc = pygame.display.set_mode((W, H))
clock = pygame.time.Clock()
x = W // 2
y = H // 2
w=30
h=30
motion=STOP
image = pygame.image.load('C:\\Users\\Pavel\\Desktop\\game\\image1.jpg').convert()
while 1:
sc.fill(WHITE)
pygame.display.update()
for i in pygame.event.get():
sc.blit(image, [x, y])
if i.type == pygame.QUIT:
exit()
elif y==0:
y=1
elif i.type == pygame.KEYDOWN:
if i.key == pygame.K_LEFT:
motion=LEFT
elif i.key == pygame.K_RIGHT:
motion=RIGHT
elif i.key == pygame.K_DOWN:
motion=DOWN
elif i.key == pygame.K_UP:
motion=UP
elif i.mod == pygame.KMOD_LSHIFT:
y+=50
if motion==LEFT:
x-=3
elif motion==RIGHT:
x+=3
elif motion==UP:
y-=3
elif motion==DOWN:
y+=3
pygame.time.delay(60)
pygame.display.flip()
Ответы (1 шт):
Автор решения: S. Nick
→ Ссылка
Попробуйте так:
import sys
import pygame
pygame.init()
width = 800
height = 600
boyut = (width, height)
pencere = pygame.display.set_mode(boyut)
clock = pygame.time.Clock()
all_sprites = pygame.sprite.Group()
class Parca(pygame.sprite.Sprite):
def __init__(self,x = width /2,y = height / 2):
super().__init__()
self.image = pygame.image.load("ball.png").convert()
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.rect.center = (x,y)
def update(self, *args):
up,down,right,left = args
if self.rect.x > width:
self.rect.x = 0
if self.rect.x < 0:
self.rect.x = width
if self.rect.y > height:
self.rect.y = 0
if self.rect.y < 0:
self.rect.y = height
if right:
self.rect.x += 10
if left:
self.rect.x -= 10
if up:
self.rect.y -= 10
if down:
self.rect.y += 10
parca1 = Parca()
all_sprites.add(parca1)
while True:
keys = pygame.key.get_pressed()
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:sys.exit()
if keys[pygame.K_UP] or keys[pygame.K_DOWN] or keys[pygame.K_LEFT] or keys[pygame.K_RIGHT]:
up,down,right,left = keys[pygame.K_UP],keys[pygame.K_DOWN],keys[pygame.K_RIGHT],keys[pygame.K_LEFT]
all_sprites.update(up,down,right,left)
pencere.fill((255,255,255))
all_sprites.draw(pencere)
pygame.display.update()