self.rect.x = Game.screen.get_rect().centerx ^^^^^^^^^^^^^^^^^^^^ AttributeError: 'int' object has no attribute 'get_rect'
时间: 2023-08-04 16:03:35 浏览: 139
这个错误表示你尝试在一个整数对象上调用 `get_rect()` 方法,但整数对象没有名为 `get_rect()` 的属性。
通常情况下,`get_rect()` 方法是用于获取图像或表面对象的矩形区域的方法。你可能在一个整数值上意外地使用了这个方法。
要解决这个问题,你需要确保你要调用 `get_rect()` 方法的对象是一个有效的图像或表面对象。在这种情况下,你可能在一个整数值上尝试调用 `get_rect()` 方法,应该调用 `get_rect()` 方法的对象是一个具有该方法的表面对象。
请检查你的代码并确保在调用 `get_rect()` 方法之前,你正在操作一个表面对象。如果你仍然遇到问题,请提供更多的代码和上下文,以便我能够更好地帮助你解决问题。
相关问题
import pygame from pygame.mixer import music import random class Ball(pygame.sprite.Sprite): def __init__(self,image_file,location,speed): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load(image_file) self.rect = self.image.get_rect() self.rect.left,self.rect.top = location self.speed = speed def move(self): self.rect = self.rect.move(self.speed) if self.rect.left < 0 or self.rect.right > width: self.speed[0] = -self.speed[0] if self.rect.top < 0 and (self.rect.left < 240 or self.rect.right > 400) : self.speed[1] = -self.speed[1] pygame.init() pygame.mixer.init() # 初始化混音器 clock = pygame.time.Clock() pygame.key.set_repeat(500,50) size = width,height = 640,480 screen = pygame.display.set_mode(size) screen.fill([255,255,255]) ball = Ball("desk_ball.png",[320,240],[10,8]) def new_func(Ball): bat = Ball("bat.png",[320,460],[0,0]) return bat bat = new_func(Ball) goal = Ball("goal.png",[240,0],[0,0]) screen.blit(ball.image,ball.rect) pygame.display.set_caption('乒乓球小游戏') #游戏标题 pygame.display.update() score = 0 lives = 5#总共有5个球 music.load("bg.mp3") # 加载背景音乐 music.play(-1) # 循环播放背景音乐,直到程序退出 done = False running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False if event.type == pygame.MOUSEMOTION: bat.rect.centerx = event.pos[0] if event.type == pygame.KEYDOWN: if event.key == pygame.K_y and lives == 0: lives = 5 done = False elif event.key == pygame.K_n and lives == 0: running = False if not done: ball.move() if pygame.sprite.collide_rect(ball,bat): ball.speed[1] = -10 if pygame.sprite.collide_rect(ball,goal): score += 1 ball.speed[1] = 10 screen.blit(ball.image,ball.rect) screen.blit(bat.image,bat.rect) for num in range(lives-1): screen.blit(ball.image,[600-num*40,0]) if ball.rect.bottom > height: lives -= 1 ball.rect.left,ball.rect.top = 320,240 if lives == 0: done = True else: over_font = pygame.font.Font(None,50) over_surf = over_font.render("Game over",1,[255,0,0]) screen.blit(over_surf,[240,240]) yn_font = pygame.font.Font(None,40) yn_surf = yn_font.render("Y:continue N:quit",1,[255,0,0]) screen.blit(yn_surf,[210,280]) score_font = pygame.font.Font(None,40) score_surf = score_font.render("score:"+str(score),1,[255,0,0]) screen.blit(score_surf,[0,0]) screen.blit(goal.image,goal.rect) pygame.display.update() clock.tick(20) screen.fill([255,255,255]) pygame.quit()基于这些代码补充在游戏界面加一条分割线
在代码中添加以下代码可以在游戏界面上添加一条分割线:
```python
# 画一条分割线
pygame.draw.line(screen, [0, 0, 0], [0, 240], [640, 240], 5)
```
将其添加在`screen.fill([255,255,255])`之后,完整代码如下:
```python
import pygame
from pygame.mixer import music
import random
class Ball(pygame.sprite.Sprite):
def __init__(self,image_file,location,speed):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left,self.rect.top = location
self.speed = speed
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.left < 0 or self.rect.right > width:
self.speed[0] = -self.speed[0]
if self.rect.top < 0 and (self.rect.left < 240 or self.rect.right > 400) :
self.speed[1] = -self.speed[1]
pygame.init()
pygame.mixer.init() # 初始化混音器
clock = pygame.time.Clock()
pygame.key.set_repeat(500,50)
size = width,height = 640,480
screen = pygame.display.set_mode(size)
screen.fill([255,255,255])
ball = Ball("desk_ball.png",[320,240],[10,8])
def new_func(Ball):
bat = Ball("bat.png",[320,460],[0,0])
return bat
bat = new_func(Ball)
goal = Ball("goal.png",[240,0],[0,0])
screen.blit(ball.image,ball.rect)
pygame.display.set_caption('乒乓球小游戏') #游戏标题
pygame.display.update()
score = 0
lives = 5#总共有5个球
music.load("bg.mp3") # 加载背景音乐
music.play(-1) # 循环播放背景音乐,直到程序退出
done = False
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEMOTION:
bat.rect.centerx = event.pos[0]
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_y and lives == 0:
lives = 5
done = False
elif event.key == pygame.K_n and lives == 0:
running = False
if not done:
ball.move()
if pygame.sprite.collide_rect(ball,bat):
ball.speed[1] = -10
if pygame.sprite.collide_rect(ball,goal):
score += 1
ball.speed[1] = 10
screen.blit(ball.image,ball.rect)
screen.blit(bat.image,bat.rect)
for num in range(lives-1):
screen.blit(ball.image,[600-num*40,0])
if ball.rect.bottom > height:
lives -= 1
ball.rect.left,ball.rect.top = 320,240
if lives == 0:
done = True
else:
over_font = pygame.font.Font(None,50)
over_surf = over_font.render("Game over",1,[255,0,0])
screen.blit(over_surf,[240,240])
yn_font = pygame.font.Font(None,40)
yn_surf = yn_font.render("Y:continue N:quit",1,[255,0,0])
screen.blit(yn_surf,[210,280])
score_font = pygame.font.Font(None,40)
score_surf = score_font.render("score:"+str(score),1,[255,0,0])
screen.blit(score_surf,[0,0])
# 画一条分割线
pygame.draw.line(screen, [0, 0, 0], [0, 240], [640, 240], 5)
screen.blit(goal.image,goal.rect)
pygame.display.update()
clock.tick(20)
screen.fill([255,255,255])
pygame.quit()
```
让这个代码中的飞船可以移动#大战外星人 import pygame from settings import Settings from ship import Ship import game_functions as gf def run_game(): pygame.init() ai_setting=Settings() screen=pygame.display.set_mode((ai_setting.screen_width,ai_setting.screen_height)) pygame.display.set_caption("alien invasion") ship=Ship(screen,ai_setting) while True: gf.check_event(ship) ship.update(screen) gf.update_screen(ai_setting,screen,ship) pygame.display.flip() run_game() #game_functions import sys import pygame def check_event(ship): for event in pygame.event.get(): if event.type == pygame.QUIT:
要让飞船可以移动,你需要在 `Ship` 类中添加移动的方法,并在 `game_functions` 模块中添加检测按键事件的函数。具体步骤如下:
1. 在 `Ship` 类中添加 `move_right()` 和 `move_left()` 方法,用于将飞船向右或向左移动一定的距离。
```python
class Ship():
def __init__(self, screen, ai_setting):
...
def move_right(self):
self.rect.centerx += self.ai_setting.ship_speed_factor
def move_left(self):
self.rect.centerx -= self.ai_setting.ship_speed_factor
```
2. 在 `game_functions` 模块中添加 `check_keydown_events()` 和 `check_keyup_events()` 函数,用于检测按键事件,并调用飞船的移动方法。
```python
def check_keydown_events(event, ship):
if event.key == pygame.K_RIGHT:
ship.move_right()
elif event.key == pygame.K_LEFT:
ship.move_left()
def check_keyup_events(event, ship):
if event.key == pygame.K_RIGHT:
# 停止向右移动
elif event.key == pygame.K_LEFT:
# 停止向左移动
```
3. 修改 `check_event()` 函数,调用检测按键事件的函数。
```python
def check_event(ship):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
check_keydown_events(event, ship)
elif event.type == pygame.KEYUP:
check_keyup_events(event, ship)
```
4. 在 `run_game()` 函数中调用 `check_event()` 函数。
```python
def run_game():
pygame.init()
ai_setting=Settings()
screen=pygame.display.set_mode((ai_setting.screen_width,ai_setting.screen_height))
pygame.display.set_caption("alien invasion")
ship=Ship(screen,ai_setting)
while True:
gf.check_event(ship)
ship.update(screen)
gf.update_screen(ai_setting,screen,ship)
pygame.display.flip()
run_game()
```
这样就可以让飞船可以通过方向键进行左右移动了。
阅读全文