翁坤1125 发表于 2020-4-11 17:36:17

零基础入门学习Python85讲

本帖最后由 翁坤1125 于 2020-4-11 17:38 编辑

大概十六分钟左右打的代码
个人认为和小甲鱼打的一样啊
import pygame
import sys
from pygame.locals import *
from random import *

class Ball(pygame.sprite.Sprite):
    def __init__(self,image,position,speed):
      pygame.sprite.Sprite.__init__(self)

      self.image = pygame.image.load(image).convert_alpha()
      self.rect = self.image.get_rect()
      self.rect.left,self.rect.top = position
      self.speed = speed

def main():
    pygame.init()
    #自己的素材
    ball_image = "球.png"
    bg_image = "背景.png"

    running = True

    bg_size = width,height = 600,315#自己的图片大小
    screen = pygame.display.set_mode(bg_size)
    pygame.display.set_caption("P l a y a b a l l")#自己的标题

    background = pygame.image.load(bg_image).convert_alpha()

    balls = []

    for i in range(5):
      position = randint(0,width-46),randint(0,height-46)#自己的图片大小
      speed =
      ball = Ball(ball_image,position,speed)
      balls.append(ball)

    clock = pygame.time.Clock()

    while running:
      for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()

      screen.blit(background,(0,0))

      for each in balls:
            screen.blit(each,image,each.rect)

      pygame.display.flip()
      clock.tick(30)

if __name__ == "__main__":
    main()
哪里错了啊
显示NameError: name 'image' is not defined

zltzlt 发表于 2020-4-11 17:37:56

这样试试:

import pygame
import sys
from pygame.locals import *
from random import *


class Ball(pygame.sprite.Sprite):
    def __init__(self, image, position, speed):
      pygame.sprite.Sprite.__init__(self)

      self.image = pygame.image.load(image).convert_alpha()
      self.rect = self.image.get_rect()
      self.rect.left, self.rect.top = position
      self.speed = speed


def main():
    pygame.init()

    ball_image = "球.png"
    bg_image = "背景.png"

    running = True

    bg_size = width, height = 600, 315
    screen = pygame.display.set_mode(bg_size)
    pygame.display.set_caption("P l a y a b a l l")

    background = pygame.image.load(bg_image).convert_alpha()

    balls = []

    for i in range(5):
      position = randint(0, width - 46), randint(0, height - 46)
      speed =
      ball = Ball(ball_image, position, speed)
      balls.append(ball)

    clock = pygame.time.Clock()

    while running:
      for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()

      screen.blit(background, (0, 0))

      for each in balls:
            screen.blit(each, ball.image, each.rect)

      pygame.display.flip()
      clock.tick(30)


if __name__ == "__main__":
    main()

翁坤1125 发表于 2020-4-11 17:39:14

zltzlt 发表于 2020-4-11 17:37
这样试试:

TypeError: argument 1 must be pygame.Surface, not Ball

zltzlt 发表于 2020-4-11 17:40:05

翁坤1125 发表于 2020-4-11 17:39
TypeError: argument 1 must be pygame.Surface, not Ball

import pygame
import sys
from pygame.locals import *
from random import *


class Ball(pygame.sprite.Sprite):
    def __init__(self, image, position, speed):
      pygame.sprite.Sprite.__init__(self)

      self.image = pygame.image.load(image).convert_alpha()
      self.rect = self.image.get_rect()
      self.rect.left, self.rect.top = position
      self.speed = speed


def main():
    pygame.init()

    ball_image = "球.png"
    bg_image = "背景.png"

    running = True

    bg_size = width, height = 600, 315
    screen = pygame.display.set_mode(bg_size)
    pygame.display.set_caption("P l a y a b a l l")

    background = pygame.image.load(bg_image).convert_alpha()

    balls = []

    for i in range(5):
      position = randint(0, width - 46), randint(0, height - 46)
      speed =
      ball = Ball(ball_image, position, speed)
      balls.append(ball)

    clock = pygame.time.Clock()

    while running:
      for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()

      screen.blit(background, (0, 0))

      for each in balls:
            screen.blit(each.image, each.rect)

      pygame.display.flip()
      clock.tick(30)


if __name__ == "__main__":
    main()

翁坤1125 发表于 2020-4-11 17:43:06

zltzlt 发表于 2020-4-11 17:40


OK啦,Thanks♪(・ω・)ノ
页: [1]
查看完整版本: 零基础入门学习Python85讲