我真的是新手学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
当您按 UP
或 DOWN
时,它也会移动矩形。
它使用KEYDOWN
, KEYUP
来改变speed
并且它在每个循环中运行move()
而这个函数使用 speed
改变位置(不检查键)。
它还将 rect.top
与 screen.top
以及 rect.bottom
与 screen.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()
我是一名优秀的程序员,十分优秀!