创建简单的python游戏按钮函数
我正在创建一个简单的python游戏,其中一个角色四处移动以收集物品。我已经创建了一个角色,当他们浏览一个项目(一个图像)时,会弹出文本询问他们是否想要拾取该项目,以及显示“是”和“否”的按钮 如果他们单击“是”,该项目应消失,然后他们返回游戏。如果他们单击“否”,该项目将停留在那里,他们应该返回游戏。为了摆脱这个项目,我尝试在没有他们收集的项目的情况下重新绘制屏幕。但是,我不知道在他们单击“是”或“否”后如何返回游戏。如果按钮函数将他们返回到创建简单的python游戏按钮函数,python,python-3.x,pygame,Python,Python 3.x,Pygame,我正在创建一个简单的python游戏,其中一个角色四处移动以收集物品。我已经创建了一个角色,当他们浏览一个项目(一个图像)时,会弹出文本询问他们是否想要拾取该项目,以及显示“是”和“否”的按钮 如果他们单击“是”,该项目应消失,然后他们返回游戏。如果他们单击“否”,该项目将停留在那里,他们应该返回游戏。为了摆脱这个项目,我尝试在没有他们收集的项目的情况下重新绘制屏幕。但是,我不知道在他们单击“是”或“否”后如何返回游戏。如果按钮函数将他们返回到game\u loop(),则重画游戏窗口()函数将
game\u loop()
,则重画游戏窗口()
函数将激活,将角色移回其起始位置并返回项目。有人知道如何解决这个问题/在我的按钮中添加什么功能吗?谢谢大家!
代码:
导入时间
导入系统
#我们需要在所有代码开始时启动pygame
pygame.init()
显示宽度=800
显示高度=600
#创建窗口时,元组中是屏幕的宽度和高度
win=pygame.display.set_模式((显示宽度、显示高度))
x=(显示宽度*0.45)
y=(显示高度*0.8)
黑色=(0,0,0)
白色=(255255)
红色=(200,0,0)
绿色=(0200,0)
鲜红色=(255,0,0)
亮绿色=(0255,0)
紫色=(183,52235)
粉红色=(255209237)
蓝色=(184243255)
亮蓝色=(120232 255)
亮粉色=(247148208)
透明=(0,0,0,0)
def crash():
信息显示(“收集的项目”)
#钮扣
def按钮(消息、x、y、w、h、ic、ac、操作=无):
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
pygame.draw.rect(赢,ic,(x,y,w,h))
#打印(鼠标)
如果x+w>鼠标[0]>x和y+h>鼠标[1]>y:
pygame.draw.rect(赢,ac,(x,y,w,h))
如果单击[0]==1并执行操作!=无:
行动()
其他:
pygame.draw.rect(赢,ic,(x,y,w,h))
smallText=pygame.font.font(“Gameplay.ttf”,20)
textSurf,textRect=text\u对象(msg,smallText)
textRect.center=((x+(w/2),y+(h/2)))
win.blit(textSurf,textRect)
def收集_项():
按钮(“是”,150450100,50,绿色,亮绿色,移除图像)
按钮(“否”,550450100,50,红色,亮红色,游戏循环)
信息_显示('是否要拾取项目?')
#pygame.display.update()
#def game_loop():
#x=(显示宽度*0.45)
#y=(显示高度*0.8)
#x_变化=0
#减淡=0
#运行=真
#创建屏幕宽度变量是个好主意
屏幕宽度=800
#我们窗口的名称
pygame.display.set_标题(“第一场游戏”)
#用于导入动画精灵的多个图像的代码
#向右行走动画
walkRight=[pygame.image.load('R1.PNG')、pygame.image.load('R2.PNG')、pygame.image.load('R3.PNG')]
#向左行走动画
walkLeft=[pygame.image.load('L1.PNG')、pygame.image.load('L2.PNG')、pygame.image.load('L3.PNG')]
#后台图像加载
bg=pygame.image.load('flowergrass.PNG')
#基本站立的精灵,它是静止的图像。当它们不移动时显示此字符
char=pygame.image.load('front.PNG')
def喷射(x,y):
win.blit(char(x,y))
#允许我们在游戏中更改fps
clock=pygame.time.clock()
swordIMG=pygame.image.load('smallsware.png')
staffIMG=pygame.image.load('staff.png')
chestIMG=pygame.image.load('chest.png')
coinIMG=pygame.image.load('coin.png')
def text_对象(文本、字体):
textSurface=font.render(文本,真,黑色)
返回textSurface,textSurface.get_rect()
def信息_显示(文本):
largeText=pygame.font.font('freesansbold.ttf',115)
TextSurf,TextRect=text\u对象(text,largeText)
TextRect.center=((显示宽度/2)),((显示高度/2))
win.blit(TextSurf,TextRect)
pygame.display.update()
时间。睡眠(2)
def quitgame():
pygame.quit()
退出
def game_intro():
简介=正确
而简介:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
win.fill(白色)
largeText=pygame.font.font('StitchWarrior demo.ttf',115)
TextSurf,TextRect=text_对象(“Title”,largeText)
TextRect.center=((显示宽度/2)),((显示高度/2))
win.blit(TextSurf,TextRect)
#钮扣
按钮(“开始!”,150450100,50,蓝色,亮蓝色,游戏循环)
按钮(“退出”,550450100,50,粉色,亮粉色,退出游戏)
pygame.display.update()
#创造人物
x=60
y=450
#雪碧的宽度和高度
宽度=100
高度=100
#职员
员工宽度=94
员工身高=106
#硬币
宽度=74
硬币高度=74
#胸膛
胸宽=84
胸高=84
def remove_image():
温·布利特(背景,(0,0))
烧焦
剑术
切斯蒂姆
铸币
sys.exit()
#速度是角色移动的速度
水平=5
左=假
右=假
步行次数=0
#重新绘制游戏窗口的函数,此区域用于绘制,我们不在主循环中绘制
def重画游戏窗口():
#x=(显示宽度*0.45)
#y=(显示高度*0.8)
全局步行计数
win.blit(背景,(0,0))#背景图像
win.blit(swordIMG,(600400))
win.blit(staffIMG,(70,60))
win.blit(Chesting,(600100))
win.blit(coinIMG,(350300))
如果walkCount+1>=0:
步行次数=0
如果留下:
win.blit(walkLeft[walkCount],(x,y))#显示walkLeft精灵
步行次数+=1
埃利夫权利:
win.blit(walkRight[walkCount],(x,y))
步行次数+=1
#重复上下动作
其他:
win.blit(char,(x,y))#如果我们不移动,我们就会轻击我们的角色
pygame.display.update()#如果我们想在pygame的屏幕上显示一些东西,我们必须更新屏幕
#程序主循环
#主回路
#运行变量
#def game_loop():
#重画游戏窗口()
#x=(显示宽度*0.45)
#y=(显示高度*0.8)
#x_变化=0
#减淡=0
def game_loop():
全局x、y、左、右、上、下、步行计数
#x=(显示宽度*0.45)
#y=(显示高度*0.8)
x_变化=0
减淡=0
运行=真
运行时:
import time
import sys
#we need to initiate pygame at the start of all our code
pygame.init()
display_width = 800
display_height = 600
#creating window, in tuple is width and height of screen
win = pygame.display.set_mode((display_width, display_height))
x = (display_width * 0.45)
y = (display_height * 0.8)
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
bright_red = (255, 0, 0)
bright_green = (0,255,0)
purple = (183,52,235)
pink = (255, 209, 237)
blue = (184, 243, 255)
bright_blue = (120, 232, 255)
bright_pink = (247, 148, 208)
transparent = (0, 0, 0, 0)
def crash():
message_display('Item collected')
#button
def button(msg,x,y,w,h,ic,ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
pygame.draw.rect(win, ic, (x,y,w,h))
#print(mouse)
if x + w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(win, ac, (x,y,w,h))
if click[0] == 1 and action != None:
action()
else:
pygame.draw.rect(win, ic, (x,y,w,h))
smallText = pygame.font.Font("Gameplay.ttf", 20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x+(w/2), y+(h/2)))
win.blit(textSurf, textRect)
def collect_item():
button("YES",150,450,100,50,green, bright_green, remove_image)
button("NO",550,450,100, 50, red, bright_red, game_loop)
message_display('Do you want to pick up item?')
#pygame.display.update()
#def game_loop():
#x = (display_width * 0.45)
#y = (display_height * 0.8)
#x_change = 0
#dodged = 0
#run = True
#good idea to create a screen width variable
screenWidth = 800
#Name of our window
pygame.display.set_caption("First Game")
#Code for importing multiple images of the animated sprite
#walk right animation
walkRight = [pygame.image.load('R1.PNG'), pygame.image.load('R2.PNG'), pygame.image.load('R3.PNG')]
#walk left animation
walkLeft = [pygame.image.load('L1.PNG'), pygame.image.load('L2.PNG'), pygame.image.load('L3.PNG')]
#back ground image load in
bg = pygame.image.load('flowergrass.PNG')
#Basic standing sprite, it is the still image. shows this character when they are not moving
char = pygame.image.load('front.PNG')
def puff(x,y):
win.blit(char (x,y))
#allows us to change our fps in the game
clock = pygame.time.Clock()
swordIMG = pygame.image.load('smallsword.png')
staffIMG = pygame.image.load('staff.png')
chestIMG = pygame.image.load('chest.png')
coinIMG = pygame.image.load('coin.png')
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf', 115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2)), ((display_height/2))
win.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
def quitgame():
pygame.quit()
quit()
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
win.fill(white)
largeText = pygame.font.Font('StitchWarrior demo.ttf', 115)
TextSurf, TextRect = text_objects("Title", largeText)
TextRect.center = ((display_width/2)), ((display_height/2))
win.blit(TextSurf, TextRect)
#Button
button("GO!",150,450,100,50, blue, bright_blue, game_loop)
button("Quit",550,450,100, 50, pink, bright_pink, quitgame)
pygame.display.update()
#creating character
x = 60
y = 450
#width and height of sprite
width = 100
height = 100
#staff
staffwidth = 94
staffheight = 106
#coin
coinwidth = 74
coinheight = 74
#chest
chestwidth = 84
chestheight = 84
def remove_image():
win.blit(bg, (0,0))
char
swordIMG
chestIMG
coinIMG
sys.exit()
#velocity is how fast the character moves
vel = 5
left = False
right = False
walkCount = 0
#function which redraws the game window, this area is for drawing, we do not draw in main loop
def redrawGameWindow():
#x = (display_width * 0.45)
#y = (display_height * 0.8)
global walkCount
win.blit(bg, (0,0)) #back ground image
win.blit(swordIMG,(600,400))
win.blit(staffIMG, (70, 60))
win.blit(chestIMG, (600, 100))
win.blit(coinIMG, (350,300))
if walkCount + 1 >= 0:
walkCount = 0
if left:
win.blit(walkLeft[walkCount], (x,y)) #displaying walk left sprite
walkCount += 1
elif right:
win.blit(walkRight[walkCount], (x,y))
walkCount += 1
#repeat for up and down
else:
win.blit(char, (x,y)) #if we are not moving we blit our character
pygame.display.update() #if we want something to show on the screen in pygame, we must update the screen
#main loop for program
#main loop
#run the variable
#def game_loop():
#redrawGameWindow()
#x = (display_width * 0.45)
#y = (display_height * 0.8)
#x_change = 0
#dodged = 0
def game_loop():
global x, y, left, right, up, down, walkCount
#x = (display_width * 0.45)
#y = (display_height * 0.8)
x_change = 0
dodged = 0
run = True
while run:
#redrawGameWindow()
#game_intro()
clock.tick(27) #sets fps to 20 seconds
#pygame.time.delay(100) #clock in pgyame, parameter is milliseconds
for event in pygame.event.get(): #event is what player does eg. mouse click or key press
if event.type == pygame.QUIT: #if they click the x button (quit)
run = FALSE #loop = false
#using arrow keys to move shape
# all of the and's mean the shape cannot move off the screen
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
left = True
right = False
elif keys[pygame.K_RIGHT] and x < 800 - width - vel: #screen width - width of character
x += vel
right = True
left = False
elif keys[pygame.K_UP] and y > vel:
y -= vel
up = True
down = False
elif keys[pygame.K_DOWN] and y < 600 - height - vel:
y += vel
down = True
up = False
else:
right = False
left = False
up = False
down = False
walkCount = 0
if x < 100 - vel and x > 50 - vel and y > 40 - vel and y < 70:
collect_item()
if x > 600 - vel and x < 703 - vel and y > 400 - vel and y < 502 - vel:
crash()
if x > 330 - vel and x < 420 - vel and y > 280 - vel and y < 300 - vel:
crash()
if x > 600 - vel and x < 684 - vel and y > 100 - vel and y < 184 - vel:
crash()
redrawGameWindow()
#if y < 160 - vel and y > 90 - vel:
#crash()
game_intro()
#game_loop()
#redrawGameWindow() #call function
pygame.quit #game ends```}
**Updated Code**
{```import pygame
import time
import sys
#we need to initiate pygame at the start of all our code
pygame.init()
display_width = 800
display_height = 600
#creating window, in tuple is width and height of screen
win = pygame.display.set_mode((display_width, display_height))
x = (display_width * 0.45)
y = (display_height * 0.8)
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
bright_red = (255, 0, 0)
bright_green = (0,255,0)
purple = (183,52,235)
pink = (255, 209, 237)
blue = (184, 243, 255)
bright_blue = (120, 232, 255)
bright_pink = (247, 148, 208)
transparent = (0, 0, 0, 0)
def remove_image(obj):
del objs[objs.index(obj)]
def things_collected(count):
font = pygame.font.SysFont(None, 25)
text = font.render("Items: " + str(count), True, black)
win.blit(text, (0,0))
def crash():
message_display('Item collected')
#button
def button(msg,x,y,w,h,ic,ac, action=None, arg=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
pygame.draw.rect(win, ic, (x,y,w,h))
#print(mouse)
if x + w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(win, ac, (x,y,w,h))
if click[0] == 1 and action != None:
if arg:
action(arg)
else:
action()
else:
pygame.draw.rect(win, ic, (x,y,w,h))
smallText = pygame.font.Font("Gameplay.ttf", 20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x+(w/2), y+(h/2)))
win.blit(textSurf, textRect)
def collect_item():
button("YES",150,450,100,50,blue, bright_blue, remove_image,obj)
button("NO",550,450,100, 50, pink, bright_pink)
message_display('Pick up item?')
#pygame.display.update()
#def game_loop():
#x = (display_width * 0.45)
#y = (display_height * 0.8)
#x_change = 0
#dodged = 0
#run = True
#good idea to create a screen width variable
screenWidth = 800
#Name of our window
pygame.display.set_caption("First Game")
#Code for importing multiple images of the animated sprite
#walk right animation
walkRight = [pygame.image.load('R1.PNG'), pygame.image.load('R2.PNG'), pygame.image.load('R3.PNG')]
#walk left animation
walkLeft = [pygame.image.load('L1.PNG'), pygame.image.load('L2.PNG'), pygame.image.load('L3.PNG')]
#back ground image load in
bg = pygame.image.load('path.PNG')
opening_bg = pygame.image.load('openingbg.PNG')
#Basic standing sprite, it is the still image. shows this character when they are not moving
char = pygame.image.load('front.PNG')
def puff(x,y):
win.blit(char (x,y))
#allows us to change our fps in the game
clock = pygame.time.Clock()
swordIMG = pygame.image.load('smallsword.png')
staffIMG = pygame.image.load('staff.png')
chestIMG = pygame.image.load('chest.png')
coinIMG = pygame.image.load('coin.png')
class Object:
def __init__(self, x, y, image):
self.x = x
self.y = y
self.image = image
self.rect = image.get_rect()
self.rect.topleft = (self.x, self.y)
objs = []
objs.append(Object(600, 400, swordIMG))
objs.append(Object(70, 60, staffIMG))
objs.append(Object(600, 100, chestIMG))
objs.append(Object(350, 300, coinIMG))
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('Gameplay.ttf', 50)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2)), ((display_height/2))
win.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
def quitgame():
pygame.quit()
quit()
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
win.blit(opening_bg, (0,0))
largeText = pygame.font.Font('StitchWarrior demo.ttf', 70)
TextSurf, TextRect = text_objects("The Adventures of Peyia", largeText)
TextRect.center = ((display_width/2)), ((display_height/4))
win.blit(TextSurf, TextRect)
#Button
button("GO!",150,450,100,50, blue, bright_blue, game_loop)
button("Quit",550,450,100, 50, pink, bright_pink, quitgame)
pygame.display.update()
#creating character
x = 60
y = 450
#width and height of sprite
width = 100
height = 100
#staff
staffwidth = 94
staffheight = 106
#coin
coinwidth = 74
coinheight = 74
#chest
chestwidth = 84
chestheight = 84
#def remove_image():
#win.blit(bg, (0,0))
#char
#swordIMG
#chestIMG
#coinIMG
#collected += 1
#velocity is how fast the character moves
vel = 5
left = False
right = False
walkCount = 0
#function which redraws the game window, this area is for drawing, we do not draw in main loop
def redrawGameWindow():
#x = (display_width * 0.45)
#y = (display_height * 0.8)
global walkCount
win.blit(bg, (0,0)) #back ground image
#win.blit(swordIMG,(600,400))
#win.blit(staffIMG, (70, 60))
#win.blit(chestIMG, (600, 100))
#win.blit(coinIMG, (350,300))
for obj in objs:
win.blit(obj.image, obj.rect)
if walkCount + 1 >= 0:
walkCount = 0
if left:
win.blit(walkLeft[walkCount], (x,y)) #displaying walk left sprite
walkCount += 1
elif right:
win.blit(walkRight[walkCount], (x,y))
walkCount += 1
#repeat for up and down
else:
win.blit(char, (x,y)) #if we are not moving we blit our character
pygame.display.update() #if we want something to show on the screen in pygame, we must update the screen
#main loop for program
#main loop
#run the variable
#def game_loop():
#redrawGameWindow()
#x = (display_width * 0.45)
#y = (display_height * 0.8)
#x_change = 0
#dodged = 0
def game_loop():
global x, y, left, right, up, down, walkCount
x = 60
y = 450
#x = (display_width * 0.45)
#y = (display_height * 0.8)
x_change = 0
collected = 0
run = True
while run:
for obj in reversed(objs): #if delete obj in list while looping through it, the loop will still try to get the deleted obj, so reverse to fix this
if obj.rect.collidepoint((x + vel, y + vel)):
collect_item(obj) #give the object so we know which one to delete
redrawGameWindow()
#game_intro()
clock.tick(27) #sets fps to 20 seconds
#pygame.time.delay(100) #clock in pgyame, parameter is milliseconds
things_collected(collected)
for event in pygame.event.get(): #event is what player does eg. mouse click or key press
if event.type == pygame.QUIT: #if they click the x button (quit)
run = FALSE #loop = false
#using arrow keys to move shape
# all of the and's mean the shape cannot move off the screen
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
left = True
right = False
elif keys[pygame.K_RIGHT] and x < 800 - width - vel: #screen width - width of character
x += vel
right = True
left = False
elif keys[pygame.K_UP] and y > vel:
y -= vel
up = True
down = False
elif keys[pygame.K_DOWN] and y < 600 - height - vel:
y += vel
down = True
up = False
else:
right = False
left = False
up = False
down = False
walkCount = 0
if x < 100 - vel and x > 50 - vel and y > 40 - vel and y < 70:
collect_item()
if x > 600 - vel and x < 703 - vel and y > 400 - vel and y < 502 - vel:
crash()
if x > 330 - vel and x < 420 - vel and y > 280 - vel and y < 300 - vel:
crash()
if x > 600 - vel and x < 684 - vel and y > 100 - vel and y < 184 - vel:
crash()
#redrawGameWindow()
#if y < 160 - vel and y > 90 - vel:
#crash()
game_intro()
#game_loop()
#redrawGameWindow() #call function
pygame.quit #game ends
def button(msg,x,y,w,h,ic,ac, action=None, arg = None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
pygame.draw.rect(win, ic, (x,y,w,h))
#print(mouse)
if x + w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(win, ac, (x,y,w,h))
if click[0] == 1 and action != None:
if arg:
action(arg)
else:
action()
else:
pygame.draw.rect(win, ic, (x,y,w,h))
smallText = pygame.font.Font("Gameplay.ttf", 20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x+(w/2), y+(h/2)))
win.blit(textSurf, textRect)
def collect_items(obj):
button("YES",150,450,100,50,green, bright_green, remove_image,obj)
button("NO",550,450,100, 50, red, bright_red)
message_display('Do you want to pick up item?')
def remove_image(obj):
del objs[objs.index(obj)]
import time
import sys
import pygame
#we need to initiate pygame at the start of all our code
pygame.init()
display_width = 800
display_height = 600
#creating window, in tuple is width and height of screen
win = pygame.display.set_mode((display_width, display_height))
x = (display_width * 0.45)
y = (display_height * 0.8)
black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
bright_red = (255, 0, 0)
bright_green = (0,255,0)
purple = (183,52,235)
pink = (255, 209, 237)
blue = (184, 243, 255)
bright_blue = (120, 232, 255)
bright_pink = (247, 148, 208)
transparent = (0, 0, 0, 0)
#allows us to change our fps in the game
clock = pygame.time.Clock()
swordIMG = pygame.Surface((40,40))
swordIMG.fill((255,0,0))
staffIMG = swordIMG
chestIMG = swordIMG
coinIMG = swordIMG
#good idea to create a screen width variable
screenWidth = 800
#Name of our window
pygame.display.set_caption("First Game")
#Code for importing multiple images of the animated sprite
img = pygame.Surface((50,50))
img.fill((0,255,0))
#walk right animation
walkRight = [img, img, img]
#walk left animation
walkLeft = walkRight
#back ground image load in
bg = pygame.Surface((800,800))
bg.fill((255,255,255))
#Basic standing sprite, it is the still image. shows this character when they are not moving
char = pygame.Surface((50,50))
char.fill((0,255,0))
#velocity is how fast the character moves
vel = 5
left = False
right = False
walkCount = 0
#creating character
x = 60
y = 450
#width and height of sprite
width = 100
height = 100
#staff
staffwidth = 94
staffheight = 106
#coin
coinwidth = 74
coinheight = 74
#chest
chestwidth = 84
chestheight = 84
class Object:
def __init__(self, x, y, image):
self.x = x
self.y = y
self.image = image
self.rect = image.get_rect()
self.rect.topleft = (self.x, self.y)
objs = []
objs.append(Object(600, 400, swordIMG))
objs.append(Object(70, 60, staffIMG))
objs.append(Object(600, 100, chestIMG))
objs.append(Object(350, 300, coinIMG))
can_pickup = True
prev_frame_over_Object = False
def crash():
message_display('Item collected')
#button
def button(msg,x,y,w,h,ic,ac, action=None, arg=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
pygame.draw.rect(win, ic, (x,y,w,h))
#print(mouse)
if x + w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(win, ac, (x,y,w,h))
if click[0] == 1 and action != None:
if arg:
action(arg)
else:
action()
else:
pygame.draw.rect(win, ic, (x,y,w,h))
smallText = pygame.font.Font(pygame.font.match_font('calibri'), 20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x+(w/2), y+(h/2)))
win.blit(textSurf, textRect)
def NO():
global can_pickup
can_pickup = False
def collect_item(obj):
button("YES",150,450,100,50,green, bright_green, remove_image, obj)
message_display('Do you want to pick up item?')
button("NO",550,450,100, 50, red, bright_red, NO)
def puff(x,y):
win.blit(char (x,y))
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def message_display(text):
largeText = pygame.font.Font('freesansbold.ttf', 115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = ((display_width/2)), ((display_height/2))
win.blit(TextSurf, TextRect)
def quitgame():
pygame.quit()
quit()
def game_intro():
intro = True
while intro:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
win.fill(white)
largeText = pygame.font.Font(pygame.font.match_font('calibri'), 115)
TextSurf, TextRect = text_objects("Title", largeText)
TextRect.center = ((display_width/2)), ((display_height/2))
win.blit(TextSurf, TextRect)
#Button
button("GO!",150,450,100,50, blue, bright_blue, game_loop)
button("Quit",550,450,100, 50, pink, bright_pink, quitgame)
pygame.display.update()
def remove_image(obj):
del objs[objs.index(obj)]
#function which redraws the game window, this area is for drawing, we do not draw in main loop
def redrawGameWindow():
global walkCount
win.blit(bg, (0,0)) #back ground image
for obj in objs:
win.blit(obj.image, obj.rect)
if walkCount + 1 >= 0:
walkCount = 0
if left:
win.blit(walkLeft[walkCount], (x,y)) #displaying walk left sprite
walkCount += 1
elif right:
win.blit(walkRight[walkCount], (x,y))
walkCount += 1
#repeat for up and down
else:
win.blit(char, (x,y)) #if we are not moving we blit our character
def game_loop():
global x, y, left, right, up, down, walkCount, prev_frame_over_Object, can_pickup
x_change = 0
dodged = 0
run = True
while run:
#redrawGameWindow()
#game_intro()
clock.tick(27) #sets fps to 20 seconds
#pygame.time.delay(100) #clock in pgyame, parameter is milliseconds
for event in pygame.event.get(): #event is what player does eg. mouse click or key press
if event.type == pygame.QUIT: #if they click the x button (quit)
run = False #loop = false
#using arrow keys to move shape
# all of the and's mean the shape cannot move off the screen
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and x > vel:
x -= vel
left = True
right = False
elif keys[pygame.K_RIGHT] and x < 800 - width - vel: #screen width - width of character
x += vel
right = True
left = False
elif keys[pygame.K_UP] and y > vel:
y -= vel
up = True
down = False
elif keys[pygame.K_DOWN] and y < 600 - height - vel:
y += vel
down = True
up = False
else:
right = False
left = False
up = False
down = False
walkCount = 0
redrawGameWindow()
is_over = False
for obj in reversed(objs): #if delete obj in list while looping through it, the loop will still try to get the deleted obj, so reverse to fix this
if obj.rect.collidepoint((x + vel, y + vel)):
is_over = True
if prev_frame_over_Object == False:
can_pickup = True
if can_pickup:
collect_item(obj) #give the object so we know which one to delete
prev_frame_over_Object = is_over
pygame.display.update() #if we want something to show on the screen in pygame, we must update the screen
game_intro()
pygame.quit #game ends```}