我正在编写一个小型python游戏,我的一些代码似乎不起作用。看一看:
import pygame
import sys
import pygame.sprite as sprite
import time
pygame.init()
pygame.display.set_caption("Uni Mario")
m_x = 100
m_y = 350
width = 40
height = 60
vel = 5
left=False
right=False
walk_count = 0
walkRight = []
walkLeft = []
myfont = pygame.font.SysFont("monospace", 25)
screen_over = pygame.font.SysFont("monospace", 100)
hitcount = 0
score=0
clock = pygame.time.Clock()
FPS = 30
background_image = pygame.image.load('Sprites/bg2.png')
background_size = background_image.get_size()
background_rect = background_image.get_rect()
sw = 837
sh = 464
win = pygame.display.set_mode((sw, sh))
w,h = background_size
t_x = sw
t_y = 350
g_x = sw-100
bg_x1 = 0
bg_y1 = 0
bg_x2 = w
bg_y2 = 0
d2 = 9999
def load_img(file_name): # loads the image, makes the pure white background transparent
img = pygame.image.load(file_name).convert()
img.set_colorkey((255,255,255))
return img
for i in range(1,7):
walkLeft.append( load_img("Sprites/L" + str(i) + ".png" ) ) #loads in lists of images
walkRight.append( load_img("Sprites/R" + str(i) + ".png") )
player_image = walkRight[0]
turtle_image = [pygame.image.load('Sprites/Goomba1.png'), pygame.image.load('Sprites/Goomba2.png'), pygame.image.load('Sprites/Goomba3.png')]
#turtle_small = pygame.transform.scale(turtle_image, (100, 60))
pygame.mixer.music.load('Music/Bros.mp3')
pygame.mixer.music.play(-1)
isJump = False
jumpCount = 10
left_idx=0
right_idx=0
#images = []
#images.append(pygame.image.load('Sprites/Goomba1.png'))
#images.append(pygame.image.load('Sprites/Goomba2.png'))
#images.append(pygame.image.load('Sprites/Goomba3.png'))
#index = 0
#
#image = images[index]
#def update(images):
# index += 1
#if index >= len(images):
# index = 0
#image = images[self.index]
run = True #main loop
while run:
clock.tick(FPS)
pygame.time.delay(50)
win.blit(background_image,(sw, sh)) #makes a scrolling background
pygame.display.update()
bg_x2 -= 5
bg_x1 -= 5
if bg_x1 < sw - 2*w:
bg_x1 = sw
if bg_x2 < sw - 2*w:
bg_x2 = sw
t_x -=20
if t_x < 0:
t_x = sw
#g_x -= 20
#if g_x < 0:
# g_x = sw+500
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and m_x > vel:
m_x -= vel
if not isJump:
player_image = walkLeft[left_idx]
left_idx += 1
if left_idx >= len(walkLeft):
left_idx=0
if keys[pygame.K_RIGHT] and m_x < sw - width - vel:
m_x += vel
if not isJump:
player_image = walkRight[right_idx]
right_idx += 1
if right_idx >= len(walkRight):
right_idx=0
if not(isJump):
if keys[pygame.K_UP]:
isJump = True
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
m_y -= (jumpCount ** 2)* 0.25 * neg
jumpCount -= 1
else:
isJump = False
jumpCount = 10
if bg_x1 > -w:
win.blit(background_image,(bg_x1,bg_y1))
if bg_x2 > -w:
win.blit(background_image,(bg_x2,bg_y2))
win.blit(player_image, (m_x,m_y))
win.blit(turtle_image, (t_x, t_y)) #22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222
label = myfont.render("Hit Count = "+ str(hitcount), 1, (0, 0, 0))
win.blit(label, ((sw-200), 420))
label3 = myfont.render("If your hit count gets",1, (0, 0, 0))
win.blit(label3, (270, 10))
label4= myfont.render("to 3, you lose.", 1, (0 , 0, 0))
win.blit(label4, (270, 60))
label5 = myfont.render("Score: "+ str(score), 1, (0, 0, 0))
win.blit(label5, ((sw-180), 50))
pygame.display.update()
d2 = (t_x - m_x)**2 + (t_y - m_y)**2 #represents the distance between the two character
if d2 < 142: #keep it at 142, it seems to be a good distance for the hitcount
hitcount += 1
else:
score += 1/10
if hitcount >= 3:
label2 = screen_over.render("Game Over", 1, (255, 0, 0))
win.blit(label2, (230, 200))
pygame.display.update()
time.sleep(2)
run= False
if score >= 100:
label6 = screen_over.render("YOU WIN", 1, (255, 0, 0))
win.blit(label6, (230,200))
pygame.display.update()
time.sleep(5)
run=False
pygame.quit()
是啊......所以错误是:
Traceback (most recent call last):
File "C:\Users\aryaat\Desktop\SPython Gaming Project\Uni Mario(A).py", line 163, in <module>
win.blit(turtle_image, (t_x, t_y)) #22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222
TypeError: argument 1 must be pygame.Surface, not list
turtle\u image
不是pygame.Surface
对象,而是pygame.Surface
对象的列表:
turtle_image = [pygame.image.load('Sprites/Goomba1.png'), pygame.image.load('Sprites/Goomba2.png'), pygame.image.load('Sprites/Goomba3.png')]
您必须使用索引运算符(例如turtle_image[0]
)从列表中选择一个对象,您想要将其"blight"到窗口表面:
win.blit(turtle_image[0], (t_x, t_y))
我有一个问题,我做了一个乒乓游戏,但我有一个问题,把分数打印到pyplay窗口。 我得到错误'TypeError:参数1必须是pygame.Surface,而不是str 我在文本中输入了blit,但出现了一个错误。我知道代码乱七八糟,我稍后会修复它
我无法理解Stackoverflow中的其他问题。当圆圈向屏幕末端移动时,它会向相反方向移动。 发出错误消息而未执行。 screen.blit(圆圈,[x,y]) 类型错误:参数1必须是pygame.Surface,而不是pygame.Rect 有什么问题吗?
我正在用python制作一个潜艇游戏,但当我试图运行它时,解释器给了我一个非常奇怪的错误:“TypeError:参数1必须是pygame.Surface,而不是type。”我试图在网上搜索我的答案,但这似乎不是很常见的错误。我也试着自己去发现错误,但我觉得一切都很好。下面是我认为错误所在的部分代码:
我一直在遵循的python flappy鸟AI教程从techwith timm,我得到了这个错误; 我的代码是这样的 我读过其他文章也有同样的问题,我理解,但是不是像那样的列表。我不知道该怎么办。 我试着做来获取“列表”的第一个图像,但它说对象不是列表。为什么?
我正在使用pygame创建一个游戏,但遇到了一个错误,我是pygame的新手。我正在使用python 3.8和pycharm社区版2020.1 这是我制作游戏的视频:https://www.youtube.com/watch?v=FfWpgLFMI7w这里是错误: 这是我的密码: