鱼C论坛

 找回密码
 立即注册
查看: 893|回复: 4

[已解决]零基础入门学习Python85讲

[复制链接]
发表于 2020-4-11 17:36:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

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

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

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

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

  17.     running = True

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

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

  22.     balls = []

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

  28.     clock = pygame.time.Clock()

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

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

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

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

  38. if __name__ == "__main__":
  39.     main()
复制代码

哪里错了啊
显示NameError: name 'image' is not defined
最佳答案
2020-4-11 17:40:05
翁坤1125 发表于 2020-4-11 17:39
TypeError: argument 1 must be pygame.Surface, not Ball

  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from random import *


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

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


  12. def main():
  13.     pygame.init()

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

  16.     running = True

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

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

  21.     balls = []

  22.     for i in range(5):
  23.         position = randint(0, width - 46), randint(0, height - 46)
  24.         speed = [randint(-10, 10), randint(-10, 10)]
  25.         ball = Ball(ball_image, position, speed)
  26.         balls.append(ball)

  27.     clock = pygame.time.Clock()

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

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

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

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


  37. if __name__ == "__main__":
  38.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-11 17:37:56 | 显示全部楼层
这样试试:

  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from random import *


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

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


  12. def main():
  13.     pygame.init()

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

  16.     running = True

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

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

  21.     balls = []

  22.     for i in range(5):
  23.         position = randint(0, width - 46), randint(0, height - 46)
  24.         speed = [randint(-10, 10), randint(-10, 10)]
  25.         ball = Ball(ball_image, position, speed)
  26.         balls.append(ball)

  27.     clock = pygame.time.Clock()

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

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

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

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


  37. if __name__ == "__main__":
  38.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-11 17:39:14 | 显示全部楼层

TypeError: argument 1 must be pygame.Surface, not Ball
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-11 17:40:05 | 显示全部楼层    本楼为最佳答案   
翁坤1125 发表于 2020-4-11 17:39
TypeError: argument 1 must be pygame.Surface, not Ball

  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from random import *


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

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


  12. def main():
  13.     pygame.init()

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

  16.     running = True

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

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

  21.     balls = []

  22.     for i in range(5):
  23.         position = randint(0, width - 46), randint(0, height - 46)
  24.         speed = [randint(-10, 10), randint(-10, 10)]
  25.         ball = Ball(ball_image, position, speed)
  26.         balls.append(ball)

  27.     clock = pygame.time.Clock()

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

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

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

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


  37. if __name__ == "__main__":
  38.     main()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-11 17:43:06 | 显示全部楼层

OK啦,Thanks♪(・ω・)ノ
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-12 21:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表