用户:
我是锅铲"查看:0 回复:2 评论:0 创建时间:2022-06-29T18:49:31
【作品展示】

【作品介绍】
如果没猜错,代码应该有问题,请高手指点一下,谢谢~
【作品源代码】
import pygame
import sys
import random
width = 1200 # 窗口宽度
height = 508 # 窗口高度
size = width, height
score = None # 分数
myFont = myFont1 = None # 字体
surObject = None # 障碍物图片
surGameOver = None # 游戏结束图片
bg = None # 背景对象
role = None # 人物对象
object = None # 障碍物对象
objectList = [] # 障碍物对象数组
clock = None # 时钟
gameState = None # 游戏状态(0,1)表示(游戏中,游戏结束)
class Role: # 人物
def __init__(self, surface=None, y=None):
self.surface = surface
self.y = y
self.w = (surface.get_width())/12
self.h = surface.get_height()/2
self.currentFrame = -1
self.state = 0 # 0代表跑步状态,1代表跳跃状态,2代表连续跳跃
self.g = 1 # 重力加速度
self.vy = 0 # y轴速度
self.vy_start = -20 # 起跳开始速度
def getRect(self):
return(0,self.y+12,self.w,self.h)
class Object: #障碍物
def __init__(self,surface,x=0,y=0):
self.surface=surface
self.x=x
self.y=y
self.w=surface.get_width()
self.h=surface.get_height()
self.currentFrame=random.randint(0,6)
self.w = 100
self.h = 100
def getRect(self):
return (self.x,self.y,self.w,self.h)
def collision(self,rect1,rect2):
#碰撞检测
if (rect2[0]>=rect1[2]-20) or (rect1[0]+40>=rect2[2])or (rect1[1]+rect1[3]=508-85:
role.y=508-85
role.state=0
#障碍物的移动
addObject()
for object in objectList:
object.x-=10 #障碍物移动
# 障碍物超出屏幕,移除障碍物
if object.x+object.w<=0:
objectList.remove(object)
score+=10 #避开障碍物,加10分
print("移除了一个目标")
#碰撞检测
if object.collision(role.getRect(),object.getRect()):
if(object.currentFrame==6):
objectList.remove(object)
score+=100 #吃金币加100分
print(score)
print("吃了一个金币")
else:
gameState=1 #游戏失败
print("发生了碰撞!")
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn送你一个pygame框架
import pygame
import sys
pygame.init()
screen = pygame.display.set_mode((300, 300), 0, 32)
pygame.display.set_caption("这是游戏窗口标题")
def main():
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
sys.exit()
if __name__ == '__main__':
main()
点赞0
评论