import random import time import pygame from pygame.constants import * from pygame.image import load as addimage class Base(object): def __init__(self, image, x, y, screen): self.x = x self.y = y self.image = image self.screen = screen self.speedx= 0 self.speedy = 0 def draw(self): self.screen.blit(self.image, (self.x, self.y)) def changespeed(self, x, y): self.speedx = x self.speedy = y def changespeedx(self, x): self.speedx = x def changespeedy(self, y): self.speedy = y def move(self): self.x += self.speedx self.y += self.speedy class Background(): def __init__(self): pygame.display.set_caption("Plane War Game") self.image1 = Base(addimage('./images/bg1.png'), 0, -1200) self.image2 = Base(addimage('./images/bg2.png'), 0, -600) self.image3 = Base(addimage('./images/bg3.png'), 0, 0) self.bgs = [] self.bgs.append(self.image1) self.bgs.append(self.image2) self.bgs.append(self.image3) self.speedy = 3 for bg in self.bgs: bg.changespeedy(self.speedy) def show(self): for bg in self.bgs: bg.move() bg.draw() if bg.y > 1200: bg.y = -600 def main(): maxsize = (800, 600) screen = pygame.display.set_mode(maxsize) a = pygame.image.load("./images/bg2.png") while True: a.show() time.sleep(0.03) if __name__ == '__main__': main()
时间: 2023-07-14 10:11:48 浏览: 117
lib_base.zip_event_pygame.base_python graphic
这段代码存在一些问题,比如:
1. 在 Base 类中,没有实现对速度的限制,可能导致物体移动过快或过慢。
2. 在 Background 类中,没有考虑到循环播放背景图片的情况,会导致背景图片移动到屏幕底部后出现空白区域。
3. 在 main 函数中,调用了一个名为 a 的变量的 show 方法,但是 a 并不是一个对象,会导致 AttributeError 错误。
下面是修改后的代码:
```python
import random
import time
import pygame
from pygame.constants import *
from pygame.image import load as addimage
class Base(object):
def __init__(self, image, x, y, screen):
self.x = x
self.y = y
self.image = image
self.screen = screen
self.speedx = 0
self.speedy = 0
self.max_speed = 10 # 设置最大速度
def draw(self):
self.screen.blit(self.image, (self.x, self.y))
def changespeed(self, x, y):
if abs(self.speedx + x) < self.max_speed: # 控制速度不超过最大速度
self.speedx += x
if abs(self.speedy + y) < self.max_speed:
self.speedy += y
def changespeedx(self, x):
if abs(self.speedx + x) < self.max_speed:
self.speedx += x
def changespeedy(self, y):
if abs(self.speedy + y) < self.max_speed:
self.speedy += y
def move(self):
self.x += self.speedx
self.y += self.speedy
class Background():
def __init__(self):
pygame.display.set_caption("Plane War Game")
self.image1 = Base(addimage('./images/bg1.png'), 0, -1200)
self.image2 = Base(addimage('./images/bg2.png'), 0, -600)
self.image3 = Base(addimage('./images/bg3.png'), 0, 0)
self.bgs = []
self.bgs.append(self.image1)
self.bgs.append(self.image2)
self.bgs.append(self.image3)
self.speedy = 3
for bg in self.bgs:
bg.changespeedy(self.speedy)
def show(self):
for bg in self.bgs:
bg.move()
bg.draw()
if bg.y > 1800: # 判断是否到达屏幕底部
bg.y = self.bgs[self.bgs.index(bg)-1].y - 600 # 计算下一张图片的位置
def main():
maxsize = (800, 600)
screen = pygame.display.set_mode(maxsize)
bg = Background() # 实例化 Background 类
while True:
bg.show() # 调用 Background 类的 show 方法
pygame.display.update()
time.sleep(0.03)
if __name__ == '__main__':
main()
```
修改后的代码增加了对速度的限制,使得物体移动更加平稳;在 Background 类中新增了循环播放背景图片的功能,避免了出现空白区域的问题;在 main 函数中,创建了 Background 类的实例,并调用其 show 方法来播放背景图片。
阅读全文