我正在做一个游戏,这里似乎有一些问题。这是我第一次使用sprite床单,如果我的错误很简单,我很抱歉。
对于大量的代码,我提前表示歉意,但不幸的是,我相信问题可能存在于代码中的任何地方。我收到的错误是“TypeError:参数1必须是pygame.Surface,而不是method”。这是指all_sprites.draw(screen)命令。我知道屏幕的格式是正确的,所以不是这样。我的老师说很可能是任何一个精灵。其中一个未正确格式化,或与图像不正确交互。请让我知道,如果你找到了我的问题的解决方案,我再次为代码的巨大范围道歉。谢谢
(我给我的不同文件贴上了标签,顺便说一句,gsettings,gmain和gsprites。你可以看到我在导入中使用它们,应该没什么大不了的,我只是想澄清一下)
完全例外`
Traceback (most recent call last):
File "/Users/AddisonWeatherhead/PycharmProjects/PyGame/Game 2/gmain.py", line 191, in <module>
game_play()
File "/Users/AddisonWeatherhead/PycharmProjects/PyGame/Game 2/gmain.py", line 128, in game_play
all_sprites.draw(screen)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pygame/sprite.py", line 475, in draw
self.spritedict[spr] = surface_blit(spr.image, spr.rect)
TypeError: argument 1 must be pygame.Surface, not method
Process finished with exit code 1
gsettings
WHITE = (255, 255, 255)
BLACK = (0, 0, 0,)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
GRAY = (180, 180, 180)
MAROON = (128, 0, 0)
NAVY = (0, 0, 128)
YELLOW = (255, 255, 0)
LIGHT_GRAY = (250, 250, 250)
LIGHT_BLUE = (144, 195, 212)
DISPLAY_WIDTH = 800
DISPLAY_HEIGHT = 800
FPS=60
gsprites
class sprite_sheet():
def __init__ (self, filename):
self.spritesheet = pygame.image.load(filename)
def get_image(self, x, y, width, height):
image = pygame.Surface((width, height))
image.blit(self.spritesheet, (0,0), (x, y, width, height))
return image
class Player(pygame.sprite.Sprite):
def __init__(self, hero_left_list, hero_right_list, left_shoot, right_shoot):
pygame.sprite.Sprite.__init__(self)
self.hero_left_list=hero_left_list
self.hero_right_list=hero_right_list
self.left_shoot=left_shoot
self.right_shoot=right_shoot
self.image = self.hero_right_list[0]
self.rect=self.image.get_rect()
self.rect.x=self.rect.x
self.rect.y=self.rect.y
self.changex=0 #Horizontal movement speed
self.changey=0 #Vertical movement speed
self.current_frame=0
self.delay=50 #In millisecond, this will be the delay BETWEEN each frame of a spritesheet (character walking, explosion exploding, etc)
self.last=pygame.time.get_ticks() #gets the time in milliseconds since pygame.init() was called
self.running_right=False
self.running_left=False
def update(self):
self.rect.x+=self.changex
self.rect.y+=self.changey
if pygame.key.get_pressed()[pygame.K_RIGHT]:
self.now=pygame.time.get_ticks() #also gets the time in milliseconds since pygame.init() was called, however this time its LATER than self.delay
self.running_right=True
self.changex=5
if self.now - self.last >self.delay: #Confirming that at least 5 milliseconds has passed since the last call of the function
self.last=self.now
self.current_frame=(self.current_frame+1)%(int(len(self.hero_right_list)))
self.image=self.hero_right_list[self.current_frame]
self.image=sprite_sheet("hero.png").get_image
elif pygame.key.get_pressed()[pygame.K_LEFT]:
self.running_left=True
self.changey=5
self.image=sprite_sheet.get_image("hero.png", )
elif pygame.key.get_pressed()[pygame.K_UP]:
self.running_left=False
self.running_right=False
self.image=sprite_sheet.get_image("hero.png")
格曼
import pygame
from gsettings import *
from gsprites import *
import time
pygame.init()
clock = pygame.time.Clock()
##########################################################################################
def score(score, lives):
background = sprite_sheet("background.jpg").get_image(0, 0, 800, 800)
screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
screen.blit(background, [0,0])
font = pygame.font.SysFont('Impact', 40, True, False)
text = font.render(('Score'+str(score)), True, WHITE)
screen.blit(text, (200, 50))
##########################################################################################
def game_start():
screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
text = "Press enter to play"
font = pygame.font.SysFont('Impact', 50, True, False)
text_render=font.render(text, True, BLACK)
background=sprite_sheet("background.jpg").get_image(0, 0, 800, 800)
screen.blit(background, [0, 0])
started=False
while started == False:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
quit()
sprite_sheet("background.jpg").get_image(0, 0, 800, 800)
screen.blit(text_render, [(DISPLAY_WIDTH / 2) - 150, (DISPLAY_HEIGHT / 2)])
if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
screen.blit(background, [0, 0])
print('last')
return True
print(started)
pygame.display.flip()
clock.tick(FPS)
##########################################################################################
def game_play():
pygame.init()
screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
hero_right_list = []
for x in range(6):
for y in range(2, 3):
right_image = sprite_sheet("hero.png").get_image(80 * x, 94 * y, 80, 94)
hero_right_list.append(right_image)
# Player is 80 wide 94 tall
hero_left_list = []
for image in hero_right_list:
hero_left_list.append(pygame.transform.flip(image, True, False))
explosion_list = []
for x in range(25):
for y in range(1, 2):
explosion_image = sprite_sheet("Explosion.png").get_image(192 * x, 195 * y, 192, 195)
explosion_list.append(explosion_image)
shoot_right=(sprite_sheet("hero.png").get_image(0, 94*3, 80, 94))
shoot_left=pygame.transform.flip(shoot_right, True, False)
#Creating Sprite groups
all_sprites=pygame.sprite.Group()
enemy_sprites=pygame.sprite.Group()
player_sprite=pygame.sprite.Group()
laser_sprites=pygame.sprite.Group()
explosion_sprite=pygame.sprite.Group()
#sprite objects
player=Player(hero_left_list, hero_right_list, shoot_left, shoot_right)
all_sprites.add(player)
player_sprite.add(player)
for val in range(10):
enemy_right=Enemy('right')
enemy_left=Enemy('left')
enemy_sprites.add(enemy_right)
enemy_sprites.add(enemy_left)
all_sprites.add(enemy_right)
all_sprites.add(enemy_left)
global game_score
game_score = 0
started=True
while started==True:
for event in pygame.event.get():
# events to end the game
if event.type == pygame.QUIT:
quit()
player.update()
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
player.image=shoot_right
laser_hits=pygame.sprite.groupcollide(laser_sprites, enemy_sprites, True, True)
for hit in laser_hits:
game_score+=5
explosion=Explosion(explosion_list)
explosion_sprite.add(explosion)
all_sprites.add(explosion)
enemy=Enemy()
enemy_sprites.add(enemy)
all_sprites.add(enemy)
score(score, 5)
#enemy_sprites.draw(screen) #NO ISSUES
#explosion_sprite.draw(screen) #NO ISSUES
#laser_sprites.draw(screen) #NO ISSUES
player_sprite.draw(screen) #ISSUES
pygame.display.flip()
clock.tick(FPS)
##########################################################################################
def game_over(score):
over_screen=pygame.display.set_mode([DISPLAY_WIDTH, DISPLAY_HEIGHT])
clock.tick(FPS)
end_message_1="GAME OVER!"
end_message_2="Your score was", score
end_message_3="Replay"
end_message_4="Quit"
font = pygame.font.SysFont('Comic Sans', 75, True, False)
#rendering text
end_message_1_render=font.render(end_message_1, True, RED)
end_message_2_render=font.render(end_message_2, True, BLACK)
end_message_3_render=font.render(end_message_3, True, BLACK)
end_message_4_render=font.render(end_message_4, True, BLACK)
started=False
while started == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
clicked=pygame.mouse.get_pressed()
pos=pygame.mouse.get_pos()
if pos[0]>0 and pos[0]<100 and pos[1]>0 and pos[1]<100:
if pygame.mouse.get_pressed()[0] ==1:
started=False
over_screen.fill(WHITE)
over_screen.blit(end_message_1, [(DISPLAY_WIDTH//2), (DISPLAY_HEIGHT//2)])
over_screen.blit(end_message_2, ((DISPLAY_WIDTH//2), ((DISPLAY_HEIGHT//2)+50)))
over_screen.blit(end_message_3, [((DISPLAY_WIDTH // 2)-20), ((DISPLAY_HEIGHT // 2) + 120)])
over_screen.blit(end_message_4, [((DISPLAY_WIDTH // 2)+20), ((DISPLAY_HEIGHT // 2) + 120)])
pygame.display.flip()
pygame.time.clock.tick(FPS)
started=True
##########################################################################################
game_start()
while True:
game_play()
game_over(game_score)
pygame.quit()
我正在使用pygame创建一个游戏,但遇到了一个错误,我是pygame的新手。我正在使用python 3.8和pycharm社区版2020.1 这是我制作游戏的视频:https://www.youtube.com/watch?v=FfWpgLFMI7w这里是错误: 这是我的密码:
我有一个问题,我做了一个乒乓游戏,但我有一个问题,把分数打印到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,我得到了这个错误; 我的代码是这样的 我读过其他文章也有同样的问题,我理解,但是不是像那样的列表。我不知道该怎么办。 我试着做来获取“列表”的第一个图像,但它说对象不是列表。为什么?