【问题标题】:Python pygame TypeError: can only concatenate list (not "float") to listPython pygame TypeError:只能将列表(不是“浮动”)连接到列表
【发布时间】:2021-12-31 11:56:26
【问题描述】:

我儿子正忙着用 pygame 在 Python 3.8.10 64 位中编写 pong。

这是终端的反馈...

Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS D:\Ruan Dercksen\Documents\python code> & C:/Users/Ruan/AppData/Local/Programs/Python/Python38/python.exe "d:/Ruan Dercksen/Documents/python code/regte code/templatepong.py"
pygame 2.1.2 (SDL 2.0.18, Python 3.8.10)
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
  File "d:/Ruan Dercksen/Documents/python code/regte code/templatepong.py", line 124, in <module>
    ball_y = direction_y * speed + ball_y
TypeError: can only concatenate list (not "float") to list

这是他的代码...

import pygame as pg
import random

pg.init()

# ------------------------------- Variable declarations -------------------------------

# Define dimensions
width = 600
height = 600

# Define colours
black = (0, 0, 0)
white = (225, 225, 225)

# Define flags
done = False

# Define game variables

#padels varibel
padel_x = [30, width-30]
padel_y = [height/2, height/2]

# score varible
scores = [0, 0]

#ball varibils
ball_x = width/2
ball_y = height/2

direction_x = 1
direction_y = 0
speed = 1

# Create screen
screen = pg.display.set_mode((width, height))
pg.display.set_caption('pong')

# ------------------------------- Function definitions -------------------------------

# Draw the game board
def draw_board():
    # Fill the screen
    screen.fill(black)
    # Draw the grid lines
    pg.draw.line(screen, white, (width/2, 0), (width/2, height), 7)
    pg.draw.line(screen, white, (0, 90), (width, 90), 7)

# Draw the player scores
def draw_scores():
    font = pg.font.Font(None, 80)
    text = font.render(str(scores[0]), 1, white)
    text_rect = text.get_rect(center=(width/4, 45))
    screen.blit(text, text_rect)

    font = pg.font.Font(None, 80)
    text = font.render(str(scores[1]), 1, white)
    text_rect = text.get_rect(center=(width/4*3, 45))
    screen.blit(text, text_rect)

# Draw the player paddles
def draw_paddles(x, y):
    
    # Draw left paddle
    pg.draw.rect(screen, white, (x[0], y[0], 10, 60))
    
    # Draw right paddle
    pg.draw.rect(screen, white, (x[1], y[1], 10, 60))

# Draw the ball
def draw_ball(x, y):
    pg.draw.rect(screen, white, (x, y, 10, 10))

# Check for collisions between the ball and other objects
def check_collision():
    
    # Define global variables
    global direction_x, direction_y, scores
    
    # Collisions with paddles, walls, ceiling, and floor
    if ball_x >= width - 5 or ball_x <= 5:
        scores[0 if ball_x>= width-5 else 1] += 1
        direction_x *= -1
    elif ball_y >=height - 5 or ball_y <=95:
        direction_y *= -1
    elif ball_x >= padel_x[1] and ball_y >= padel_y[1] and ball_y <= padel_y[1] + 60:
        direction_x *= -1
        direction_y = random.choices([1, -1])
    elif ball_x<= padel_x[0] + 10 and ball_y >= padel_y[0] and ball_y <= padel_y[0] + 60:
        direction_x *= -1
        direction_y = random.choices([1, -1])



    


# Intialize the game board for the first time

# ------------------------------- Main game loop -------------------------------
while not done:
    # Get all the keys that were pressed
    pressed = pg.key.get_pressed()
    
    
    # Move the paddles
    if pressed [pg.K_UP]:
        if padel_y[1] >= 95:
            padel_y[1] -= 1
    elif pressed [pg.K_DOWN]:
        if padel_y[1] + 65 <= height:
            padel_y[1] += 1
    elif pressed[pg.K_w]:
        if padel_y[0] >= 95:
            padel_y[0] -=1
    elif pressed[pg.K_s]:
        if padel_y[0] + 65 <= height:
            padel_y[0] += 1
    
    # Move the ball
    ball_x = direction_x * speed + ball_x

    ball_y = direction_y * speed + ball_y
    
    # Check for quit event
    for event in pg.event.get():
        if event.type == pg.QUIT:
            done = True
    
    # Update screen
    draw_board()
    draw_scores()
    draw_paddles(padel_x, padel_y)
    draw_ball(ball_x, ball_y)
    check_collision()
    pg.display.update()

我们将不胜感激有关如何解决此问题并使游戏正常运行的任何帮助。

【问题讨论】:

  • 如果我的回答解决了您的问题,请考虑接受(答案左侧的复选标记)。

标签: python python-3.x


【解决方案1】:

您必须在函数check_collisions 中调用random.choice 而不是random.choices

第一个从可迭代对象中随机选择一个元素,而第二个从可迭代对象中选择一个元素列表。

引发错误的原因是,在以下行:

ball_y = direction_y * speed + ball_y

在您的主循环中,您对floatlist(由random.choices 产生)和int 执行无效操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    相关资源
    最近更新 更多