gpt4 book ai didi

Python Pygame如何将矩形放在屏幕中央

转载 作者:太空宇宙 更新时间:2023-11-04 11:07:57 25 4
gpt4 key购买 nike

我真的是新手学python做练习我想创建一个矩形或任何其他图形,例如我使用的 pygame.Rect(x,y,w,h)并通过

设置 screen_center
self.screen_rect=setting.screen.get_rect()
self.screent_center=self.screen_rect.center

但是矩形的中心不在屏幕的中心我也想使用 self.b=self.bullet.get_rect() 但它显示错误我该如何解决?这是代码:

#! /usr/bin/python
import pygame as p
import sys

class Setting():
def __init__(self,width,height):
self.w=width
self.h=height
self.flag=p.RESIZABLE
self.color=(255,255,255)
self.speed=1
self.screen=p.display.set_mode((self.w,self.h),self.flag)
p.display.set_caption("Bullet")
self.bullet_s=1
self.bullet_w=100
self.bullet_h=300
self.bullet_c=(0,0,0)

class Bullet(p.sprite.Sprite):
def __init__(self,setting):
super().__init__()
self.screen_rect=setting.screen.get_rect()
self.screent_center=self.screen_rect.center

self.bullet=p.Rect((self.screen_center),(setting.bullet_w,setting.bullet_h)) **<-- not in the center**


self.b=self.bullet.get_rect() **<-- AttributeError: 'pygame.Rect' object has no attribute 'get_rect'**
self.color=setting.bullet_c
self.speed=setting.bullet_s
# self.centery=float(self.bullet.centery)
def bullet_move(self):
self.y -= self.speed
self.bullet.y=self.y
def draw_bullet(self,setting):
self.rect=p.draw.rect(setting.screen,self.color,self.bullet)


def game():
p.init()
setting=Setting(1200,800)
bullet=Bullet(setting)


while True:
for event in p.event.get():
if event.type == p.QUIT:
sys.exit()
setting.screen.fill((255,0,0))
bullet.draw_bullet(setting)
p.display.flip()
game()

最佳答案

此代码使用屏幕中心矩形

 self.screen_rect = setting.screen.get_rect()

self.rect.center = self.screen_rect.center

当您按 UPDOWN 时,它也会移动矩形。

它使用KEYDOWN, KEYUP 来改变speed 并且它在每个循环中运行move() 而这个函数使用 speed 改变位置(不检查键)。

它还将 rect.topscreen.top 以及 rect.bottomscreen.bottom 进行比较触摸屏幕边框时停止矩形。


顺便说一句:我还在代码中添加了空格和空行以使其更具可读性。

参见:PEP 8 -- Style Guide for Python Code


import pygame as p

class Setting():

def __init__(self, width, height):
self.w = width
self.h = height
self.flag = p.RESIZABLE
self.color = (255, 255, 255)
self.speed = 1

self.screen = p.display.set_mode((self.w, self.h), self.flag)

p.display.set_caption("Bullet")

self.bullet_s = 1
self.bullet_w = 100
self.bullet_h = 300
self.bullet_c = (0, 0, 0)


class Bullet(p.sprite.Sprite):

def __init__(self, setting):
super().__init__()

self.setting = setting

self.screen_rect = setting.screen.get_rect()

self.rect = p.Rect(0, 0, setting.bullet_w, setting.bullet_h)
self.rect.center = self.screen_rect.center

self.color = setting.bullet_c
self.speed = 0 #setting.bullet_s

def move(self):
self.rect.y -= self.speed

if self.rect.top < 0:
self.rect.top = 0
elif self.rect.bottom > self.screen_rect.bottom:
self.rect.bottom = self.screen_rect.bottom

def draw(self):
p.draw.rect(self.setting.screen, self.color, self.rect)

def handle_event(self, event):
if event.type == p.KEYDOWN:

if event.key == p.K_UP:
self.speed = self.setting.bullet_s
elif event.key == p.K_DOWN:
self.speed = -self.setting.bullet_s

elif event.type == p.KEYUP:

if event.key == p.K_UP:
self.speed = 0
elif event.key == p.K_DOWN:
self.speed = 0

def game():
p.init()
setting = Setting(1200,800)
bullet = Bullet(setting)

running = True
while running:
for event in p.event.get():
if event.type == p.QUIT:
running = False
bullet.handle_event(event)

bullet.move()

setting.screen.fill((255, 0, 0))
bullet.draw()
p.display.flip()

p.quit()


game()

关于Python Pygame如何将矩形放在屏幕中央,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58976335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com