我有一个问题,我做了一个乒乓游戏,但我有一个问题,把分数打印到pyplay窗口。
我得到错误'TypeError:参数1必须是pygame.Surface,而不是str
我在文本中输入了blit,但出现了一个错误。我知道代码乱七八糟,我稍后会修复它
import pygame
from pygame.locals import *
from random import randint
import random
import time
pygame.display.set_caption("PONG!")
myimage = pygame.image.load("lose.png")
middle = pygame.image.load("middle.png")
pygame.init()
screensize = (640,480)
screen = pygame.display.set_mode(screensize)
clock = pygame.time.Clock()
PlayerProp = 0
AiProp = 0
Score = str(PlayerProp)
AiScore = str(AiProp)
game = True
while game == True:
class pong_:
def __init__(self, screensize):
self.screensize = screensize
self.centerx = int(screensize[0]*0.5)
self.centery = int(screensize[1]*0.5)
self.radius = 8
self.rect = pygame.Rect(self.centerx-self.radius,
self.centery-self.radius,
self.radius*2, self.radius*2)
self.color = (255,255,255)
self.direction = [1,1]
self.speedx = 3
self.speedy = 4
#CODE TASK: change speed as game progresses to make it harder mwahaha
self.hit_edge_left = False
self.hit_edge_right = False
def update(self, player_paddle, ai_paddle):
self.centerx += self.direction[0]*self.speedx
self.centery += self.direction[1]*self.speedy
self.rect.center = (self.centerx, self.centery)
#Make new random for each -1
if self.rect.top <= 0:
self.direction[1] = 1
elif self.rect.bottom >= self.screensize[1]-1:
self.direction[1] = -1
if self.rect.right >= self.screensize[0]-1:
self.hit_edge_right = True
elif self.rect.left <= 0:
self.hit_edge_left = True
if self.rect.colliderect(player_paddle.rect):
self.direction[0] = random.randrange(-2, -1)
if self.rect.colliderect(ai_paddle.rect):
self.direction[0] = random.randrange(2, 3)
def render(self, screen):
pygame.draw.circle(screen, self.color, self.rect.center, self.radius, 0)
pygame.draw.circle(screen, (0,0,0), self.rect.center, self.radius, 1)
class AIPaddle(object):
def __init__(self, screensize):
self.screensize = screensize
self.centerx = 4
self.centery = int(screensize[1]*0.5)
self.height = 100
self.width = 10
self.rect = pygame.Rect(0, self.centery-int(self.height*0.5), self.width, self.height)
self.color = (255,255,255)
#CODE TASK: Adjust size of AI paddle as match progresses to make it more difficult
self.speed = random.randrange(3, 4)
def update(self, pong):
if pong.rect.top < self.rect.top:
self.centery -= self.speed
elif pong.rect.bottom > self.rect.bottom:
self.centery += self.speed
self.rect.center = (self.centerx, self.centery)
def render(self, screen):
pygame.draw.rect(screen, self.color, self.rect, 0)
pygame.draw.rect(screen, (0,0,0), self.rect, 1)
class PlayerPaddle(object):
def __init__(self, screensize):
self.screensize = screensize
self.centerx = screensize[0]-5
self.centery = int(screensize[1]*0.5)
self.height = 100
self.width = 10
self.rect = pygame.Rect(0, self.centery-int(self.height*0.5), self.width, self.height)
self.color = (255,255,255)
#CODE TASK: Adjust size of player paddle as match progresses to make it more difficult
self.speed = 3
self.direction = 0
def update(self):
self.centery += self.direction*self.speed
self.rect.center = (self.centerx, self.centery)
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > self.screensize[1]-1:
self.rect.bottom = self.screensize[1]-1
def render(self, screen):
pygame.draw.rect(screen, self.color, self.rect, 0)
pygame.draw.rect(screen, (0,0,0), self.rect, 1)
def main():
pygame.init()
pong = pong_(screensize)
ai_paddle = AIPaddle(screensize)
player_paddle = PlayerPaddle(screensize)
running = True
while running:
#fps limit
clock.tick(64)
#big boy event handling
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == KEYDOWN:
if event.key == K_UP:
player_paddle.direction = -1
elif event.key == K_DOWN:
player_paddle.direction = 1
if event.type == KEYUP:
if event.key == K_UP and player_paddle.direction == -1:
player_paddle.direction = 0
if event.key == K_DOWN and player_paddle.direction == 1:
player_paddle.direction = 0
#object updates
ai_paddle.update(pong)
player_paddle.update()
pong.update(player_paddle, ai_paddle)
screen.fill((0,0,0))
screen.blit(middle, (0,0))
screen.blit(AiScore, (0,0))
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
basicFont = pygame.font.SysFont(None, 48)
#CODE TASK: Make some text on the screen over everything
#code task 2: Make game restart?
if pong.hit_edge_left:
print('You Won!')
running = False
elif pong.hit_edge_right:
print('You lose')
running = False
#rendering phase
ai_paddle.render(screen)
player_paddle.render(screen)
pong.render(screen)
pygame.display.flip()
main()
使用basicFont.render(…)
创建带有文本的曲面。然后,blit
。
https://www.pygame.org/docs/ref/font.html#pygame.font.Font.render
顺便说一句:当游戏进行时,不要在中定义
类
和main()
。你不必这么做。游戏时把它放在外面。
我无法理解Stackoverflow中的其他问题。当圆圈向屏幕末端移动时,它会向相反方向移动。 发出错误消息而未执行。 screen.blit(圆圈,[x,y]) 类型错误:参数1必须是pygame.Surface,而不是pygame.Rect 有什么问题吗?
我正在用python制作一个潜艇游戏,但当我试图运行它时,解释器给了我一个非常奇怪的错误:“TypeError:参数1必须是pygame.Surface,而不是type。”我试图在网上搜索我的答案,但这似乎不是很常见的错误。我也试着自己去发现错误,但我觉得一切都很好。下面是我认为错误所在的部分代码:
我一直在遵循的python flappy鸟AI教程从techwith timm,我得到了这个错误; 我的代码是这样的 我读过其他文章也有同样的问题,我理解,但是不是像那样的列表。我不知道该怎么办。 我试着做来获取“列表”的第一个图像,但它说对象不是列表。为什么?
我正在编写一个小型python游戏,我的一些代码似乎不起作用。看一看: 是啊......所以错误是:
我正在使用pygame创建一个游戏,但遇到了一个错误,我是pygame的新手。我正在使用python 3.8和pycharm社区版2020.1 这是我制作游戏的视频:https://www.youtube.com/watch?v=FfWpgLFMI7w这里是错误: 这是我的密码: