【python】pygame 使用rect记录surface对象的位置并显示会造成卡顿(已解决)

如题,声明:只为我个人做记录,不供参考,欢迎批评指正。

目的:实现类似重力作用的效果。

想法:按空格使小球有一个向上的速度。(只按一次空格)最终效果应为:先减速向上运动,后加速向下运动。

造成卡顿的代码如下:

import pygame
import sys
from pygame.locals import *


pygame.init()
size = width, height = (1200, 600)  
screen = pygame.display.set_mode(size)  
screen.fill((255, 251, 240))


bird = pygame.image.load('./images/1.png').convert_alpha()
# 一个球,背景透明的图片
bird_rect = bird.get_rect()
bird_w, bird_h=bird_rect.width,bird_rect.height

bird_rect.x= 80
bird_rect.y = 300


move_y = 1
y_bool = True


while True:  
    if move_y < 1:
        move_y += 0.002

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

    keys_pressed = pygame.key.get_pressed()

    if keys_pressed[K_SPACE] and y_bool:
        move_y = -0.7
        y_bool = False

    elif not keys_pressed[K_SPACE]:
        y_bool = True

    if bird_rect.top > height - bird_h:  # 到底端
        bird_rect.top = height - bird_h
        
    elif bird_rect.top < 0:  # 到顶端
        bird_rect.top = 0
        move_y = 0

    bird_rect.top += move_y

    screen.fill((255, 251, 240))

    screen.blit(bird, (bird_rect.left, bird_rect.top))
    pygame.display.update()

效果如图所示:
卡顿,运行不流畅
卡顿;运行不流畅;并没有预想中的加速向下;向上运动时减速到0时会停顿长时间

而不用rect记录位置等的代码如下:

import pygame
import sys
from pygame.locals import *
import cv2


def get_img_width_hight(img_name1):
    img1 = cv2.imread(img_name1)
    size1 = img1.shape
    return size1[1], size1[0]

pygame.init()
size = width, height = (1200, 600)
screen = pygame.display.set_mode(size)
screen.fill((255, 251, 240))
bird_w, bird_h = get_img_width_hight('./images/1.png')

bird = pygame.image.load('./images/1.png').convert_alpha()

bird_x= 80
bird_y = 300


move_y = 1
y_bool = True


while True:
    if move_y < 1:
        move_y += 0.002

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

    keys_pressed = pygame.key.get_pressed()

    if keys_pressed[K_SPACE] and y_bool:
        move_y = -0.7
        y_bool = False

    elif not keys_pressed[K_SPACE]:
        y_bool = True

    if bird_y > height - bird_h:  # 到底端
        bird_y = height - bird_h

    elif bird_y < 0:  # 到顶端
        bird_y = 0
        move_y = 0

    bird_y += move_y

    screen.fill((255, 251, 240))

    screen.blit(bird, (bird_x, bird_y))
    pygame.display.update()

运行效果如下:
流畅
流畅,实现预想中的重力作用

不知为何用rect记录就会卡顿

(当然可能是我的电脑问题)

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,这是一个简单的贪吃蛇代码,你可以使用Python编写并运行它: ```python import pygame import random # 初始化pygame pygame.init() # 设置游戏界面大小 screen = pygame.display.set_mode((600, 600)) # 设置游戏标题 pygame.display.set_caption("贪吃蛇游戏") # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # 定义贪吃蛇初始位置和大小 snake_position = [300, 300] snake_body = [[300, 300], [280, 300], [260, 300]] # 食物初始位置 food_position = [random.randrange(1, 60) * 10, random.randrange(1, 60) * 10] food_spawn = True # 定义方向变量 direction = "RIGHT" change_to = direction # 定义游戏时钟 clock = pygame.time.Clock() # 定义游戏结束函数 def game_over(): font = pygame.font.SysFont('arial', 30) score = len(snake_body) - 3 text = font.render("游戏结束!得分:" + str(score), True, RED) screen.blit(text, (200, 250)) pygame.display.update() pygame.time.delay(3000) pygame.quit() quit() # 游戏主循环 while True: # 处理游戏事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT: change_to = "RIGHT" if event.key == pygame.K_LEFT: change_to = "LEFT" if event.key == pygame.K_UP: change_to = "UP" if event.key == pygame.K_DOWN: change_to = "DOWN" # 判断方向是否相反 if change_to == "RIGHT" and direction != "LEFT": direction = "RIGHT" if change_to == "LEFT" and direction != "RIGHT": direction = "LEFT" if change_to == "UP" and direction != "DOWN": direction = "UP" if change_to == "DOWN" and direction != "UP": direction = "DOWN" # 根据方向移动贪吃蛇 if direction == "RIGHT": snake_position[0] += 10 if direction == "LEFT": snake_position[0] -= 10 if direction == "UP": snake_position[1] -= 10 if direction == "DOWN": snake_position[1] += 10 # 增加贪吃蛇长度 snake_body.insert(0, list(snake_position)) if snake_position[0] == food_position[0] and snake_position[1] == food_position[1]: food_spawn = False else: snake_body.pop() # 生成食物 if not food_spawn: food_position = [random.randrange(1, 60) * 10, random.randrange(1, 60) * 10] food_spawn = True # 绘制游戏界面 screen.fill(BLACK) for pos in snake_body: pygame.draw.rect(screen, GREEN, pygame.Rect(pos[0], pos[1], 10, 10)) pygame.draw.rect(screen, BLUE, pygame.Rect(food_position[0], food_position[1], 10, 10)) # 判断游戏是否结束 if snake_position[0] < 0 or snake_position[0] > 590 or snake_position[1] < 0 or snake_position[1] > 590: game_over() for block in snake_body[1:]: if snake_position[0] == block[0] and snake_position[1] == block[1]: game_over() # 刷新游戏界面 pygame.display.update() # 控制游戏帧率 clock.tick(15) ``` 你可以将代码保存为一个.py文件,然后在命令行中运行它,或者使用Python IDE(如PyCharm)打开并运行它。运行结果将在一个黑色的游戏窗口中显示

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值